son.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. # Copyright 2009-present MongoDB, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Tools for creating and manipulating SON, the Serialized Ocument Notation.
  15. Regular dictionaries can be used instead of SON objects, but not when the order
  16. of keys is important. A SON object can be used just like a normal Python
  17. dictionary."""
  18. import copy
  19. import re
  20. from bson.py3compat import abc, iteritems
  21. # This sort of sucks, but seems to be as good as it gets...
  22. # This is essentially the same as re._pattern_type
  23. RE_TYPE = type(re.compile(""))
  24. class SON(dict):
  25. """SON data.
  26. A subclass of dict that maintains ordering of keys and provides a
  27. few extra niceties for dealing with SON. SON provides an API
  28. similar to collections.OrderedDict from Python 2.7+.
  29. """
  30. def __init__(self, data=None, **kwargs):
  31. self.__keys = []
  32. dict.__init__(self)
  33. self.update(data)
  34. self.update(kwargs)
  35. def __new__(cls, *args, **kwargs):
  36. instance = super(SON, cls).__new__(cls, *args, **kwargs)
  37. instance.__keys = []
  38. return instance
  39. def __repr__(self):
  40. result = []
  41. for key in self.__keys:
  42. result.append("(%r, %r)" % (key, self[key]))
  43. return "SON([%s])" % ", ".join(result)
  44. def __setitem__(self, key, value):
  45. if key not in self.__keys:
  46. self.__keys.append(key)
  47. dict.__setitem__(self, key, value)
  48. def __delitem__(self, key):
  49. self.__keys.remove(key)
  50. dict.__delitem__(self, key)
  51. def keys(self):
  52. return list(self.__keys)
  53. def copy(self):
  54. other = SON()
  55. other.update(self)
  56. return other
  57. # TODO this is all from UserDict.DictMixin. it could probably be made more
  58. # efficient.
  59. # second level definitions support higher levels
  60. def __iter__(self):
  61. for k in self.__keys:
  62. yield k
  63. def has_key(self, key):
  64. return key in self.__keys
  65. # third level takes advantage of second level definitions
  66. def iteritems(self):
  67. for k in self:
  68. yield (k, self[k])
  69. def iterkeys(self):
  70. return self.__iter__()
  71. # fourth level uses definitions from lower levels
  72. def itervalues(self):
  73. for _, v in self.iteritems():
  74. yield v
  75. def values(self):
  76. return [v for _, v in self.iteritems()]
  77. def items(self):
  78. return [(key, self[key]) for key in self]
  79. def clear(self):
  80. self.__keys = []
  81. super(SON, self).clear()
  82. def setdefault(self, key, default=None):
  83. try:
  84. return self[key]
  85. except KeyError:
  86. self[key] = default
  87. return default
  88. def pop(self, key, *args):
  89. if len(args) > 1:
  90. raise TypeError("pop expected at most 2 arguments, got "\
  91. + repr(1 + len(args)))
  92. try:
  93. value = self[key]
  94. except KeyError:
  95. if args:
  96. return args[0]
  97. raise
  98. del self[key]
  99. return value
  100. def popitem(self):
  101. try:
  102. k, v = next(self.iteritems())
  103. except StopIteration:
  104. raise KeyError('container is empty')
  105. del self[k]
  106. return (k, v)
  107. def update(self, other=None, **kwargs):
  108. # Make progressively weaker assumptions about "other"
  109. if other is None:
  110. pass
  111. elif hasattr(other, 'iteritems'): # iteritems saves memory and lookups
  112. for k, v in other.iteritems():
  113. self[k] = v
  114. elif hasattr(other, 'keys'):
  115. for k in other.keys():
  116. self[k] = other[k]
  117. else:
  118. for k, v in other:
  119. self[k] = v
  120. if kwargs:
  121. self.update(kwargs)
  122. def get(self, key, default=None):
  123. try:
  124. return self[key]
  125. except KeyError:
  126. return default
  127. def __eq__(self, other):
  128. """Comparison to another SON is order-sensitive while comparison to a
  129. regular dictionary is order-insensitive.
  130. """
  131. if isinstance(other, SON):
  132. return len(self) == len(other) and self.items() == other.items()
  133. return self.to_dict() == other
  134. def __ne__(self, other):
  135. return not self == other
  136. def __len__(self):
  137. return len(self.__keys)
  138. def to_dict(self):
  139. """Convert a SON document to a normal Python dictionary instance.
  140. This is trickier than just *dict(...)* because it needs to be
  141. recursive.
  142. """
  143. def transform_value(value):
  144. if isinstance(value, list):
  145. return [transform_value(v) for v in value]
  146. elif isinstance(value, abc.Mapping):
  147. return dict([
  148. (k, transform_value(v))
  149. for k, v in iteritems(value)])
  150. else:
  151. return value
  152. return transform_value(dict(self))
  153. def __deepcopy__(self, memo):
  154. out = SON()
  155. val_id = id(self)
  156. if val_id in memo:
  157. return memo.get(val_id)
  158. memo[val_id] = out
  159. for k, v in self.iteritems():
  160. if not isinstance(v, RE_TYPE):
  161. v = copy.deepcopy(v, memo)
  162. out[k] = v
  163. return out