~crunch.io/ubuntu/precise/pymongo/unstable

« back to all changes in this revision

Viewing changes to bson/son.py

  • Committer: Joseph Tate
  • Date: 2013-01-31 08:00:57 UTC
  • mfrom: (1.1.12)
  • Revision ID: jtate@dragonstrider.com-20130131080057-y7lv17xi6x8c1j5x
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
dictionary."""
20
20
 
21
21
import copy
 
22
import re
 
23
 
 
24
# This sort of sucks, but seems to be as good as it gets...
 
25
RE_TYPE = type(re.compile(""))
22
26
 
23
27
 
24
28
class SON(dict):
193
197
            return default
194
198
 
195
199
    def __eq__(self, other):
 
200
        """Comparison to another SON is order-sensitive while comparison to a
 
201
        regular dictionary is order-insensitive.
 
202
        """
196
203
        if isinstance(other, SON):
197
 
            return (len(self) == len(other) and
198
 
                    dict(self.items()) == dict(other.items()))
199
 
        return dict(self.items()) == other
 
204
            return len(self) == len(other) and self.items() == other.items()
 
205
        return self.to_dict() == other
 
206
 
 
207
    def __ne__(self, other):
 
208
        return not self == other
200
209
 
201
210
    def __len__(self):
202
211
        return len(self.keys())
222
231
 
223
232
    def __deepcopy__(self, memo):
224
233
        out = SON()
 
234
        val_id = id(self)
 
235
        if val_id in memo:
 
236
            return memo.get(val_id)
 
237
        memo[val_id] = out
225
238
        for k, v in self.iteritems():
226
 
            out[k] = copy.deepcopy(v, memo)
 
239
            if not isinstance(v, RE_TYPE):
 
240
                v = copy.deepcopy(v, memo)
 
241
            out[k] = v
227
242
        return out