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

1from collections.abc import Iterator, MutableMapping 

2from datetime import timedelta 

3from typing import override 

4 

5from ttl_dict import TTLDict 

6 

7from ..blob import BytesBlob 

8from . import BlobDictBase 

9 

10 

11class InMemoryBlobDict(BlobDictBase): 

12 __EXTERNAL_DICT_TTL_ERROR_MESSAGE: str = "Cannot specify `ttl` for external `data_dict`" 

13 

14 def __init__( 

15 self, 

16 data_dict: MutableMapping[str, BytesBlob] | None = None, 

17 *, 

18 ttl: timedelta | None = None, 

19 ) -> None: 

20 super().__init__() 

21 

22 if data_dict is not None and ttl is not None: 

23 raise ValueError(InMemoryBlobDict.__EXTERNAL_DICT_TTL_ERROR_MESSAGE) 

24 

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 ) 

32 

33 @override 

34 def __len__(self) -> int: 

35 return len(self.__dict) 

36 

37 @override 

38 def __contains__(self, key: str) -> bool: 

39 return key in self.__dict 

40 

41 @override 

42 def get(self, key: str, /, default: BytesBlob | None) -> BytesBlob | None: 

43 return self.__dict.get(key, default) 

44 

45 @override 

46 def __getitem__(self, key: str, /) -> BytesBlob: 

47 return self.__dict[key] 

48 

49 @override 

50 def __iter__(self) -> Iterator[str]: 

51 yield from ( 

52 key for key in self.__dict 

53 ) 

54 

55 @override 

56 def clear(self) -> None: 

57 self.__dict.clear() 

58 

59 @override 

60 def pop(self, key: str, /, default: BytesBlob | None = None) -> BytesBlob | None: 

61 return self.__dict.pop(key, default) 

62 

63 @override 

64 def __delitem__(self, key: str, /) -> None: 

65 del self.__dict[key] 

66 

67 @override 

68 def __setitem__(self, key: str, blob: BytesBlob, /) -> None: 

69 self.__dict[key] = blob