Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify imports #29

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions ksuid/ksuid.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import math
import secrets
import typing as t
from datetime import datetime, timezone
from datetime import datetime as datetime_lib
from datetime import timezone
from functools import total_ordering
from typing import Optional, Type, TypeVar

from baseconv import base62

Expand All @@ -16,7 +17,7 @@ class ByteArrayLengthException(Exception):
pass


SelfT = t.TypeVar("SelfT", bound="Ksuid")
SelfT = TypeVar("SelfT", bound="Ksuid")


@total_ordering
Expand All @@ -38,13 +39,13 @@ class Ksuid:
_uid: bytes

@classmethod
def from_base62(cls: t.Type[SelfT], data: str) -> SelfT:
def from_base62(cls: Type[SelfT], data: str) -> SelfT:
"""initializes Ksuid from base62 encoding"""

return cls.from_bytes(int.to_bytes(int(base62.decode(data)), cls.BYTES_LENGTH, "big"))

@classmethod
def from_bytes(cls: t.Type[SelfT], value: bytes) -> SelfT:
def from_bytes(cls: Type[SelfT], value: bytes) -> SelfT:
"""initializes Ksuid from bytes"""

if len(value) != cls.TIMESTAMP_LENGTH_IN_BYTES + cls.PAYLOAD_LENGTH_IN_BYTES:
Expand All @@ -55,9 +56,7 @@ def from_bytes(cls: t.Type[SelfT], value: bytes) -> SelfT:

return res

def __init__(self, datetime: t.Optional[datetime] = None, payload: t.Optional[bytes] = None):
from datetime import datetime as datetime_lib

def __init__(self, datetime: Optional[datetime_lib] = None, payload: Optional[bytes] = None):
if payload is not None and len(payload) != self.PAYLOAD_LENGTH_IN_BYTES:
raise ByteArrayLengthException()

Expand Down Expand Up @@ -86,16 +85,16 @@ def __lt__(self: SelfT, other: SelfT) -> bool:
def __hash__(self) -> int:
return int.from_bytes(self._uid, "big")

def _inner_init(self, dt: datetime, payload: bytes) -> bytes:
timestamp = int(dt.timestamp() - EPOCH_STAMP)
def _inner_init(self, datetime: datetime_lib, payload: bytes) -> bytes:
timestamp = int(datetime.timestamp() - EPOCH_STAMP)

return int.to_bytes(timestamp, self.TIMESTAMP_LENGTH_IN_BYTES, "big") + payload

@property
def datetime(self) -> datetime:
def datetime(self) -> datetime_lib:
unix_time = self.timestamp

return datetime.fromtimestamp(unix_time, tz=timezone.utc)
return datetime_lib.fromtimestamp(unix_time, tz=timezone.utc)

@property
def timestamp(self) -> float:
Expand All @@ -121,8 +120,8 @@ class KsuidMs(Ksuid):

TIMESTAMP_MULTIPLIER = 256

def _inner_init(self, dt: datetime, payload: bytes) -> bytes:
timestamp = round((dt.timestamp() - EPOCH_STAMP) * self.TIMESTAMP_MULTIPLIER)
def _inner_init(self, datetime: datetime_lib, payload: bytes) -> bytes:
timestamp = round((datetime.timestamp() - EPOCH_STAMP) * self.TIMESTAMP_MULTIPLIER)

return int.to_bytes(timestamp, self.TIMESTAMP_LENGTH_IN_BYTES, "big") + payload

Expand Down