Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tables/misc/proxydict.py: 40%

30 statements  

« prev     ^ index     » next       coverage.py v7.2.5, created at 2023-05-10 06:15 +0000

1"""Proxy dictionary for objects stored in a container.""" 

2import weakref 

3 

4 

5class ProxyDict(dict): 

6 """A dictionary which uses a container object to store its values.""" 

7 

8 def __init__(self, container): 

9 self.containerref = weakref.ref(container) 

10 """A weak reference to the container object. 

11 

12 .. versionchanged:: 3.0 

13 The *containerRef* attribute has been renamed into 

14 *containerref*. 

15 

16 """ 

17 

18 def __getitem__(self, key): 

19 if key not in self: 

20 raise KeyError(key) 

21 

22 # Values are not actually stored to avoid extra references. 

23 return self._get_value_from_container(self._get_container(), key) 

24 

25 def __setitem__(self, key, value): 

26 # Values are not actually stored to avoid extra references. 

27 super().__setitem__(key, None) 

28 

29 def __repr__(self): 

30 return object.__repr__(self) 

31 

32 def __str__(self): 

33 # C implementation does not use `self.__getitem__()`. :( 

34 return '{' + ", ".join("{k!r}: {v!r}" for k, v in self.items()) + '}' 

35 

36 def values(self): 

37 # C implementation does not use `self.__getitem__()`. :( 

38 return [self[key] for key in self.keys()] 

39 

40 def itervalues(self): 

41 # C implementation does not use `self.__getitem__()`. :( 

42 for key in self.keys(): 

43 yield self[key] 

44 

45 def items(self): 

46 # C implementation does not use `self.__getitem__()`. :( 

47 return [(key, self[key]) for key in self.keys()] 

48 

49 def iteritems(self): 

50 # C implementation does not use `self.__getitem__()`. :( 

51 for key in self.keys(): 

52 yield (key, self[key]) 

53 

54 def _get_container(self): 

55 container = self.containerref() 

56 if container is None: 

57 raise ValueError("the container object does no longer exist") 

58 return container