Coverage for src/blob_dict/dict/in_memory.py: 0%
38 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-10 06:26 -0700
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-10 06:26 -0700
1from collections import UserDict
2from collections.abc import Iterator
3from datetime import timedelta
4from typing import override
6from ttl_dict import TTLDict
8from ..blob import BytesBlob
9from . import BlobDictBase
12class InMemoryBlobDict(BlobDictBase):
13 __EXTERNAL_DICT_TTL_ERROR_MESSAGE: str = "Cannot specify `ttl` for external `data_dict`"
15 def __init__(
16 self,
17 data_dict: dict[str, BytesBlob] | UserDict[str, BytesBlob] | None = None,
18 *,
19 ttl: timedelta | None = None,
20 ) -> None:
21 super().__init__()
23 if data_dict is not None and ttl is not None:
24 raise ValueError(InMemoryBlobDict.__EXTERNAL_DICT_TTL_ERROR_MESSAGE)
26 self.__dict: dict[str, BytesBlob] | UserDict[str, BytesBlob] = (
27 (
28 {} if ttl is None
29 else TTLDict[str, BytesBlob](ttl)
30 ) if data_dict is None
31 else data_dict
32 )
34 @override
35 def __len__(self) -> int:
36 return len(self.__dict)
38 @override
39 def __contains__(self, key: str) -> bool:
40 return key in self.__dict
42 @override
43 def get(self, key: str, default: BytesBlob | None = None) -> BytesBlob | None:
44 return self.__dict.get(key, default)
46 @override
47 def __iter__(self) -> Iterator[str]:
48 yield from (
49 key for key in self.__dict
50 )
52 @override
53 def clear(self) -> None:
54 self.__dict.clear()
56 @override
57 def pop(self, key: str, default: BytesBlob | None = None) -> BytesBlob | None:
58 return self.__dict.pop(key, default)
60 @override
61 def __delitem__(self, key: str) -> None:
62 del self.__dict[key]
64 @override
65 def __setitem__(self, key: str, blob: BytesBlob) -> None:
66 self.__dict[key] = blob