Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. import weakref
  2. import gc
  3.  
  4. builtin_types = (bool, float, int, str)
  5.  
  6. class MemoryStuff():
  7. id = 0
  8.  
  9. def __init__(self):
  10. MemoryStuff.id += 1
  11. self.id = MemoryStuff.id
  12.  
  13. def __repr__(self):
  14. return "{Memory#" + str(self.id) + "}"
  15.  
  16.  
  17. class WeakDictBase:
  18. primdict = {}
  19.  
  20. def keys(self):
  21. return self.objdict.keys() + self.primdict.keys()
  22.  
  23. def __len__(self):
  24. return len(self.objdict) + len(self.primdict)
  25.  
  26.  
  27. class WeakKeyDict(WeakDictBase):
  28. objdict = weakref.WeakKeyDictionary()
  29.  
  30. def __getitem__(self, key):
  31. dict = self.primdict if type(key) in builtin_types else self.objdict
  32.  
  33. return dict[key] if key in dict else "Not found"
  34.  
  35. def __setitem__(self, key, val):
  36. if type(key) in builtin_types:
  37. self.primdict[key] = val
  38. else:
  39. self.objdict[key] = val
  40.  
  41. class WeakValueDict(WeakDictBase):
  42. objdict = weakref.WeakValueDictionary()
  43.  
  44. def __getitem__(self, key):
  45. if key in self.primdict:
  46. return self.primdict[key]
  47. elif key in self.objdict:
  48. return self.objdict[key]
  49.  
  50. return "Not found"
  51.  
  52. def __setitem__(self, key, val):
  53. if type(val) in builtin_types:
  54. if key in self.objdict:
  55. del self.objdict[key]
  56.  
  57. self.primdict[key] = val
  58. else:
  59. if key in self.primdict:
  60. del self.primdict[key]
  61.  
  62. self.objdict[key] = val
  63.  
  64. class WeakKeyValueDict(dict):
  65.  
  66. def __getitem__(self, key):
  67. val = dict.__getitem__(self, key)
  68. return val() if isinstance(val, weakref.ref) else val
  69.  
  70. def __setitem__(self, key, val):
  71. k = key if type(key) in builtin_types else weakref.ref(key)
  72. v = val if type(val) in builtin_types else weakref.ref(val)
  73.  
  74. return dict.__setitem__(self, k, v)
  75.  
  76. def keys(self):
  77. return filter(lambda key: not isinstance(key, weakref.ref) or key() is not None, dict.keys(self))
  78.  
  79. print("--- Weak Keys ---")
  80.  
  81. a = WeakKeyDict()
  82. a[10] = 100
  83.  
  84. x = MemoryStuff()
  85. a[x] = 777
  86.  
  87. print("keys: ")
  88. print(a.keys())
  89. print(len(a))
  90. print(a[x])
  91.  
  92. x = 555
  93.  
  94. print("-- gc --")
  95. gc.collect()
  96.  
  97. print("keys: ")
  98. print(a.keys())
  99. print(len(a))
  100. print(a[x])
  101.  
  102.  
  103. print("--- Weak Values ---")
  104.  
  105. a = WeakValueDict()
  106. a[10] = 100
  107.  
  108. x = MemoryStuff()
  109. a[x] = 777
  110.  
  111. y = MemoryStuff()
  112. a[10] = y
  113.  
  114. print("keys: ")
  115. print(a.keys())
  116. print(len(a))
  117. print(a[x])
  118.  
  119. y = 20
  120.  
  121. print("-- gc --")
  122. gc.collect()
  123.  
  124. print("keys: ")
  125. print(a.keys())
  126. print(len(a))
  127. print(a[x])
  128.  
  129.  
  130. print("--- Weak Keys & Values ---")
  131.  
  132. a = WeakKeyValueDict()
  133. a[10] = 100
  134. obj = MemoryStuff()
  135. stuff = MemoryStuff()
  136. a[obj] = 100
  137. a[100] = obj
  138. a[stuff] = obj
  139.  
  140. print("keys: ")
  141. print(a.keys())
  142. print(len(a))
  143. print(a[10])
  144. print(a[100])
  145.  
  146. obj = 10
  147. stuff = 20
  148. gc.collect()
  149.  
  150. print("keys: ")
  151. print(a.keys())
  152. print(len(a))
  153. print(a[10])
  154. print(a[100])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement