~ubuntu-branches/ubuntu/wily/pymongo/wily

« back to all changes in this revision

Viewing changes to bson/son.py

  • Committer: Package Import Robot
  • Author(s): Federico Ceratto
  • Date: 2012-05-10 21:21:40 UTC
  • mfrom: (1.1.11)
  • Revision ID: package-import@ubuntu.com-20120510212140-9c66c00zz850h6l9
Tags: 2.2-1
* New upstream release.
* Dependencies added (Closes: #670268)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2009-2010 10gen, Inc.
 
1
# Copyright 2009-2012 10gen, Inc.
2
2
#
3
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
4
# you may not use this file except in compliance with the License.
51
51
    unicode                              code           bson -> py
52
52
    `bson.code.Code`                     code           py -> bson
53
53
    unicode                              symbol         bson -> py
 
54
    bytes (Python 3) [#bytes]_           binary         both
54
55
    ===================================  =============  ===================
55
56
 
56
57
    Note that to save binary data it must be wrapped as an instance of
58
59
    and retrieved as unicode.
59
60
 
60
61
    .. [#int] A Python int will be saved as a BSON int32 or BSON int64 depending
61
 
       on its size. A BSON int32 will always decode to a Python int. A BSON int64
62
 
       will always decode to a Python long.
 
62
       on its size. A BSON int32 will always decode to a Python int. In Python 2.x
 
63
       a BSON int64 will always decode to a Python long. In Python 3.x a BSON
 
64
       int64 will decode to a Python int since there is no longer a long type.
63
65
    .. [#dt] datetime.datetime instances will be rounded to the nearest
64
66
       millisecond when saved
65
67
    .. [#dt2] all datetime.datetime instances are treated as *naive*. clients
66
68
       should always use UTC.
 
69
    .. [#bytes] The bytes type from Python 3.x is encoded as BSON binary with
 
70
       subtype 0. In Python 3.x it will be decoded back to bytes. In Python 2.x
 
71
       it will be decoded to an instance of :class:`~bson.binary.Binary` with
 
72
       subtype 0.
67
73
    """
68
74
 
69
75
    def __init__(self, data=None, **kwargs):
72
78
        self.update(data)
73
79
        self.update(kwargs)
74
80
 
 
81
    def __new__(cls, *args, **kwargs):
 
82
        instance = super(SON, cls).__new__(cls, *args, **kwargs)
 
83
        instance.__keys = []
 
84
        return instance
 
85
 
75
86
    def __repr__(self):
76
87
        result = []
77
88
        for key in self.__keys:
125
136
        return [v for _, v in self.iteritems()]
126
137
 
127
138
    def items(self):
128
 
        return list(self.iteritems())
 
139
        return [(key, self[key]) for key in self]
129
140
 
130
141
    def clear(self):
131
142
        for key in self.keys():
181
192
        except KeyError:
182
193
            return default
183
194
 
184
 
    def __cmp__(self, other):
 
195
    def __eq__(self, other):
185
196
        if isinstance(other, SON):
186
 
            return cmp((dict(self.iteritems()), self.keys()),
187
 
                       (dict(other.iteritems()), other.keys()))
188
 
        return cmp(dict(self.iteritems()), other)
 
197
            return (len(self) == len(other) and
 
198
                    dict(self.items()) == dict(other.items()))
 
199
        return dict(self.items()) == other
189
200
 
190
201
    def __len__(self):
191
202
        return len(self.keys())