Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vd2org/snowflake
The Snowflake generator done right.
https://github.com/vd2org/snowflake
id id-generator ids snowflake snowflake-id
Last synced: 6 days ago
JSON representation
The Snowflake generator done right.
- Host: GitHub
- URL: https://github.com/vd2org/snowflake
- Owner: vd2org
- License: mit
- Created: 2021-07-13T10:30:20.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-06-12T08:16:04.000Z (6 months ago)
- Last Synced: 2024-12-09T01:16:55.642Z (13 days ago)
- Topics: id, id-generator, ids, snowflake, snowflake-id
- Language: Python
- Homepage:
- Size: 58.6 KB
- Stars: 101
- Watchers: 5
- Forks: 12
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Snowflake
The Snowflake generator done right.
See [here](https://en.wikipedia.org/wiki/Snowflake_ID) for additional information.
### Requirements
Python 3.8 and above. No additional dependencies.
### Installation
`pip install snowflake-id`
## Usage
### Using generator
```python
from snowflake import SnowflakeGeneratorgen = SnowflakeGenerator(42)
for i in range(100):
val = next(gen)
print(val)
```#### Output:
```text
6820698575169822721
6820698575169822722
6820698575169822723
6820698575174017024
6820698575174017025
...
```### Parse snowflake id
```python
from snowflake import Snowflakesf = 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:
```text
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, 339000)
int(sf) = 856165981072306191
```### Load generator state
```python
from snowflake import SnowflakeGenerator, Snowflakesf = Snowflake.parse(856165981072306191, 1288834974657)
gen = SnowflakeGenerator.from_snowflake(sf)for i in range(100):
val = next(gen)
print(val)
```#### Output:
```text
1414934653136449536
1414934653136449537
1414934653136449538
1414934653136449539
1414934653136449540
...
```