Skip to content

Commit

Permalink
feat: Add UUID Generator
Browse files Browse the repository at this point in the history
  • Loading branch information
justiceHui committed Aug 21, 2023
1 parent 5d9b537 commit f2660b6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/polytope/core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
__all__ = [
"PolytopeUUID",
"uuid",
]

from .uuidgen import PolytopeUUID, uuid
56 changes: 56 additions & 0 deletions src/polytope/core/uuidgen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import random

# collision probability in 150,000 entries ~ 1%
# lowercase alphabet + digit except [l, 1, o, 0]
DEFAULT_ALPHABET = "abcdefghijkmnpqrstuvwxyz23456789"
DEFAULT_LENGTH = 8


class PolytopeUUID:
"""UUID generator class."""

def __init__(self, alphabet: str = None, length: int = 0):
"""! PolytopeUUID class initializer.
@param alphabet alphabet for generating uuid
@param length length of uuid
"""
if alphabet is None:
self._alphabet = DEFAULT_ALPHABET
else:
self._alphabet = alphabet

if length == 0:
self._length = DEFAULT_LENGTH
else:
self._length = length

@property
def alphabet(self):
"""! An alphabet property for generating uuid."""
return self._alphabet

@property
def length(self):
"""! A length property of uuid."""
return self._length

@alphabet.setter
def alphabet(self, value):
"""! A getter method for alphabet property."""
self._alphabet = value

@length.setter
def length(self, value):
"""! A getter method for length property."""
self._length = value

def uuid(self) -> str:
"""! A method for generating uuid."""
char_list = [random.choice(self.alphabet) for _ in range(self.length)]
return "".join(char_list)


def uuid(alphabet: str = DEFAULT_ALPHABET, length: int = DEFAULT_LENGTH):
generator = PolytopeUUID(alphabet, length)
return generator.uuid()

0 comments on commit f2660b6

Please sign in to comment.