Repository navigation
1. Кательников Кирилл Владимирович. 614 #22
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
base: main
Are you sure you want to change the base?
Changes from all commits
3176441
259056f
bf69af2
316aee5
da22fa8
8a22886
268bc4f
c5e0f2f
e708f77
89df45e
bdc5c1b
059081e
78d3119
a37f462
f462419
0102cfc
b15f4b9
b52a662
d2fd434
11fdaf1
7cfeb0f
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,12 +13,17 @@ | |
| :func:`decode_node_id` and :func:`decode_sequence_id`. | ||
| """ | ||
|
|
||
| import time # noqa: F401 | ||
| import time | ||
|
|
||
| from .constants import ( # noqa: F401 | ||
| from .constants import ( | ||
| # Epoch | ||
| EPOCH_MS_DEFAULT, | ||
| # Node ID | ||
| NODE_ID_BITS, | ||
| NODE_ID_DEFAULT, | ||
| NODE_ID_MAX, | ||
| # Sequence ID | ||
| SEQUENCE_ID_BITS, | ||
| SEQUENCE_ID_MAX, | ||
| TIMESTAMP_MS_MAX, | ||
| ) | ||
|
|
@@ -35,7 +40,8 @@ def read_current_millis(epoch_ms: int) -> int: | |
| negative if ``epoch_ms`` lies in the future. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| a = (time.time_ns() // 1000000) - epoch_ms | ||
|
Member
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. Неинформативное имя переменной. Невозможно понять, какой физический смысл заложен в переменную с именем Минус 0.5 балла |
||
| return a | ||
|
|
||
|
|
||
| def decode_timestamp_ms(snowflake_id: int, epoch_ms: int = EPOCH_MS_DEFAULT) -> int: | ||
|
|
@@ -51,7 +57,8 @@ def decode_timestamp_ms(snowflake_id: int, epoch_ms: int = EPOCH_MS_DEFAULT) -> | |
| generated. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| epoch_time = snowflake_id >> (SEQUENCE_ID_BITS + NODE_ID_BITS) | ||
| return epoch_time + epoch_ms | ||
|
|
||
|
|
||
| def decode_node_id(snowflake_id: int) -> int: | ||
|
|
@@ -65,7 +72,8 @@ def decode_node_id(snowflake_id: int) -> int: | |
| ``[0, NODE_ID_MAX]``. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| node_num = (snowflake_id >> SEQUENCE_ID_BITS) & NODE_ID_MAX | ||
| return node_num | ||
|
|
||
|
|
||
| def decode_sequence_id(snowflake_id: int) -> int: | ||
|
|
@@ -79,7 +87,8 @@ def decode_sequence_id(snowflake_id: int) -> int: | |
| the range ``[0, SEQUENCE_ID_MAX]``. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| seq_id = snowflake_id & SEQUENCE_ID_MAX | ||
| return seq_id | ||
|
|
||
|
|
||
| def generate_snowflake_id( | ||
|
|
@@ -111,4 +120,19 @@ def generate_snowflake_id( | |
| cases an explanatory message is printed to stdout first. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| if not (0 <= node_id <= NODE_ID_MAX): | ||
| print(f"node_id must be in [0, {NODE_ID_MAX}], got {node_id}") | ||
| return None | ||
| elif not (0 <= sequence_id <= SEQUENCE_ID_MAX): | ||
| print(f"sequence_id must be in [0, {SEQUENCE_ID_MAX}], got {sequence_id}") | ||
| return None | ||
| a = (time.time_ns() // 1000000) - epoch_ms | ||
|
Member
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. Копипаста. У вас же уже реализована функция Минус 1 балл за копипасту |
||
| if a > TIMESTAMP_MS_MAX: | ||
| print("overflows") | ||
| return None | ||
| else: | ||
|
Member
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. Ненужный |
||
| return ( | ||
| (a << (NODE_ID_BITS + SEQUENCE_ID_BITS)) | ||
| | (node_id << SEQUENCE_ID_BITS) | ||
| | sequence_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.
Использование магической константы
41. Если в будущем длина полей в идентификаторе изменится, придется вручную искать места, где встречается эта константа, и исправлять все самостоятельно.Правильнее было бы использовать константу
TIMESTAMP_BITSдля расчетов.Минус 1 балл за использование магических констант