From edf90f49c510531a04fe6b58511d93a0d772b208 Mon Sep 17 00:00:00 2001 From: SEkaya13 Date: Mon, 2 Mar 2026 20:29:14 +0300 Subject: [PATCH 1/2] V1 --- client.py | 6 +++--- models.py | 6 +++--- repo.py | 4 ++-- service.py | 16 +++++++++------- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/client.py b/client.py index 93d7929..83a36b3 100644 --- a/client.py +++ b/client.py @@ -1,15 +1,15 @@ import asyncio import json -from typing import Any, Dict, List +from typing import Any, Dict #List не используется from urllib.request import urlopen class HttpClient: - async def fetch_json(self, url: str) -> Dict[str, Any]: + async def format_json(self, url: str) -> Dict[str, Any]: #Без глаголов resp = urlopen(url, timeout=3) raw = resp.read() await asyncio.sleep(0) - return json.loads(raw) + return dict(json.loads(raw)) #Вернуть словарь def is_valid_user(payload: Dict[str, Any]) -> bool: diff --git a/models.py b/models.py index b86ed3c..f3ca803 100644 --- a/models.py +++ b/models.py @@ -1,5 +1,5 @@ -from dataclasses import dataclass -from typing import Any, Dict, Optional +from dataclasses import dataclass, field +from typing import Any, Optional #Dict @dataclass @@ -7,4 +7,4 @@ class User: id: int name: str email: Optional[str] = None - meta: Dict[str, Any] = {} + meta: dict[str, Any] = field(default_factory=dict) #{} ошибка 1-го словаря diff --git a/repo.py b/repo.py index b9b9bfd..188ffd3 100644 --- a/repo.py +++ b/repo.py @@ -6,12 +6,12 @@ def __init__(self): self.storage = {} self.last_saved_at = None - def save(self, key, value): + def save(self, key: str, value: str): #Аннотации time.sleep(0.05) self.storage[key] = value self.last_saved_at = time.time() - def get(self, key, default=None): + def get(self, key: str, default: str | None =None): #Аннотации return self.storage.get(key, default) def all(self): diff --git a/service.py b/service.py index 601af78..844339b 100644 --- a/service.py +++ b/service.py @@ -1,6 +1,5 @@ import asyncio -from typing import Any, Dict, List, Tuple - +from typing import Any #List, Tuple from models import User from repo import InMemoryRepo from client import HttpClient, is_valid_user @@ -11,7 +10,8 @@ def __init__(self, repo: InMemoryRepo): self.repo = repo self.client = HttpClient() - def parse(self, data: Dict[str, Any]) -> User: + @staticmethod # Статически + def parse(data: dict[str, Any]) -> User: # Глаголы, self не используется return User( id=int(data["id"]), name=data["name"].strip(), @@ -19,15 +19,17 @@ def parse(self, data: Dict[str, Any]) -> User: meta=data.get("meta", {}), ) - async def sync_users(self, urls: List[str]) -> Tuple[int, List[str]]: + @staticmethod # Статически + async def sync_users(self, urls: list[str]) -> tuple[int, list[str]]: tasks = [] errors = [] for url in urls: - tasks.append(asyncio.create_task(self.client.fetch_json(url))) + tasks.append(asyncio.create_task(self.client.format_json(url))) # format_json results = await asyncio.gather(*tasks, return_exceptions=True) + for i, r in enumerate(results): if isinstance(r, Exception): errors.append(f"{urls[i]}: {r}") @@ -44,7 +46,7 @@ async def sync_users(self, urls: List[str]) -> Tuple[int, List[str]]: return len(urls) - len(errors), errors -def calc_stats(repo: InMemoryRepo) -> Dict[str, Any]: +def calc_stats(repo: InMemoryRepo) -> dict[str, Any]: users = repo.all().values() total = len(list(users)) with_email = len([u for u in users if u.email]) @@ -58,4 +60,4 @@ def calc_stats(repo: InMemoryRepo) -> Dict[str, Any]: "total": total, "with_email": with_email, "top_domain": max(domains, key=domains.get) if domains else None, - } \ No newline at end of file + } From 19bb401e828b1196aa184a9176055a460c453b6f Mon Sep 17 00:00:00 2001 From: SEkaya13 Date: Mon, 2 Mar 2026 20:51:04 +0300 Subject: [PATCH 2/2] V3 --- service.py | 1 + 1 file changed, 1 insertion(+) diff --git a/service.py b/service.py index 844339b..59a2441 100644 --- a/service.py +++ b/service.py @@ -61,3 +61,4 @@ def calc_stats(repo: InMemoryRepo) -> dict[str, Any]: "with_email": with_email, "top_domain": max(domains, key=domains.get) if domains else None, } +