Skip to content

The Snowflake generator done right.

License

Notifications You must be signed in to change notification settings

vd2org/snowflake

Repository files navigation

Snowflake

The Snowflake generator done right.

See here for additional information.

Requirements

Python 3.8 and above. No additional dependencies.

Installation

pip install snowflake-id

Usage

Using generator

from src.snowflake import SnowflakeGenerator

gen = SnowflakeGenerator(42)

for i in range(100):
    val = next(gen)
    print(val)

Output:

6820698575169822721
6820698575169822722
6820698575169822723
6820698575174017024
6820698575174017025
...

Parse snowflake id

from src.snowflake import Snowflake

sf = Snowflake.parse(856165981072306191, 1288834974657)

print(f"{sf.timestamp = }")
print(f"{sf.instance = }")
print(f"{sf.epoch = }")
print(f"{sf.seq = }")
print(f"{sf.seconds = }")
print(f"{sf.milliseconds = }")
print(f"{sf.datetime = }")
print(f"{int(sf) = }")

Output:

sf.timestamp = 204125876682
sf.instance = 363
sf.epoch = 1288834974657
sf.seq = 15
sf.seconds = 1492960851.339
sf.milliseconds = 1492960851339
sf.datetime = datetime.datetime(2017, 4, 23, 15, 20, 51, 339500)
int(sf) = 856165981072306191

Load generator state

from src.snowflake import SnowflakeGenerator, Snowflake

sf = Snowflake.parse(856165981072306191, 1288834974657)
gen = SnowflakeGenerator.from_snowflake(sf)

for i in range(100):
    val = next(gen)
    print(val)

Output:

1414934653136449536
1414934653136449537
1414934653136449538
1414934653136449539
1414934653136449540
...