Repository navigation
4. Дьяков Григорий Александрович. 614 #60
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
Open
dyakovgregory-bot
wants to merge
9
commits into
python-department:main
Choose a base branch
from
dyakovgregory-bot:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0a6ffa4
Solve task 1
dyakovgregory-bot f90e2d8
Solve task 1
dyakovgregory-bot 5f7983b
Merge branch 'python-department:main' into main
dyakovgregory-bot ee02850
Solve task 2
dyakovgregory-bot 760d4ae
Solve task 2
dyakovgregory-bot bf94c79
Merge branch 'python-department:main' into main
dyakovgregory-bot c531036
Solve task 3
dyakovgregory-bot 37beac4
Merge branch 'python-department:main' into main
dyakovgregory-bot 1fb352b
Solve task 4
dyakovgregory-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,12 +13,13 @@ | |
| :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_DEFAULT, | ||
| NODE_ID_MAX, | ||
| SEQUENCE_ID_BITS, | ||
| SEQUENCE_ID_MAX, | ||
| TIMESTAMP_MS_MAX, | ||
| ) | ||
|
|
@@ -34,8 +35,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.0 - epoch_ms) | ||
|
|
||
|
|
||
| def decode_timestamp_ms(snowflake_id: int, epoch_ms: int = EPOCH_MS_DEFAULT) -> int: | ||
|
|
@@ -50,8 +50,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 (snowflake_id >> (NODE_ID_MAX + SEQUENCE_ID_MAX)) + epoch_ms | ||
|
|
||
|
|
||
| def decode_node_id(snowflake_id: int) -> int: | ||
|
|
@@ -64,8 +63,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) & NODE_ID_MAX | ||
|
|
||
|
|
||
| def decode_sequence_id(snowflake_id: int) -> int: | ||
|
|
@@ -78,8 +76,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 & SEQUENCE_ID_MAX | ||
|
|
||
|
|
||
| def generate_snowflake_id( | ||
|
|
@@ -110,5 +107,21 @@ 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}], but 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 is {sequence_id}") | ||
| return None | ||
|
|
||
| timestamp = read_current_millis(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. Из-за ошибки в |
||
| if timestamp > TIMESTAMP_MS_MAX: | ||
| print("timestamp overflows") | ||
| return None | ||
|
|
||
| return ( | ||
| (timestamp << (NODE_ID_MAX + SEQUENCE_ID_MAX)) | ||
| | (node_id << SEQUENCE_ID_BITS) | ||
| | sequence_id | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Идея правильная, но реализация неверная: значение сдвига не то. Правый аргумент сдвига - число битов, на которое этот сдвиг совершается. Число
snowflake_idсостоит из 63 битов, а у вас толькоNODE_ID_MAXимеет значение 1023. Т.е. вы на очень большое значение вправо все сдвините, получите 0 и прибавите к немуepoch_ms. В итоге функция всегда возвращает значениеepoch_ms.