Skip to content

Commit

Permalink
chore: Add pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
skorokithakis committed Nov 8, 2021
1 parent adbdfb3 commit d741a23
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 37 deletions.
23 changes: 16 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
repos:
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v1.0.0
hooks:
- id: conventional-pre-commit
stages: [commit-msg]
- repo: https://github.com/ambv/black
rev: 19.10b0
hooks:
- id: black
args: [--line-length=120]
- repo: https://github.com/asottile/reorder_python_imports
rev: v1.7.0
rev: v2.3.0
hooks:
- id: reorder-python-imports
- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.9
rev: '3.8.3'
hooks:
- id: flake8
args: ["--config=setup.cfg"]
language_version: python3
- repo: https://github.com/pycqa/pydocstyle
rev: 6.1.1
hooks:
- id: flake8
- repo: git://github.com/skorokithakis/pre-commit-mypy
rev: v0.701
- id: pydocstyle
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.782
hooks:
- id: mypy
args: [-s]
- repo: local
hooks:
- id: gitchangelog
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# Changelog


## v1.0.2 (2021-11-08)

### Features

* Added basic input type validation to encode and decode (#49) [Ivan Savov]

### Fixes

* Use sys.version_info since sys.version returns string that interprets 3.10 as 3.1 in comparison. (#54) [Karthikeyan Singaravelan]


## v1.0.1 (2020-03-06)

### Fixes
Expand Down
17 changes: 8 additions & 9 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
[flake8]
exclude=
ignore=F403,E128,E126,E123,E121,E203,E265,E501,W503
import-order-style = smarkets

[isort]
include_trailing_comma = true
line_length = 120
force_grid_wrap = 0
multi_line_output = 3
exclude = docs/*,setup.py,.*
ignore = F403,E128,E231,E126,E123,E121,E265,E501,N802,N803,N806,C901,D100,D101,D102,D105,W503

[pep8]
max-line-length = 120

[pydocstyle]
inherit = false
add-ignore = D100,D101,D102,D103,D104,D105,D106,D107,D401
add-select = D205,D213
match = .*\.py
1 change: 1 addition & 0 deletions shortuuid/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# flake8: noqa
import pkg_resources

from shortuuid.main import decode
from shortuuid.main import encode
from shortuuid.main import get_alphabet
Expand Down
35 changes: 15 additions & 20 deletions shortuuid/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Concise UUID generation. """
"""Concise UUID generation."""
import binascii
import math
import os
Expand All @@ -8,6 +8,7 @@
def int_to_string(number, alphabet, padding=None):
"""
Convert a number to a string, using the given alphabet.
The output has the most significant digit first.
"""
output = ""
Expand All @@ -24,6 +25,7 @@ def int_to_string(number, alphabet, padding=None):
def string_to_int(string, alphabet):
"""
Convert a string to a number, using the given alphabet.
The input is assumed to have the most significant digit first.
"""
number = 0
Expand All @@ -36,21 +38,20 @@ def string_to_int(string, alphabet):
class ShortUUID(object):
def __init__(self, alphabet=None):
if alphabet is None:
alphabet = list("23456789ABCDEFGHJKLMNPQRSTUVWXYZ" "abcdefghijkmnopqrstuvwxyz")
alphabet = list(
"23456789ABCDEFGHJKLMNPQRSTUVWXYZ" "abcdefghijkmnopqrstuvwxyz"
)

self.set_alphabet(alphabet)

@property
def _length(self):
"""
Return the necessary length to fit the entire UUID given
the current alphabet.
"""
"""Return the necessary length to fit the entire UUID given the current alphabet."""
return int(math.ceil(math.log(2 ** 128, self._alpha_len)))

def encode(self, uuid, pad_length=None):
"""
Encode a UUID into a string (LSB first) according to the alphabet
Encode a UUID into a string (LSB first) according to the alphabet.
If leftmost (MSB) bits are 0, the string might be shorter.
"""
Expand All @@ -62,14 +63,14 @@ def encode(self, uuid, pad_length=None):

def decode(self, string, legacy=False):
"""
Decode a string according to the current alphabet into a UUID
Raises ValueError when encountering illegal characters
or a too-long string.
Decode a string according to the current alphabet into a UUID.
Raises ValueError when encountering illegal characters or a too-long string.
If string too short, fills leftmost (MSB) bits with 0.
Pass `legacy=True` if your UUID was encoded with a ShortUUID version
prior to 1.0.0.
Pass `legacy=True` if your UUID was encoded with a ShortUUID version prior to
1.0.0.
"""
if not isinstance(string, str):
raise ValueError("Input `string` must be a str.")
Expand Down Expand Up @@ -97,10 +98,7 @@ def uuid(self, name=None, pad_length=None):
return self.encode(u, pad_length)

def random(self, length=None):
"""
Generate and return a cryptographically-secure short random string
of the specified length.
"""
"""Generate and return a cryptographically secure short random string of `length`."""
if length is None:
length = self._length

Expand All @@ -113,7 +111,6 @@ def get_alphabet(self):

def set_alphabet(self, alphabet):
"""Set the alphabet to be used for new UUIDs."""

# Turn the alphabet into a set and sort it to prevent duplicates
# and ensure reproducibility.
new_alphabet = list(sorted(set(alphabet)))
Expand All @@ -124,9 +121,7 @@ def set_alphabet(self, alphabet):
raise ValueError("Alphabet with more than " "one unique symbols required.")

def encoded_length(self, num_bytes=16):
"""
Returns the string length of the shortened UUID.
"""
"""Return the string length of the shortened UUID."""
factor = math.log(256) / math.log(self._alpha_len)
return int(math.ceil(factor * num_bytes))

Expand Down
4 changes: 3 additions & 1 deletion shortuuid/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ def test_encoded_length(self):
su1 = ShortUUID()
self.assertEqual(su1.encoded_length(), 22)

base64_alphabet = string.ascii_uppercase + string.ascii_lowercase + string.digits + "+/"
base64_alphabet = (
string.ascii_uppercase + string.ascii_lowercase + string.digits + "+/"
)

su2 = ShortUUID(base64_alphabet)
self.assertEqual(su2.encoded_length(), 22)
Expand Down

0 comments on commit d741a23

Please sign in to comment.