Ejemplo n.º 1
0
    def python_value(self, value):
        if value is None:
            return value

        if isinstance(value, uuid.UUID):
            return timeflake.parse(from_hex=value.hex)

        return timeflake.parse(from_hex=uuid.UUID(value).hex)
Ejemplo n.º 2
0
    def python_value(self, value):
        if value is None:
            return value

        if isinstance(value, timeflake.Timeflake):
            return value

        return timeflake.parse(from_base62=value)
Ejemplo n.º 3
0
def _parse(value) -> timeflake.Timeflake:
    if value is None:
        return value
    elif isinstance(value, timeflake.Timeflake):
        return value
    elif isinstance(value, bytes):
        return timeflake.parse(from_bytes=value)
    elif isinstance(value, int):
        return timeflake.parse(from_int=value)
    elif isinstance(value, str):
        size = len(value)
        # hex
        if size == 32:
            return timeflake.parse(from_hex=value)
        # base62
        elif size == 22:
            return timeflake.parse(from_base62=value)
    elif isinstance(value, uuid.UUID):
        return timeflake.parse(from_bytes=value.bytes)
    raise ValueError(f"Could not parse Timeflake from {value}")
Ejemplo n.º 4
0
def test_parse_int_and_conversions():
    flake = timeflake.parse(from_int=1909005012028578488143182045514754249)
    assert isinstance(flake, timeflake.Timeflake)
    assert isinstance(flake.random, int)
    assert isinstance(flake.timestamp, int)
    assert flake.timestamp == 1579091935216
    assert flake.random == 724773312193627487660233
    assert flake.int == 1909005012028578488143182045514754249
    assert flake.hex == "016fa936bff0997a0a3c428548fee8c9"
    assert flake.base62 == "02i1KoFfY3auBS745gImbZ"
    assert flake.bytes == b"\x01o\xa96\xbf\xf0\x99z\n<B\x85H\xfe\xe8\xc9"
    assert flake.uuid == uuid.UUID("016fa936-bff0-997a-0a3c-428548fee8c9")