COW.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #
  2. # This is a copy on write dictionary and set which abuses classes to try and be nice and fast.
  3. #
  4. # Copyright (C) 2006 Tim Ansell
  5. #
  6. #Please Note:
  7. # Be careful when using mutable types (ie Dict and Lists) - operations involving these are SLOW.
  8. # Assign a file to __warn__ to get warnings about slow operations.
  9. #
  10. import copy
  11. import types
  12. ImmutableTypes = (
  13. bool,
  14. complex,
  15. float,
  16. int,
  17. tuple,
  18. frozenset,
  19. str
  20. )
  21. MUTABLE = "__mutable__"
  22. class COWMeta(type):
  23. pass
  24. class COWDictMeta(COWMeta):
  25. __warn__ = False
  26. __hasmutable__ = False
  27. __marker__ = tuple()
  28. def __str__(cls):
  29. # FIXME: I have magic numbers!
  30. return "<COWDict Level: %i Current Keys: %i>" % (cls.__count__, len(cls.__dict__) - 3)
  31. __repr__ = __str__
  32. def cow(cls):
  33. class C(cls):
  34. __count__ = cls.__count__ + 1
  35. return C
  36. copy = cow
  37. __call__ = cow
  38. def __setitem__(cls, key, value):
  39. if value is not None and not isinstance(value, ImmutableTypes):
  40. if not isinstance(value, COWMeta):
  41. cls.__hasmutable__ = True
  42. key += MUTABLE
  43. setattr(cls, key, value)
  44. def __getmutable__(cls, key, readonly=False):
  45. nkey = key + MUTABLE
  46. try:
  47. return cls.__dict__[nkey]
  48. except KeyError:
  49. pass
  50. value = getattr(cls, nkey)
  51. if readonly:
  52. return value
  53. if not cls.__warn__ is False and not isinstance(value, COWMeta):
  54. print("Warning: Doing a copy because %s is a mutable type." % key, file=cls.__warn__)
  55. try:
  56. value = value.copy()
  57. except AttributeError as e:
  58. value = copy.copy(value)
  59. setattr(cls, nkey, value)
  60. return value
  61. __getmarker__ = []
  62. def __getreadonly__(cls, key, default=__getmarker__):
  63. """\
  64. Get a value (even if mutable) which you promise not to change.
  65. """
  66. return cls.__getitem__(key, default, True)
  67. def __getitem__(cls, key, default=__getmarker__, readonly=False):
  68. try:
  69. try:
  70. value = getattr(cls, key)
  71. except AttributeError:
  72. value = cls.__getmutable__(key, readonly)
  73. # This is for values which have been deleted
  74. if value is cls.__marker__:
  75. raise AttributeError("key %s does not exist." % key)
  76. return value
  77. except AttributeError as e:
  78. if not default is cls.__getmarker__:
  79. return default
  80. raise KeyError(str(e))
  81. def __delitem__(cls, key):
  82. cls.__setitem__(key, cls.__marker__)
  83. def __revertitem__(cls, key):
  84. if key not in cls.__dict__:
  85. key += MUTABLE
  86. delattr(cls, key)
  87. def __contains__(cls, key):
  88. return cls.has_key(key)
  89. def has_key(cls, key):
  90. value = cls.__getreadonly__(key, cls.__marker__)
  91. if value is cls.__marker__:
  92. return False
  93. return True
  94. def iter(cls, type, readonly=False):
  95. for key in dir(cls):
  96. if key.startswith("__"):
  97. continue
  98. if key.endswith(MUTABLE):
  99. key = key[:-len(MUTABLE)]
  100. if type == "keys":
  101. yield key
  102. try:
  103. if readonly:
  104. value = cls.__getreadonly__(key)
  105. else:
  106. value = cls[key]
  107. except KeyError:
  108. continue
  109. if type == "values":
  110. yield value
  111. if type == "items":
  112. yield (key, value)
  113. return
  114. def iterkeys(cls):
  115. return cls.iter("keys")
  116. def itervalues(cls, readonly=False):
  117. if not cls.__warn__ is False and cls.__hasmutable__ and readonly is False:
  118. print("Warning: If you arn't going to change any of the values call with True.", file=cls.__warn__)
  119. return cls.iter("values", readonly)
  120. def iteritems(cls, readonly=False):
  121. if not cls.__warn__ is False and cls.__hasmutable__ and readonly is False:
  122. print("Warning: If you arn't going to change any of the values call with True.", file=cls.__warn__)
  123. return cls.iter("items", readonly)
  124. class COWSetMeta(COWDictMeta):
  125. def __str__(cls):
  126. # FIXME: I have magic numbers!
  127. return "<COWSet Level: %i Current Keys: %i>" % (cls.__count__, len(cls.__dict__) -3)
  128. __repr__ = __str__
  129. def cow(cls):
  130. class C(cls):
  131. __count__ = cls.__count__ + 1
  132. return C
  133. def add(cls, value):
  134. COWDictMeta.__setitem__(cls, repr(hash(value)), value)
  135. def remove(cls, value):
  136. COWDictMeta.__delitem__(cls, repr(hash(value)))
  137. def __in__(cls, value):
  138. return repr(hash(value)) in COWDictMeta
  139. def iterkeys(cls):
  140. raise TypeError("sets don't have keys")
  141. def iteritems(cls):
  142. raise TypeError("sets don't have 'items'")
  143. # These are the actual classes you use!
  144. class COWDictBase(object, metaclass = COWDictMeta):
  145. __count__ = 0
  146. class COWSetBase(object, metaclass = COWSetMeta):
  147. __count__ = 0
  148. if __name__ == "__main__":
  149. import sys
  150. COWDictBase.__warn__ = sys.stderr
  151. a = COWDictBase()
  152. print("a", a)
  153. a['a'] = 'a'
  154. a['b'] = 'b'
  155. a['dict'] = {}
  156. b = a.copy()
  157. print("b", b)
  158. b['c'] = 'b'
  159. print()
  160. print("a", a)
  161. for x in a.iteritems():
  162. print(x)
  163. print("--")
  164. print("b", b)
  165. for x in b.iteritems():
  166. print(x)
  167. print()
  168. b['dict']['a'] = 'b'
  169. b['a'] = 'c'
  170. print("a", a)
  171. for x in a.iteritems():
  172. print(x)
  173. print("--")
  174. print("b", b)
  175. for x in b.iteritems():
  176. print(x)
  177. print()
  178. try:
  179. b['dict2']
  180. except KeyError as e:
  181. print("Okay!")
  182. a['set'] = COWSetBase()
  183. a['set'].add("o1")
  184. a['set'].add("o1")
  185. a['set'].add("o2")
  186. print("a", a)
  187. for x in a['set'].itervalues():
  188. print(x)
  189. print("--")
  190. print("b", b)
  191. for x in b['set'].itervalues():
  192. print(x)
  193. print()
  194. b['set'].add('o3')
  195. print("a", a)
  196. for x in a['set'].itervalues():
  197. print(x)
  198. print("--")
  199. print("b", b)
  200. for x in b['set'].itervalues():
  201. print(x)
  202. print()
  203. a['set2'] = set()
  204. a['set2'].add("o1")
  205. a['set2'].add("o1")
  206. a['set2'].add("o2")
  207. print("a", a)
  208. for x in a.iteritems():
  209. print(x)
  210. print("--")
  211. print("b", b)
  212. for x in b.iteritems(readonly=True):
  213. print(x)
  214. print()
  215. del b['b']
  216. try:
  217. print(b['b'])
  218. except KeyError:
  219. print("Yay! deleted key raises error")
  220. if 'b' in b:
  221. print("Boo!")
  222. else:
  223. print("Yay - has_key with delete works!")
  224. print("a", a)
  225. for x in a.iteritems():
  226. print(x)
  227. print("--")
  228. print("b", b)
  229. for x in b.iteritems(readonly=True):
  230. print(x)
  231. print()
  232. b.__revertitem__('b')
  233. print("a", a)
  234. for x in a.iteritems():
  235. print(x)
  236. print("--")
  237. print("b", b)
  238. for x in b.iteritems(readonly=True):
  239. print(x)
  240. print()
  241. b.__revertitem__('dict')
  242. print("a", a)
  243. for x in a.iteritems():
  244. print(x)
  245. print("--")
  246. print("b", b)
  247. for x in b.iteritems(readonly=True):
  248. print(x)
  249. print()