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

« back to all changes in this revision

Viewing changes to bson/dbref.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.
14
14
 
15
15
"""Tools for manipulating DBRefs (references to MongoDB documents)."""
16
16
 
 
17
from copy import deepcopy
 
18
 
17
19
from bson.son import SON
18
 
from copy import deepcopy
19
20
 
20
21
 
21
22
class DBRef(object):
26
27
        """Initialize a new :class:`DBRef`.
27
28
 
28
29
        Raises :class:`TypeError` if `collection` or `database` is not
29
 
        an instance of :class:`basestring`. `database` is optional and
30
 
        allows references to documents to work across databases. Any
31
 
        additional keyword arguments will create additional fields in
32
 
        the resultant embedded document.
 
30
        an instance of :class:`basestring` (:class:`str` in python 3).
 
31
        `database` is optional and allows references to documents to work
 
32
        across databases. Any additional keyword arguments will create
 
33
        additional fields in the resultant embedded document.
33
34
 
34
35
        :Parameters:
35
36
          - `collection`: name of the collection the document is stored in
46
47
        .. mongodoc:: dbrefs
47
48
        """
48
49
        if not isinstance(collection, basestring):
49
 
            raise TypeError("collection must be an instance of basestring")
 
50
            raise TypeError("collection must be an "
 
51
                            "instance of %s" % (basestring.__name__,))
50
52
        if database is not None and not isinstance(database, basestring):
51
 
            raise TypeError("database must be an instance of basestring")
 
53
            raise TypeError("database must be an "
 
54
                            "instance of %s" % (basestring.__name__,))
52
55
 
53
56
        self.__collection = collection
54
57
        self.__id = id
110
113
        return "DBRef(%r, %r, %r%s)" % (self.collection, self.id,
111
114
                                        self.database, extra)
112
115
 
113
 
    def __cmp__(self, other):
 
116
    def __eq__(self, other):
114
117
        if isinstance(other, DBRef):
115
 
            return cmp([self.__database, self.__collection,
116
 
                        self.__id, self.__kwargs],
117
 
                       [other.__database, other.__collection,
118
 
                        other.__id, other.__kwargs])
 
118
            us = [self.__database, self.__collection,
 
119
                  self.__id, self.__kwargs]
 
120
            them = [other.__database, other.__collection,
 
121
                    other.__id, other.__kwargs]
 
122
            return us == them
119
123
        return NotImplemented
120
124
 
121
125
    def __hash__(self):