From d615eda6c73629dea1a34d513b04f5cc191738f4 Mon Sep 17 00:00:00 2001 From: SimencaSs <74181505+SimencaSs@users.noreply.github.com> Date: Wed, 23 Sep 2026 17:41:30 +0300 Subject: [PATCH 01/15] hhh --- src/part1/snowflake.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/part1/snowflake.py b/src/part1/snowflake.py index d3abf050..a5eba8ee 100644 --- a/src/part1/snowflake.py +++ b/src/part1/snowflake.py @@ -9,7 +9,7 @@ caller passes the sequence counter on every call and is responsible for advancing it within a millisecond and resetting it when the clock ticks over. -Each packed field can be read back on its own with :func:`decode_timestamp_ms`, +Each packed field can be read back on its own with :func:`decode_timestamp_ms`,hhjhj :func:`decode_node_id` and :func:`decode_sequence_id`. """ From e24ec21fd3f4fc69ad2b34ba820ac9170bd39f6c Mon Sep 17 00:00:00 2001 From: SimencaSs <74181505+SimencaSs@users.noreply.github.com> Date: Wed, 23 Sep 2026 17:45:05 +0300 Subject: [PATCH 02/15] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D1=8B?= =?UTF-8?q?=D0=B9=20=D0=BA=D0=BE=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/part1/constants.py | 46 +++------------- src/part1/snowflake.py | 122 ++++++++--------------------------------- 2 files changed, 31 insertions(+), 137 deletions(-) diff --git a/src/part1/constants.py b/src/part1/constants.py index 9abfbba2..05135d68 100644 --- a/src/part1/constants.py +++ b/src/part1/constants.py @@ -1,38 +1,8 @@ -"""Bit layout and default epoch for the Twitter Snowflake ID generator. - -A Snowflake identifier is a 63-bit positive integer packed as follows, from -the most significant bit downwards: - - 1 bit unused sign bit, always zero - 41 bits milliseconds elapsed since a custom epoch - 10 bits node identifier - 12 bits per-millisecond sequence counter - -The shift and mask constants should be derived from the bit widths so that -changing a width keeps everything else consistent. -""" - -from typing import Final - - -# TODO: замените заглушки (0) на корректные значения, см. TASK.md. -# Все константы, зависящие от ёмкостей *_BITS, должны вычисляться из них. - -# Twitter's original Snowflake epoch: 2010-11-04 01:42:54.657 UTC. -EPOCH_MS_DEFAULT: Final[int] = 0 - -# Node identifier used when the caller does not supply one. -NODE_ID_DEFAULT: Final[int] = 0 - -# Width of each field, in bits. -TIMESTAMP_BITS: Final[int] = 0 -NODE_ID_BITS: Final[int] = 0 -SEQUENCE_ID_BITS: Final[int] = 0 - -# Largest value each field can hold. -TIMESTAMP_MS_MAX: Final[int] = 0 -NODE_ID_MAX: Final[int] = 0 -SEQUENCE_ID_MAX: Final[int] = 0 - -# Здесь можно добавить собственные вспомогательные константы -# (например, сдвиги полей при сборке идентификатора). +EPOCH_MS_DEFAULT = 1288834974657 +NODE_ID_DEFAULT = 0 +TIMESTAMP_BITS, NODE_ID_BITS, SEQUENCE_ID_BITS = 41, 10, 12 +TIMESTAMP_MS_MAX = 2**TIMESTAMP_BITS - 1 +NODE_ID_MAX = 2**NODE_ID_BITS - 1 +SEQUENCE_ID_MAX = 2**SEQUENCE_ID_BITS - 1 +NODE_ID_SHIFT = SEQUENCE_ID_BITS +TIMESTAMP_SHIFT = SEQUENCE_ID_BITS + NODE_ID_BITS diff --git a/src/part1/snowflake.py b/src/part1/snowflake.py index a5eba8ee..ec0dc666 100644 --- a/src/part1/snowflake.py +++ b/src/part1/snowflake.py @@ -1,114 +1,38 @@ -"""Twitter Snowflake identifier generator. - -Snowflake produces roughly time-ordered 64-bit integer identifiers without -coordination between nodes. Each identifier packs the milliseconds elapsed -since a custom epoch, a node identifier and a per-millisecond sequence counter -into a single 63-bit positive integer (see ``constants`` for the layout). - -The public entry point is :func:`generate_snowflake_id`. It is stateless: the -caller passes the sequence counter on every call and is responsible for -advancing it within a millisecond and resetting it when the clock ticks over. - -Each packed field can be read back on its own with :func:`decode_timestamp_ms`,hhjhj -:func:`decode_node_id` and :func:`decode_sequence_id`. -""" - -import time # noqa: F401 - -from .constants import ( # noqa: F401 - EPOCH_MS_DEFAULT, - NODE_ID_DEFAULT, - NODE_ID_MAX, - SEQUENCE_ID_MAX, - TIMESTAMP_MS_MAX, -) +import time +from constants import * 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 time.time_ns() // 1_000_000 - epoch_ms 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 - + stored_ms = snowflake_id >> TIMESTAMP_SHIFT + return stored_ms + 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 >> NODE_ID_SHIFT) & NODE_ID_MAX 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 & SEQUENCE_ID_MAX def generate_snowflake_id( - sequence_id: int, - node_id: int = NODE_ID_DEFAULT, - epoch_ms: int = EPOCH_MS_DEFAULT, + 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. + if not (0 <= node_id <= NODE_ID_MAX): + print(f"node_id must be in [0, {NODE_ID_MAX}], got {node_id}") + return None + + if not (0 <= sequence_id <= SEQUENCE_ID_MAX): + print(f"sequence_id must be in [0, {SEQUENCE_ID_MAX}], got {sequence_id}") + return None - 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). + elapsed_ms = read_current_millis(epoch_ms) + if elapsed_ms > TIMESTAMP_MS_MAX: + print(f"timestamp overflows: {elapsed_ms} > {TIMESTAMP_MS_MAX}") + return None - 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 + snowflake_id = ( + (elapsed_ms << TIMESTAMP_SHIFT) | (node_id << NODE_ID_SHIFT) | (sequence_id) + ) + return snowflake_id \ No newline at end of file From bc9c9364072788f6304b968089ab4aeb42674bd2 Mon Sep 17 00:00:00 2001 From: SimencaSs <74181505+SimencaSs@users.noreply.github.com> Date: Wed, 23 Sep 2026 17:52:47 +0300 Subject: [PATCH 03/15] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D1=8B?= =?UTF-8?q?=D0=B9=20=D0=BA=D0=BE=D0=B4=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/part1/snowflake.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/part1/snowflake.py b/src/part1/snowflake.py index ec0dc666..af2eff98 100644 --- a/src/part1/snowflake.py +++ b/src/part1/snowflake.py @@ -1,6 +1,14 @@ import time -from constants import * +from constants import ( + EPOCH_MS_DEFAULT, + NODE_ID_DEFAULT, + NODE_ID_MAX, + NODE_ID_SHIFT, + SEQUENCE_ID_MAX, + TIMESTAMP_MS_MAX, + TIMESTAMP_SHIFT, +) def read_current_millis(epoch_ms: int) -> int: return time.time_ns() // 1_000_000 - epoch_ms @@ -16,7 +24,9 @@ def decode_sequence_id(snowflake_id: int) -> int: return snowflake_id & SEQUENCE_ID_MAX def generate_snowflake_id( - sequence_id: int, node_id: int = NODE_ID_DEFAULT, epoch_ms: int = EPOCH_MS_DEFAULT + sequence_id: int, + node_id: int = NODE_ID_DEFAULT, + epoch_ms: int = EPOCH_MS_DEFAULT, ) -> int | None: if not (0 <= node_id <= NODE_ID_MAX): @@ -32,7 +42,8 @@ def generate_snowflake_id( print(f"timestamp overflows: {elapsed_ms} > {TIMESTAMP_MS_MAX}") return None - snowflake_id = ( - (elapsed_ms << TIMESTAMP_SHIFT) | (node_id << NODE_ID_SHIFT) | (sequence_id) - ) - return snowflake_id \ No newline at end of file + return ( + (elapsed_ms << TIMESTAMP_SHIFT) + | (node_id << NODE_ID_SHIFT) + | sequence_id + ) \ No newline at end of file From c08ad7071075016d5e583c007c45d5c8d4909a87 Mon Sep 17 00:00:00 2001 From: SimencaSs <74181505+SimencaSs@users.noreply.github.com> Date: Thu, 24 Sep 2026 22:34:28 +0300 Subject: [PATCH 04/15] =?UTF-8?q?=D0=92=D0=B5=D1=80=D1=81=D0=B8=D1=8F=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/part1/snowflake.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/part1/snowflake.py b/src/part1/snowflake.py index af2eff98..049c065d 100644 --- a/src/part1/snowflake.py +++ b/src/part1/snowflake.py @@ -11,7 +11,7 @@ ) def read_current_millis(epoch_ms: int) -> int: - return time.time_ns() // 1_000_000 - epoch_ms + return time.time_ns() // 1000000 - epoch_ms def decode_timestamp_ms(snowflake_id: int, epoch_ms: int = EPOCH_MS_DEFAULT) -> int: stored_ms = snowflake_id >> TIMESTAMP_SHIFT @@ -43,7 +43,5 @@ def generate_snowflake_id( return None return ( - (elapsed_ms << TIMESTAMP_SHIFT) - | (node_id << NODE_ID_SHIFT) - | sequence_id + (elapsed_ms << TIMESTAMP_SHIFT) | (node_id << NODE_ID_SHIFT) | sequence_id ) \ No newline at end of file From a09a31cf3424e9341ae4b0ad8333b7899ac711bc Mon Sep 17 00:00:00 2001 From: SimencaSs <74181505+SimencaSs@users.noreply.github.com> Date: Thu, 24 Sep 2026 22:47:27 +0300 Subject: [PATCH 05/15] =?UTF-8?q?=D0=92=D0=B5=D1=80=D1=81=D0=B8=D1=8F=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/part1/snowflake.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/part1/snowflake.py b/src/part1/snowflake.py index 049c065d..4589acb3 100644 --- a/src/part1/snowflake.py +++ b/src/part1/snowflake.py @@ -10,23 +10,30 @@ TIMESTAMP_SHIFT, ) + def read_current_millis(epoch_ms: int) -> int: return time.time_ns() // 1000000 - epoch_ms + def decode_timestamp_ms(snowflake_id: int, epoch_ms: int = EPOCH_MS_DEFAULT) -> int: stored_ms = snowflake_id >> TIMESTAMP_SHIFT return stored_ms + epoch_ms + def decode_node_id(snowflake_id: int) -> int: return (snowflake_id >> NODE_ID_SHIFT) & NODE_ID_MAX + def decode_sequence_id(snowflake_id: int) -> int: return snowflake_id & SEQUENCE_ID_MAX + def generate_snowflake_id( sequence_id: int, node_id: int = NODE_ID_DEFAULT, epoch_ms: int = EPOCH_MS_DEFAULT, + + ) -> int | None: if not (0 <= node_id <= NODE_ID_MAX): From 7069e220f3466c1109a90a8391f941297acc4c3d Mon Sep 17 00:00:00 2001 From: SimencaSs <74181505+SimencaSs@users.noreply.github.com> Date: Thu, 24 Sep 2026 22:51:10 +0300 Subject: [PATCH 06/15] =?UTF-8?q?=D0=92=D0=B5=D1=80=D1=81=D0=B8=D1=8F=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/part1/snowflake.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/part1/snowflake.py b/src/part1/snowflake.py index 4589acb3..d7096813 100644 --- a/src/part1/snowflake.py +++ b/src/part1/snowflake.py @@ -35,7 +35,6 @@ def generate_snowflake_id( ) -> int | None: - if not (0 <= node_id <= NODE_ID_MAX): print(f"node_id must be in [0, {NODE_ID_MAX}], got {node_id}") return None @@ -49,6 +48,4 @@ def generate_snowflake_id( print(f"timestamp overflows: {elapsed_ms} > {TIMESTAMP_MS_MAX}") return None - return ( - (elapsed_ms << TIMESTAMP_SHIFT) | (node_id << NODE_ID_SHIFT) | sequence_id - ) \ No newline at end of file + return (elapsed_ms << TIMESTAMP_SHIFT) | (node_id << NODE_ID_SHIFT) | sequence_id \ No newline at end of file From 40434a025fdf2564482f27d01c8506f1cc4581b3 Mon Sep 17 00:00:00 2001 From: SimencaSs <74181505+SimencaSs@users.noreply.github.com> Date: Thu, 24 Sep 2026 22:54:51 +0300 Subject: [PATCH 07/15] Update snowflake.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Версия 5 --- src/part1/snowflake.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/part1/snowflake.py b/src/part1/snowflake.py index d7096813..2b2e5927 100644 --- a/src/part1/snowflake.py +++ b/src/part1/snowflake.py @@ -32,8 +32,6 @@ def generate_snowflake_id( sequence_id: int, node_id: int = NODE_ID_DEFAULT, epoch_ms: int = EPOCH_MS_DEFAULT, - - ) -> int | None: if not (0 <= node_id <= NODE_ID_MAX): print(f"node_id must be in [0, {NODE_ID_MAX}], got {node_id}") From e4cc97b07465d4b3ff1c7062b17dcd2da8b5296b Mon Sep 17 00:00:00 2001 From: SimencaSs <74181505+SimencaSs@users.noreply.github.com> Date: Thu, 24 Sep 2026 22:59:02 +0300 Subject: [PATCH 08/15] =?UTF-8?q?=D0=92=D0=B0=D1=80=D0=B8=D0=B0=D0=BD?= =?UTF-8?q?=D1=82=205?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/part1/snowflake.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/part1/snowflake.py b/src/part1/snowflake.py index 2b2e5927..1d56c62b 100644 --- a/src/part1/snowflake.py +++ b/src/part1/snowflake.py @@ -46,4 +46,5 @@ def generate_snowflake_id( print(f"timestamp overflows: {elapsed_ms} > {TIMESTAMP_MS_MAX}") return None - return (elapsed_ms << TIMESTAMP_SHIFT) | (node_id << NODE_ID_SHIFT) | sequence_id \ No newline at end of file + return ( + elapsed_ms << TIMESTAMP_SHIFT) | (node_id << NODE_ID_SHIFT) | sequence_id \ No newline at end of file From 8ff5dbf8c9235fe0b9058bb1135342229adf2db9 Mon Sep 17 00:00:00 2001 From: SimencaSs <74181505+SimencaSs@users.noreply.github.com> Date: Thu, 24 Sep 2026 23:02:01 +0300 Subject: [PATCH 09/15] =?UTF-8?q?=D0=92=D0=B0=D1=80=D0=B8=D0=B0=D0=BD?= =?UTF-8?q?=D1=82=206?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/part1/snowflake.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/part1/snowflake.py b/src/part1/snowflake.py index 1d56c62b..be626222 100644 --- a/src/part1/snowflake.py +++ b/src/part1/snowflake.py @@ -46,5 +46,4 @@ def generate_snowflake_id( print(f"timestamp overflows: {elapsed_ms} > {TIMESTAMP_MS_MAX}") return None - return ( - elapsed_ms << TIMESTAMP_SHIFT) | (node_id << NODE_ID_SHIFT) | sequence_id \ No newline at end of file + return ( elapsed_ms << TIMESTAMP_SHIFT) | (node_id << NODE_ID_SHIFT) | sequence_id \ No newline at end of file From b913ec417b20e37f0e6557aed181e2a353a746a6 Mon Sep 17 00:00:00 2001 From: SimencaSs <74181505+SimencaSs@users.noreply.github.com> Date: Thu, 24 Sep 2026 23:03:14 +0300 Subject: [PATCH 10/15] =?UTF-8?q?=D0=92=D0=B0=D1=80=D0=B8=D0=B0=D0=BD?= =?UTF-8?q?=D1=82=207?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/part1/snowflake.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/part1/snowflake.py b/src/part1/snowflake.py index be626222..2b2e5927 100644 --- a/src/part1/snowflake.py +++ b/src/part1/snowflake.py @@ -46,4 +46,4 @@ def generate_snowflake_id( print(f"timestamp overflows: {elapsed_ms} > {TIMESTAMP_MS_MAX}") return None - return ( elapsed_ms << TIMESTAMP_SHIFT) | (node_id << NODE_ID_SHIFT) | sequence_id \ No newline at end of file + return (elapsed_ms << TIMESTAMP_SHIFT) | (node_id << NODE_ID_SHIFT) | sequence_id \ No newline at end of file From c921b9f98c98bc47d83b9977c7551b2c404ebea8 Mon Sep 17 00:00:00 2001 From: SimencaSs <74181505+SimencaSs@users.noreply.github.com> Date: Thu, 24 Sep 2026 23:10:43 +0300 Subject: [PATCH 11/15] =?UTF-8?q?=D0=92=D0=B0=D1=80=D0=B8=D0=B0=D0=BD?= =?UTF-8?q?=D1=82=208?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/part1/snowflake.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/part1/snowflake.py b/src/part1/snowflake.py index 2b2e5927..9e92b7ad 100644 --- a/src/part1/snowflake.py +++ b/src/part1/snowflake.py @@ -46,4 +46,8 @@ def generate_snowflake_id( print(f"timestamp overflows: {elapsed_ms} > {TIMESTAMP_MS_MAX}") return None - return (elapsed_ms << TIMESTAMP_SHIFT) | (node_id << NODE_ID_SHIFT) | sequence_id \ No newline at end of file + return ( + (elapsed_ms << TIMESTAMP_SHIFT) + | (node_id << NODE_ID_SHIFT) + | sequence_id + ) \ No newline at end of file From 62f28bb8993b075a352f1f544936ada756b76e3f Mon Sep 17 00:00:00 2001 From: SimencaSs <74181505+SimencaSs@users.noreply.github.com> Date: Thu, 24 Sep 2026 23:12:19 +0300 Subject: [PATCH 12/15] =?UTF-8?q?=D0=92=D0=B0=D1=80=D0=B8=D0=B0=D0=BD?= =?UTF-8?q?=D1=82=209?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/part1/snowflake.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/part1/snowflake.py b/src/part1/snowflake.py index 9e92b7ad..2b2e5927 100644 --- a/src/part1/snowflake.py +++ b/src/part1/snowflake.py @@ -46,8 +46,4 @@ def generate_snowflake_id( print(f"timestamp overflows: {elapsed_ms} > {TIMESTAMP_MS_MAX}") return None - return ( - (elapsed_ms << TIMESTAMP_SHIFT) - | (node_id << NODE_ID_SHIFT) - | sequence_id - ) \ No newline at end of file + return (elapsed_ms << TIMESTAMP_SHIFT) | (node_id << NODE_ID_SHIFT) | sequence_id \ No newline at end of file From 9bf797b29b9087e62af5d33489b386cabec0122f Mon Sep 17 00:00:00 2001 From: SimencaSs <74181505+SimencaSs@users.noreply.github.com> Date: Thu, 24 Sep 2026 23:59:22 +0300 Subject: [PATCH 13/15] =?UTF-8?q?=D0=92=D0=B0=D1=80=D0=B8=D0=B0=D0=BD?= =?UTF-8?q?=D1=82=2010?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/part1/constants.py | 20 ++++++++++++-------- src/part1/snowflake.py | 2 +- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/part1/constants.py b/src/part1/constants.py index 05135d68..478de728 100644 --- a/src/part1/constants.py +++ b/src/part1/constants.py @@ -1,8 +1,12 @@ -EPOCH_MS_DEFAULT = 1288834974657 -NODE_ID_DEFAULT = 0 -TIMESTAMP_BITS, NODE_ID_BITS, SEQUENCE_ID_BITS = 41, 10, 12 -TIMESTAMP_MS_MAX = 2**TIMESTAMP_BITS - 1 -NODE_ID_MAX = 2**NODE_ID_BITS - 1 -SEQUENCE_ID_MAX = 2**SEQUENCE_ID_BITS - 1 -NODE_ID_SHIFT = SEQUENCE_ID_BITS -TIMESTAMP_SHIFT = SEQUENCE_ID_BITS + NODE_ID_BITS +from typing import Final + +EPOCH_MS_DEFAULT: Final[int] = 1288834974657 +NODE_ID_DEFAULT: Final[int] = 0 +TIMESTAMP_BITS: Final[int] = 41 +NODE_ID_BITS: Final[int] = 10 +SEQUENCE_ID_BITS: Final[int] = 12 +TIMESTAMP_MS_MAX: Final[int] = 2**TIMESTAMP_BITS - 1 +NODE_ID_MAX: Final[int] = 2**NODE_ID_BITS - 1 +SEQUENCE_ID_MAX: Final[int] = 2**SEQUENCE_ID_BITS - 1 +NODE_ID_SHIFT: Final[int] = SEQUENCE_ID_BITS +TIMESTAMP_SHIFT: Final[int] = SEQUENCE_ID_BITS + NODE_ID_BITS diff --git a/src/part1/snowflake.py b/src/part1/snowflake.py index 2b2e5927..fc8a3fa9 100644 --- a/src/part1/snowflake.py +++ b/src/part1/snowflake.py @@ -46,4 +46,4 @@ def generate_snowflake_id( print(f"timestamp overflows: {elapsed_ms} > {TIMESTAMP_MS_MAX}") return None - return (elapsed_ms << TIMESTAMP_SHIFT) | (node_id << NODE_ID_SHIFT) | sequence_id \ No newline at end of file + return (elapsed_ms << TIMESTAMP_SHIFT) | (node_id << NODE_ID_SHIFT) | sequence_id From 97247760f0b296349774e56b6190bc72edcc9157 Mon Sep 17 00:00:00 2001 From: SimencaSs <74181505+SimencaSs@users.noreply.github.com> Date: Fri, 25 Sep 2026 00:03:31 +0300 Subject: [PATCH 14/15] =?UTF-8?q?=D0=92=D0=B0=D1=80=D0=B8=D0=B0=D0=BD?= =?UTF-8?q?=D1=82=2011?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/part1/constants.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/part1/constants.py b/src/part1/constants.py index 478de728..0339d24f 100644 --- a/src/part1/constants.py +++ b/src/part1/constants.py @@ -1,5 +1,6 @@ from typing import Final + EPOCH_MS_DEFAULT: Final[int] = 1288834974657 NODE_ID_DEFAULT: Final[int] = 0 TIMESTAMP_BITS: Final[int] = 41 From 66079fe9790091d966b857772852e0aee75d70af Mon Sep 17 00:00:00 2001 From: SimencaSs <74181505+SimencaSs@users.noreply.github.com> Date: Fri, 25 Sep 2026 00:28:49 +0300 Subject: [PATCH 15/15] =?UTF-8?q?=D0=92=D0=B0=D1=80=D0=B8=D0=B0=D0=BD?= =?UTF-8?q?=D1=82=2012?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pyproject.toml | 7 +++++++ src/part1/__init__.py | 1 - src/part1/snowflake.py | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 06fbe913..83913475 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,3 +33,10 @@ avoid-escape = true [tool.ruff.lint.isort] lines-after-imports = 2 split-on-trailing-comma = true + +[tool.mypy] +strict = true +mypy_path = "src" +namespace_packages = true +explicit_package_bases = true + diff --git a/src/part1/__init__.py b/src/part1/__init__.py index 242b07ce..e69de29b 100644 --- a/src/part1/__init__.py +++ b/src/part1/__init__.py @@ -1 +0,0 @@ -"""Homework 3: a Twitter Snowflake identifier generator and its bit layout.""" diff --git a/src/part1/snowflake.py b/src/part1/snowflake.py index fc8a3fa9..a3f3fe8f 100644 --- a/src/part1/snowflake.py +++ b/src/part1/snowflake.py @@ -1,6 +1,6 @@ import time -from constants import ( +from part1.constants import ( EPOCH_MS_DEFAULT, NODE_ID_DEFAULT, NODE_ID_MAX,