Fix default dict creating unnecessary keys

This commit is contained in:
Timmy Welch 2025-01-10 16:25:10 -08:00
parent e8e21eb1b6
commit 8de35bdfa1
3 changed files with 29 additions and 13 deletions

View File

@ -1,12 +1,11 @@
from __future__ import annotations
import dataclasses
from collections import defaultdict
from collections.abc import Collection
from enum import auto
from typing import Any
from comicapi.utils import StrEnum, norm_fold
from comicapi.utils import DefaultDict, StrEnum, norm_fold
@dataclasses.dataclass
@ -55,19 +54,19 @@ def overlay(old: Any, new: Any) -> Any:
return new
attribute = defaultdict(
lambda: overlay,
attribute = DefaultDict(
{
Mode.OVERLAY: overlay,
Mode.ADD_MISSING: lambda old, new: overlay(new, old),
},
default=lambda x: overlay,
)
lists = defaultdict(
lambda: overlay,
lists = DefaultDict(
{
Mode.OVERLAY: merge_lists,
Mode.ADD_MISSING: lambda old, new: merge_lists(new, old),
},
default=lambda x: overlay,
)

View File

@ -22,11 +22,10 @@ import pathlib
import platform
import sys
import unicodedata
from collections import defaultdict
from collections.abc import Iterable, Mapping
from enum import Enum, auto
from shutil import which # noqa: F401
from typing import Any, TypeVar, cast
from typing import Any, Callable, TypeVar, cast
from comicfn2dict import comicfn2dict
@ -107,6 +106,17 @@ else:
logger = logging.getLogger(__name__)
class DefaultDict(dict):
def __init__(self, *args, default: Callable[[Any], Any] | None = None) -> None:
super().__init__(*args)
self.default = default
def __missing__(self, key: Any) -> Any:
if self.default is None:
return key
return self.default(key)
class Parser(StrEnum):
ORIGINAL = auto()
COMPLICATED = auto()
@ -360,7 +370,9 @@ def xlate_float(data: Any) -> float | None:
if isinstance(data, (int, float)):
i = data
else:
i = str(data).translate(defaultdict(lambda: None, zip((ord(c) for c in "1234567890."), "1234567890.")))
i = str(data).translate(
DefaultDict(zip((ord(c) for c in "1234567890."), "1234567890."), default=lambda x: None)
)
if i == "":
return None
try:
@ -493,9 +505,9 @@ def parse_version(s: str) -> tuple[int, int, int]:
return (parts[0], parts[1], parts[2])
_languages: dict[str | None, str | None] = defaultdict(lambda: None)
_languages: dict[str | None, str | None] = DefaultDict(default=lambda x: None)
_countries: dict[str | None, str | None] = defaultdict(lambda: None)
_countries: dict[str | None, str | None] = DefaultDict(default=lambda x: None)
def countries() -> dict[str | None, str | None]:
@ -517,6 +529,8 @@ def languages() -> dict[str | None, str | None]:
def get_language_from_iso(iso: str | None) -> str | None:
if not _languages:
return languages()[iso]
return _languages[iso]
@ -529,10 +543,12 @@ def get_language_iso(string: str | None) -> str | None:
lang = string.casefold()
found = None
for lng in isocodes.extendend_languages.items:
for x in ("alpha_2", "alpha_3", "bibliographic", "common_name", "name"):
if x in lng and lng[x].casefold() == lang:
found = lng
# break
if found:
break
@ -542,6 +558,8 @@ def get_language_iso(string: str | None) -> str | None:
def get_country_from_iso(iso: str | None) -> str | None:
if not _countries:
return countries()[iso]
return _countries[iso]

View File

@ -22,7 +22,6 @@ import json
import logging
import pathlib
import time
from collections import defaultdict
from typing import Any, Callable, Generic, TypeVar, cast
from urllib.parse import parse_qsl, urljoin
@ -186,7 +185,7 @@ class ComicVineTalker(ComicTalker):
self.default_api_url = self.api_url = f"{self.website}/api/"
self.default_api_key = self.api_key = "27431e6787042105bd3e47e169a624521f89f3a4"
self.use_series_start_as_volume: bool = False
self.total_requests_made: dict[str, int] = defaultdict(int)
self.total_requests_made: dict[str, int] = utils.DefaultDict(default=lambda x: 0)
self.custom_url_parameters: dict[str, str] = {}
def _log_total_requests(self) -> None: