2222import datetime
2323import calendar
2424
25- from binary import Binary
26- from code import Code
27- from objectid import ObjectId
28- from dbref import DBRef
29- from son import SON
30- from errors import InvalidBSON , InvalidDocument
31- from errors import InvalidName , InvalidStringData
25+ from pymongo . binary import Binary
26+ from pymongo . code import Code
27+ from pymongo . objectid import ObjectId
28+ from pymongo . dbref import DBRef
29+ from pymongo . son import SON
30+ from pymongo . errors import InvalidBSON , InvalidDocument
31+ from pymongo . errors import InvalidName , InvalidStringData
3232
3333try :
3434 import _cbson
@@ -243,7 +243,8 @@ def _get_string(data):
243243def _get_object (data ):
244244 (object , data ) = _bson_to_dict (data )
245245 if "$ref" in object :
246- return (DBRef (object ["$ref" ], object ["$id" ], object .get ("$db" , None )), data )
246+ return (DBRef (object ["$ref" ], object ["$id" ],
247+ object .get ("$db" , None )), data )
247248 return (object , data )
248249
249250
@@ -381,7 +382,8 @@ def _bson_to_dict(data):
381382
382383def _element_to_bson (key , value , check_keys ):
383384 if not isinstance (key , (str , unicode )):
384- raise InvalidDocument ("documents must have only string keys, key was %r" % key )
385+ raise InvalidDocument ("documents must have only string keys, "
386+ "key was %r" % key )
385387
386388 if check_keys :
387389 if key .startswith ("$" ):
@@ -461,7 +463,8 @@ def _element_to_bson(key, value, check_keys):
461463 flags += "u"
462464 if value .flags & re .VERBOSE :
463465 flags += "x"
464- return "\x0B " + name + _make_c_string (pattern , True ) + _make_c_string (flags )
466+ return "\x0B " + name + _make_c_string (pattern , True ) + \
467+ _make_c_string (flags )
465468 if isinstance (value , DBRef ):
466469 return _element_to_bson (key , value .as_doc (), False )
467470
@@ -512,10 +515,11 @@ def _to_dict(data):
512515
513516
514517def is_valid (bson ):
515- """Validate that the given string represents valid BSON data.
518+ """Check that the given string represents valid :class:` BSON` data.
516519
517- Raises TypeError if the data is not an instance of a subclass of str.
518- Returns True if the data represents a valid BSON object, False otherwise.
520+ Raises :class:`TypeError` if `bson` is not an instance of
521+ :class:`str`. Returns ``True`` if `bson` is valid :class:`BSON`,
522+ ``False`` otherwise.
519523
520524 :Parameters:
521525 - `bson`: the data to be validated
@@ -541,28 +545,34 @@ class BSON(str):
541545 """
542546
543547 def __new__ (cls , bson ):
544- """Initialize a new BSON object with some data.
548+ """Initialize a new :class:` BSON` instance with some data.
545549
546- Raises TypeError if `bson` is not an instance of str.
550+ Raises :class:`TypeError` if `bson` is not an instance of
551+ :class:`str`.
547552
548553 :Parameters:
549554 - `bson`: the initial data
550555 """
551556 return str .__new__ (cls , bson )
552557
553- def from_dict (cls , dict , check_keys = False , move_id = False ):
554- """Create a new BSON object from a python mapping type (like dict).
558+ def from_dict (cls , dct , check_keys = False , move_id = False ):
559+ """Create a new :class:`BSON` instance from a mapping type
560+ (like :class:`dict`).
555561
556- Raises TypeError if the argument is not a mapping type, or contains
557- keys that are not instance of (str, unicode). Raises InvalidDocument
558- if the dictionary cannot be converted to BSON.
562+ Raises :class:`TypeError` if `dct` is not a mapping type, or
563+ contains keys that are not instances of ``(str,
564+ unicode)``. Raises :class:`~pymongo.errors.InvalidDocument` if
565+ `dct` cannot be converted to :class:`BSON`.
559566
560567 :Parameters:
561- - `dict`: mapping type representing a Mongo document
562- - `check_keys`: check if keys start with '$' or contain '.',
563- raising `pymongo.errors.InvalidName` in either case
568+ - `dct`: mapping type representing a document
569+ - `check_keys` (optional): check if keys start with '$' or
570+ contain '.', raising :class:`~pymongo.errors.InvalidName`
571+ in either case
572+ - `move_id` (optional): move ``_id`` to the front of the
573+ document's :class:`BSON` representation, if it is present.
564574 """
565- return cls (_dict_to_bson (dict , check_keys , move_id ))
575+ return cls (_dict_to_bson (dct , check_keys , move_id ))
566576 from_dict = classmethod (from_dict )
567577
568578 def to_dict (self ):
0 commit comments