Coverage for src/blob_dict/dict/in_memory.py: 0%
40 statements
« prev ^ index » next coverage.py v7.8.1, created at 2025-05-21 20:53 -0700
« prev ^ index » next coverage.py v7.8.1, created at 2025-05-21 20:53 -0700
1from collections.abc import Iterator, MutableMapping
2from datetime import timedelta
3from typing import override
5from ttl_dict import TTLDict
7from ..blob import BytesBlob
8from . import BlobDictBase
11class InMemoryBlobDict(BlobDictBase):
12 __EXTERNAL_DICT_TTL_ERROR_MESSAGE: str = "Cannot specify `ttl` for external `data_dict`"
14 def __init__(
15 self,
16 data_dict: MutableMapping[str, BytesBlob] | None = None,
17 *,
18 ttl: timedelta | None = None,
19 ) -> None:
20 super().__init__()
22 if data_dict is not None and ttl is not None:
23 raise ValueError(InMemoryBlobDict.__EXTERNAL_DICT_TTL_ERROR_MESSAGE)
25 self.__dict: MutableMapping[str, BytesBlob] = (
26 (
27 {} if ttl is None
28 else TTLDict[str, BytesBlob](ttl)
29 ) if data_dict is None
30 else data_dict
31 )
33 @override
34 def __len__(self) -> int:
35 return len(self.__dict)
37 @override
38 def __contains__(self, key: str) -> bool:
39 return key in self.__dict
41 @override
42 def get(self, key: str, /, default: BytesBlob | None) -> BytesBlob | None:
43 return self.__dict.get(key, default)
45 @override
46 def __getitem__(self, key: str, /) -> BytesBlob:
47 return self.__dict[key]
49 @override
50 def __iter__(self) -> Iterator[str]:
51 yield from (
52 key for key in self.__dict
53 )
55 @override
56 def clear(self) -> None:
57 self.__dict.clear()
59 @override
60 def pop(self, key: str, /, default: BytesBlob | None = None) -> BytesBlob | None:
61 return self.__dict.pop(key, default)
63 @override
64 def __delitem__(self, key: str, /) -> None:
65 del self.__dict[key]
67 @override
68 def __setitem__(self, key: str, blob: BytesBlob, /) -> None:
69 self.__dict[key] = blob