Repository navigation
1. Раджабов Руслан Ильгамович. 614 #17
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
somet1me123
wants to merge
23
commits into
python-department:main
Choose a base branch
from
somet1me123: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
23 commits
Select commit
Hold shift + click to select a range
9df3598
modificed all functions
096d048
Solve task 1
ff04e6c
Solve task 1
1e1ef11
Solve task 1
3e39cb7
Solve task 1
42215d8
Merge branch 'python-department:main' into main
somet1me123 a540f3b
add constants to storage.py
c213746
modificated function in utils.py
c30612d
modificed all functions and solved task 2
2bbf0cf
slightly modificed the solution
da0d232
Merge branch 'python-department:main' into main
somet1me123 1a3503c
started to modify func. in crud.py
af32ad3
Changed some functions, preparing for ending assignment
4554e9f
Solved task 3
91467b7
Merge branch 'python-department:main' into main
somet1me123 56ba4d0
changed storage.py and utils.py
94d1549
changed all func
6caa7a4
Merge branch 'python-department:main' into main
somet1me123 f22886a
Modified constants.py
bc63970
Modified snowflake.py
2daa80f
Modified decorators.py
4213deb
Modified all functions
f5dc6ce
Repaired all functions
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,102 +13,63 @@ | |
| :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, | ||
| ) | ||
|
|
||
|
|
||
| def read_current_millis(epoch_ms: int) -> int: | ||
| """Read the number of milliseconds elapsed since the custom epoch. | ||
| current_time = int(time.time() * 1000) | ||
|
|
||
| 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 current_time - 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). | ||
| time_mark = snowflake_id >> (NODE_ID_BITS + SEQUENCE_ID_BITS) | ||
|
|
||
| Returns: | ||
| The absolute Unix time in milliseconds at which the identifier was | ||
| generated. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| return time_mark + epoch_ms | ||
|
|
||
|
|
||
| def decode_node_id(snowflake_id: int) -> int: | ||
| """Read the node identifier field out of a Snowflake identifier. | ||
| without_seq = snowflake_id >> SEQUENCE_ID_BITS | ||
| node_id = without_seq & NODE_ID_MAX | ||
|
|
||
| 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 node_id | ||
|
|
||
|
|
||
| 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`. | ||
| sequence_id = snowflake_id & SEQUENCE_ID_MAX | ||
|
|
||
| Returns: | ||
| The per-millisecond sequence counter packed into ``snowflake_id``, in | ||
| the range ``[0, SEQUENCE_ID_MAX]``. | ||
| """ | ||
| # TODO: реализуйте функцию | ||
| return 0 | ||
| return sequence_id | ||
|
|
||
|
|
||
| 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 | ||
| time_stamp = read_current_millis(epoch_ms) | ||
| if not (0 <= node_id <= NODE_ID_MAX): | ||
| print("node_id must be in [0, 2**10-1]") | ||
| return None | ||
| elif not (0 <= sequence_id <= SEQUENCE_ID_MAX): | ||
| print("sequence_id must be in [0, 2**12-1]") | ||
| return None | ||
| elif time_stamp > TIMESTAMP_MS_MAX: | ||
| print("overflows") | ||
| return None | ||
| snowflake_id = ( | ||
| (time_stamp << (NODE_ID_BITS + SEQUENCE_ID_BITS)) | ||
|
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. Вычисление сдвига дублируется. Нарушение критерия |
||
| | (node_id << SEQUENCE_ID_BITS) | ||
| | sequence_id | ||
| ) | ||
|
|
||
| return snowflake_id | ||
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.
Лучше не делать такую инъекцию констант в строку. При изменении значения константы придется вручную вспоминать, где вы сделали инъекцию, и менять вручную это сообщение. Вместо этого, лучше использовать f-строки и вставлять значения констант