Repository navigation
1.Кривчанская Елена Ивановна.614. #136
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
734973b
8b1ac7b
749cade
0df2861
97d390d
8dc93d5
f9153b3
fddb8de
f9f2117
2bb9747
6586588
8f90ead
3dfb800
319c9b5
d3d7c81
f1320ae
9009f68
9ddfe92
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,102 +13,56 @@ | |
| :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_BITS, | ||
| 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. | ||
|
|
||
| Returns: | ||
| The count of whole milliseconds between ``epoch_ms`` and now. May be | ||
| negative if ``epoch_ms`` lies in the future. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| return int(time.time() * 1000) - epoch_ms - EPOCH_MS_DEFAULT | ||
|
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. Это ошибка. Вычитание |
||
|
|
||
|
|
||
| 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). | ||
|
|
||
| Returns: | ||
| The absolute Unix time in milliseconds at which the identifier was | ||
| generated. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| creation_time = (snowflake_id >> SEQUENCE_ID_BITS >> NODE_ID_BITS) & ( | ||
| (1 << TIMESTAMP_BITS) - 1 | ||
|
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 creation_time + epoch_ms | ||
|
|
||
|
|
||
| def decode_node_id(snowflake_id: int) -> int: | ||
| """Read the node identifier field out of a Snowflake identifier. | ||
|
|
||
| Args: | ||
| snowflake_id: An identifier produced by :func:`generate_snowflake_id`. | ||
|
|
||
| Returns: | ||
| The node identifier packed into ``snowflake_id``, in the range | ||
| ``[0, NODE_ID_MAX]``. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| return (snowflake_id >> SEQUENCE_ID_BITS) & ((1 << NODE_ID_BITS) - 1) | ||
|
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. |
||
|
|
||
|
|
||
| def decode_sequence_id(snowflake_id: int) -> int: | ||
| """Read the sequence counter field out of a Snowflake identifier. | ||
|
|
||
| Args: | ||
| snowflake_id: An identifier produced by :func:`generate_snowflake_id`. | ||
|
|
||
| Returns: | ||
| The per-millisecond sequence counter packed into ``snowflake_id``, in | ||
| the range ``[0, SEQUENCE_ID_MAX]``. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| return (snowflake_id) & ((1 << SEQUENCE_ID_BITS) - 1) | ||
|
|
||
|
|
||
| def generate_snowflake_id( | ||
| sequence_id: int, | ||
| node_id: int = NODE_ID_DEFAULT, | ||
| epoch_ms: int = EPOCH_MS_DEFAULT, | ||
| ) -> int | None: | ||
| """Build and return a Snowflake identifier for the current millisecond. | ||
|
|
||
| The function is stateless: the caller passes the per-millisecond sequence | ||
| counter explicitly. It is the caller's responsibility to increment | ||
| ``sequence_id`` for identifiers minted within the same millisecond and to | ||
| reset it once the clock advances. | ||
|
|
||
| Args: | ||
| sequence_id: The per-millisecond sequence counter, in the range | ||
| ``[0, SEQUENCE_ID_MAX]``. | ||
| node_id: The identifier of this node, in the range ``[0, NODE_ID_MAX]``. | ||
| Optional; defaults to ``NODE_ID_DEFAULT``. | ||
| epoch_ms: The start of the epoch as Unix milliseconds. Optional; | ||
| defaults to the original Twitter epoch (2010-11-04 01:42:54.657 | ||
| UTC). | ||
|
|
||
| Returns: | ||
| The Snowflake identifier as a positive 63-bit integer, or ``None`` if | ||
| ``node_id`` is outside ``[0, NODE_ID_MAX]``, ``sequence_id`` is outside | ||
| ``[0, SEQUENCE_ID_MAX]``, or the elapsed time no longer fits in the | ||
| 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 not (0 <= node_id < NODE_ID_MAX): | ||
| print(f"node_id must be in [0, {NODE_ID_MAX}], but it is {node_id}") | ||
| return None | ||
| if not (0 <= sequence_id < SEQUENCE_ID_MAX): | ||
| print(f"sequence_id must be in [0, {SEQUENCE_ID_MAX}], but it is {sequence_id}") | ||
| return None | ||
|
|
||
| epoch_ms = int(time.time() * 1000) - EPOCH_MS_DEFAULT | ||
|
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. Здесь следовало вызывать функцию для расчета |
||
| if not (0 <= epoch_ms < TIMESTAMP_MS_MAX): | ||
| print("overflow") | ||
| return None | ||
|
|
||
| return ( | ||
| (epoch_ms << NODE_ID_MAX << SEQUENCE_ID_MAX) | (node_id << SEQUENCE_ID_MAX) | ||
| ) | 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.
Дублирование значения. Вы ранее уже определили переменную
TIMESTAMP_BITS, которую следовало использовать тут. Теперь, если значениеTIMESTAMP_BITS, придется идти и вносить дополнительную правку еще и тут. Это потенциальный источник ошибок при поддержки кода.Нарушение критерия
S1. Штраф - 1 баллА еще само значение неверно рассчитано.
TIMESTAMP_MS_MAXдолжен заниматьTIMESTAMP_BITSбитов. Значение2 ** 41занимает 42 бита, т.е. на один больше, чем требуется. Аналогично с другими максимальными значениями