Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions fix_and_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
# Auto-fix imports/format, then run tests.
set -euo pipefail

cd "$(dirname "$0")"

if ! command -v uv >/dev/null 2>&1; then
echo "Error: uv is not installed. Install it: https://docs.astral.sh/uv/getting-started/installation/" >&2
exit 1
fi

echo "==> Install dependencies"
uv sync --locked

echo "==> Ruff fix (imports, lint)"
uv run ruff check --fix src
uv run ruff format src

echo "==> Pytest"
uv run pytest src/part3/tests

echo "All done."
11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencies = []
dev = [
"mypy==2.3.1",
"ruff==0.16.8",
"pytest==9.1.1",
]

[tool.ruff]
Expand All @@ -33,3 +34,13 @@ avoid-escape = true
[tool.ruff.lint.isort]
lines-after-imports = 2
split-on-trailing-comma = true

[tool.mypy]
strict = true
files = ["src"]
mypy_path = "src"
explicit_package_bases = true

[tool.pytest.ini_options]
pythonpath = ["src"]
testpaths = ["src/tests"]
22 changes: 8 additions & 14 deletions src/part1/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,18 @@
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
EPOCH_MS_DEFAULT: Final[int] = 1288834974657

# Node identifier used when the caller does not supply one.
NODE_ID_DEFAULT: Final[int] = 0
NODE_ID_DEFAULT: Final[int] = 1

# Width of each field, in bits.
TIMESTAMP_BITS: Final[int] = 0
NODE_ID_BITS: Final[int] = 0
SEQUENCE_ID_BITS: Final[int] = 0
TIMESTAMP_BITS: Final[int] = 41
NODE_ID_BITS: Final[int] = 10
SEQUENCE_ID_BITS: Final[int] = 12

# Largest value each field can hold.
TIMESTAMP_MS_MAX: Final[int] = 0
NODE_ID_MAX: Final[int] = 0
SEQUENCE_ID_MAX: Final[int] = 0

# Здесь можно добавить собственные вспомогательные константы
# (например, сдвиги полей при сборке идентификатора).
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
37 changes: 25 additions & 12 deletions src/part1/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
: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,
)
Expand All @@ -34,8 +36,7 @@ def read_current_millis(epoch_ms: int) -> int:
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


def decode_timestamp_ms(snowflake_id: int, epoch_ms: int = EPOCH_MS_DEFAULT) -> int:
Expand All @@ -50,8 +51,7 @@ def decode_timestamp_ms(snowflake_id: int, epoch_ms: int = EPOCH_MS_DEFAULT) ->
The absolute Unix time in milliseconds at which the identifier was
generated.
"""
# TODO: реализуйте функцию
return 0
return epoch_ms + (snowflake_id >> (NODE_ID_BITS + SEQUENCE_ID_BITS))


def decode_node_id(snowflake_id: int) -> int:
Expand All @@ -64,8 +64,7 @@ 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
return (snowflake_id >> SEQUENCE_ID_BITS) & ((1 << NODE_ID_BITS) - 1)


def decode_sequence_id(snowflake_id: int) -> int:
Expand All @@ -78,8 +77,7 @@ 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
return (snowflake_id) & ((1 << SEQUENCE_ID_BITS) - 1)


def generate_snowflake_id(
Expand Down Expand Up @@ -110,5 +108,20 @@ 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 not (0 <= node_id <= NODE_ID_MAX):
print(f"node_id must be in [0, {NODE_ID_MAX}], received {node_id}")
return None
if not (0 <= sequence_id <= SEQUENCE_ID_MAX):
print(f"sequence_id must be in [0, {SEQUENCE_ID_MAX}], received {sequence_id}")
return None

elapsed = read_current_millis(epoch_ms)
if elapsed > TIMESTAMP_MS_MAX:
print("overflows")
return None

return (
(elapsed << (NODE_ID_BITS + SEQUENCE_ID_BITS))
| (node_id << SEQUENCE_ID_BITS)
| sequence_id
)
Loading
Loading