Skip to content

Commit e6b12d9

Browse files
committed
🎲 fix generating random quote/author ids
1 parent 03a3a51 commit e6b12d9

File tree

2 files changed

+2
-10
lines changed

2 files changed

+2
-10
lines changed

‎an_website/quotes/utils.py‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
)
3333
from dataclasses import dataclass
3434
from datetime import date
35-
from multiprocessing import Value
3635
from typing import Any, Final, Literal, cast
3736
from urllib.parse import urlencode
3837

@@ -81,9 +80,6 @@ class UltraDictType[K, V](MutableMapping[K, V], abc.ABC):
8180
UltraDict(buffer_size=1024**2, serializer=dill)
8281
)
8382

84-
MAX_QUOTES_ID = Value("Q", 0)
85-
MAX_AUTHORS_ID = Value("Q", 0)
86-
8783

8884
@dataclass(init=False, slots=True)
8985
class QuotesObjBase(abc.ABC):
@@ -437,7 +433,6 @@ def parse_author(json_data: Mapping[str, Any]) -> Author:
437433
if author is None:
438434
# pylint: disable-next=too-many-function-args
439435
author = Author(id_, name, None)
440-
MAX_AUTHORS_ID.value = max(MAX_AUTHORS_ID.value, id_)
441436
elif author.name != name:
442437
author.name = name
443438
author.info = None # reset info
@@ -474,7 +469,6 @@ def parse_quote(
474469
if quote is None: # new quote
475470
# pylint: disable=too-many-function-args
476471
quote = Quote(quote_id, quote_str, author.id)
477-
MAX_QUOTES_ID.value = max(MAX_QUOTES_ID.value, quote.id)
478472
else: # quote was already saved
479473
quote.quote = quote_str
480474
quote.author_id = author.id
@@ -795,12 +789,12 @@ async def get_rating_by_id(quote_id: int, author_id: int) -> int | None:
795789

796790
def get_random_quote_id() -> int:
797791
"""Get random quote id."""
798-
return random.randint(1, MAX_QUOTES_ID.value) # nosec: B311
792+
return random.choice(tuple(QUOTES_CACHE))
799793

800794

801795
def get_random_author_id() -> int:
802796
"""Get random author id."""
803-
return random.randint(1, MAX_AUTHORS_ID.value) # nosec: B311
797+
return random.choice(tuple(AUTHORS_CACHE))
804798

805799

806800
def get_random_id() -> tuple[int, int]:

‎tests/test_quotes.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ async def test_parsing_wrong_quotes() -> None:
7171
assert await quotes.get_rating_by_id(1, 2) == 4
7272

7373
assert len(quotes.QUOTES_CACHE) == 1
74-
assert quotes.MAX_QUOTES_ID.value == 1
7574
assert len(quotes.AUTHORS_CACHE) == 2
76-
assert quotes.MAX_AUTHORS_ID.value == 2
7775

7876

7977
def test_author_updating() -> None:

0 commit comments

Comments
 (0)