Repository navigation
1. Дябденко Богдан Константинович. 613 #74
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
Changes from all commits
c9de47d
91e402f
65fc2c7
48ac6b7
a406051
cb1fb21
9337433
c0e0371
caa09d9
eb0df79
fd2fd1e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,45 +13,35 @@ | |
| :func:`decode_node_id` and :func:`decode_sequence_id`. | ||
| """ | ||
|
|
||
| import time # noqa: F401 | ||
| import time | ||
|
|
||
| from .constants import ( # noqa: F401 | ||
| from .constants import ( | ||
| EPOCH_MS_DEFAULT, | ||
| NODE_ID_BITS, | ||
| NODE_ID_DEFAULT, | ||
| NODE_ID_MAX, | ||
| SEQUENCE_ID_BITS, | ||
| SEQUENCE_ID_MAX, | ||
| TIMESTAMP_MS_MAX, | ||
| ) | ||
|
|
||
|
|
||
| def read_current_millis(epoch_ms: int) -> int: | ||
| """Read the number of milliseconds elapsed since the custom epoch. | ||
|
|
||
| Args: | ||
| epoch_ms: The custom epoch expressed as Unix milliseconds. | ||
| user_time = int(time.time() * 1000) | ||
|
|
||
| Returns: | ||
| The count of whole milliseconds between ``epoch_ms`` and now. May be | ||
| negative if ``epoch_ms`` lies in the future. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| differnse_time = user_time - epoch_ms | ||
|
|
||
| return differnse_time | ||
|
|
||
|
|
||
| def decode_timestamp_ms(snowflake_id: int, epoch_ms: int = EPOCH_MS_DEFAULT) -> int: | ||
| """Read the timestamp field out of a Snowflake identifier. | ||
|
|
||
| Args: | ||
| snowflake_id: An identifier produced against the same epoch. | ||
| epoch_ms: The epoch the identifier was generated against. Defaults to | ||
| the original Twitter epoch (2010-11-04 01:42:54.657 UTC). | ||
| user_mc = snowflake_id >> (NODE_ID_BITS + SEQUENCE_ID_BITS) | ||
|
|
||
| Returns: | ||
| The absolute Unix time in milliseconds at which the identifier was | ||
| generated. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| absolut_time = user_mc + epoch_ms | ||
|
|
||
| return absolut_time | ||
|
|
||
|
|
||
| def decode_node_id(snowflake_id: int) -> int: | ||
|
|
@@ -64,8 +54,8 @@ def decode_node_id(snowflake_id: int) -> int: | |
| The node identifier packed into ``snowflake_id``, in the range | ||
| ``[0, NODE_ID_MAX]``. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| identifier_id = (snowflake_id >> SEQUENCE_ID_BITS) & NODE_ID_MAX | ||
| return identifier_id | ||
|
|
||
|
|
||
| def decode_sequence_id(snowflake_id: int) -> int: | ||
|
|
@@ -78,8 +68,8 @@ def decode_sequence_id(snowflake_id: int) -> int: | |
| The per-millisecond sequence counter packed into ``snowflake_id``, in | ||
| the range ``[0, SEQUENCE_ID_MAX]``. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| sequence_id = snowflake_id & SEQUENCE_ID_MAX | ||
| return sequence_id | ||
|
|
||
|
|
||
| def generate_snowflake_id( | ||
|
|
@@ -110,5 +100,19 @@ def generate_snowflake_id( | |
| timestamp field (roughly 69 years after ``epoch_ms``). In each of those | ||
| cases an explanatory message is printed to stdout first. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| if node_id < 0 or node_id > NODE_ID_MAX: | ||
| print(f"node_id must be in [0, {NODE_ID_MAX}], got {node_id}") | ||
| return None | ||
| if sequence_id < 0 or sequence_id > SEQUENCE_ID_MAX: | ||
| print(f"sequence_id must be in [0, {SEQUENCE_ID_MAX}], got {sequence_id}") | ||
| return None | ||
| elapsed_ms = read_current_millis(epoch_ms) | ||
| if elapsed_ms > TIMESTAMP_MS_MAX: | ||
| print(f"timestamp overflows: {elapsed_ms} > {TIMESTAMP_MS_MAX}") | ||
| return None | ||
| snowflake_id = ( | ||
| (elapsed_ms << (NODE_ID_BITS + SEQUENCE_ID_BITS)) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вычисление сдвига дублировано в функции |
||
| | (node_id << SEQUENCE_ID_BITS) | ||
| | sequence_id | ||
| ) | ||
| return snowflake_id | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Не проходит
ruff check