snowflake
所属分类:数据库系统
开发工具:Python
文件大小:0KB
下载次数:0
上传日期:2023-07-03 21:00:23
上 传 者:
sh-1993
说明: 雪花发生器做得不错。,
(The Snowflake generator done right.,)
文件列表:
.bumpversion.cfg (160, 2023-11-08)
LICENSE (1066, 2023-11-08)
requirements-package.txt (23, 2023-11-08)
requirements-test.txt (14, 2023-11-08)
setup.py (1555, 2023-11-08)
snowflake/ (0, 2023-11-08)
snowflake/__init__.py (201, 2023-11-08)
snowflake/snowflake.py (4112, 2023-11-08)
tests/ (0, 2023-11-08)
tests/test_generator.py (2727, 2023-11-08)
tests/test_snowflake.py (2523, 2023-11-08)
# Snowflake
The Snowflake generator done right.
See [here](https://en.wikipedia.org/wiki/Snowflake_ID) for additional information.
### Requirements
Python 3.7 and above. No additional dependencies.
### Installation
`pip install snowflake-id`
## Usage
### Using generator
```python
from snowflake import SnowflakeGenerator
gen = 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 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:
```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, Snowflake
sf = 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
...
```
近期下载者:
相关文件:
收藏者: