init with some kind of working prototype
This commit is contained in:
commit
b5cb22822e
22 changed files with 1292 additions and 0 deletions
31
unwind/types.py
Normal file
31
unwind/types.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import re
|
||||
from typing import Union, cast
|
||||
|
||||
import ulid
|
||||
from ulid.hints import Buffer
|
||||
|
||||
|
||||
class ULID(ulid.ULID):
|
||||
"""Extended ULID type.
|
||||
|
||||
Same as ulid.ULID, but allows initializing without a buffer, to make
|
||||
it easier to use the class as a standard factory.
|
||||
|
||||
For more information about ULIDs, see https://github.com/ulid/spec.
|
||||
"""
|
||||
|
||||
_pattern = re.compile(r"^[0-9A-HJKMNP-TV-Z]{26}$")
|
||||
|
||||
def __init__(self, buffer: Union[Buffer, ulid.ULID, str, None] = None):
|
||||
if isinstance(buffer, str):
|
||||
if not self._pattern.search(buffer):
|
||||
raise ValueError("Invalid ULID.")
|
||||
buffer = ulid.from_str(buffer)
|
||||
assert isinstance(buffer, ulid.ULID)
|
||||
|
||||
if isinstance(buffer, ulid.ULID):
|
||||
buffer = cast(memoryview, buffer.memory)
|
||||
elif buffer is None:
|
||||
buffer = cast(memoryview, ulid.new().memory)
|
||||
|
||||
super().__init__(buffer)
|
||||
Loading…
Add table
Add a link
Reference in a new issue