COW.py 7.2 KB

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