Coverage for src/blob_dict/dict/in_memory.py: 0%

38 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-07 08:32 -0700

1from collections import UserDict 

2from collections.abc import Iterator 

3from datetime import timedelta 

4from typing import override 

5 

6from ttl_dict import TTLDict 

7 

8from ..blob import BytesBlob 

9from . import BlobDictBase 

10 

11 

12class InMemoryBlobDict(BlobDictBase): 

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

14 

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__() 

22 

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

24 raise ValueError(InMemoryBlobDict.__EXTERNAL_DICT_TTL_ERROR_MESSAGE) 

25 

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 ) 

33 

34 @override 

35 def __len__(self) -> int: 

36 return len(self.__dict) 

37 

38 @override 

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

40 return key in self.__dict 

41 

42 @override 

43 def get(self, key: str, default: BytesBlob | None = None) -> BytesBlob | None: 

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

45 

46 @override 

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

48 yield from ( 

49 key for key in self.__dict 

50 ) 

51 

52 @override 

53 def clear(self) -> None: 

54 self.__dict.clear() 

55 

56 @override 

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

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

59 

60 @override 

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

62 del self.__dict[key] 

63 

64 @override 

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

66 self.__dict[key] = blob