3737from pymongo .message import _INSERT , _UPDATE , _DELETE
3838from pymongo .options import ReturnDocument , _WriteOp
3939from pymongo .read_preferences import ReadPreference
40- from pymongo .results import *
40+ from pymongo .results import (BulkWriteResult ,
41+ DeleteResult ,
42+ InsertOneResult ,
43+ InsertManyResult ,
44+ UpdateResult )
4145from pymongo .write_concern import WriteConcern
4246
4347
4448try :
4549 from collections import OrderedDict
46- ordered_types = (SON , OrderedDict )
50+ _ORDERED_TYPES = (SON , OrderedDict )
4751except ImportError :
48- ordered_types = SON
52+ _ORDERED_TYPES = ( SON ,)
4953
5054_NO_OBJ_ERROR = "No matching object found"
5155
@@ -146,7 +150,7 @@ def __init__(self, database, name, create=False, codec_options=None,
146150 def _command (
147151 self , command , read_preference = None , codec_options = None , ** kwargs ):
148152 """Internal command helper.
149-
153+
150154 :Parameters:
151155 - `command` - The command itself, as a SON instance.
152156 - `read_preference` (optional) - An subclass of
@@ -200,9 +204,8 @@ def __repr__(self):
200204
201205 def __eq__ (self , other ):
202206 if isinstance (other , Collection ):
203- us = (self .__database , self .__name )
204- them = (other .__database , other .__name )
205- return us == them
207+ return (self .__database == other .database and
208+ self .__name == other .name )
206209 return NotImplemented
207210
208211 def __ne__ (self , other ):
@@ -334,20 +337,24 @@ def _insert(self, docs, ordered=True,
334337
335338 if manipulate :
336339 def gen ():
337- db = self .__database
340+ """Generator that applies SON manipulators to each document
341+ and adds _id if necessary.
342+ """
343+ _db = self .__database
338344 for doc in docs :
339345 # Apply user-configured SON manipulators. This order of
340346 # operations is required for backwards compatibility,
341347 # see PYTHON-709.
342- doc = db ._apply_incoming_manipulators (doc , self )
348+ doc = _db ._apply_incoming_manipulators (doc , self )
343349 if '_id' not in doc :
344350 doc ['_id' ] = ObjectId ()
345351
346- doc = db ._apply_incoming_copying_manipulators (doc , self )
352+ doc = _db ._apply_incoming_copying_manipulators (doc , self )
347353 ids .append (doc ['_id' ])
348354 yield doc
349355 else :
350356 def gen ():
357+ """Generator that only tracks existing _ids."""
351358 for doc in docs :
352359 ids .append (doc .get ('_id' ))
353360 yield doc
@@ -1312,8 +1319,8 @@ def aggregate(self, pipeline, **kwargs):
13121319 }
13131320 return CommandCursor (self , cursor_info , address )
13141321
1315- # TODO key and condition ought to be optional, but deprecation
1316- # could be painful as argument order would have to change.
1322+ # key and condition ought to be optional, but deprecation
1323+ # would be painful as argument order would have to change.
13171324 def group (self , key , condition , initial , reduce , finalize = None , ** kwargs ):
13181325 """Perform a query similar to an SQL *group by* operation.
13191326
@@ -1747,10 +1754,10 @@ def find_and_modify(self, query={}, update=None,
17471754 ", find_one_and_replace, or find_one_and_update instead" ,
17481755 DeprecationWarning , stacklevel = 2 )
17491756
1750- if ( not update and not kwargs .get ('remove' , None ) ):
1757+ if not update and not kwargs .get ('remove' , None ):
17511758 raise ValueError ("Must either update or remove" )
17521759
1753- if ( update and kwargs .get ('remove' , None ) ):
1760+ if update and kwargs .get ('remove' , None ):
17541761 raise ValueError ("Can't do both update and remove" )
17551762
17561763 # No need to include empty args
@@ -1766,16 +1773,16 @@ def find_and_modify(self, query={}, update=None,
17661773 kwargs ['sort' ] = helpers ._index_document (sort )
17671774 # Accept OrderedDict, SON, and dict with len == 1 so we
17681775 # don't break existing code already using find_and_modify.
1769- elif (isinstance (sort , ordered_types ) or
1776+ elif (isinstance (sort , _ORDERED_TYPES ) or
17701777 isinstance (sort , dict ) and len (sort ) == 1 ):
17711778 warnings .warn ("Passing mapping types for `sort` is deprecated,"
17721779 " use a list of (key, direction) pairs instead" ,
17731780 DeprecationWarning , stacklevel = 2 )
17741781 kwargs ['sort' ] = sort
17751782 else :
17761783 raise TypeError ("sort must be a list of (key, direction) "
1777- "pairs, a dict of len 1, or an instance of "
1778- "SON or OrderedDict" )
1784+ "pairs, a dict of len 1, or an instance of "
1785+ "SON or OrderedDict" )
17791786
17801787
17811788 fields = kwargs .pop ("fields" , None )
@@ -1806,10 +1813,10 @@ def find_and_modify(self, query={}, update=None,
18061813 def __iter__ (self ):
18071814 return self
18081815
1809- def next (self ):
1816+ def __next__ (self ):
18101817 raise TypeError ("'Collection' object is not iterable" )
18111818
1812- __next__ = next
1819+ next = __next__
18131820
18141821 def __call__ (self , * args , ** kwargs ):
18151822 """This is only here so that some API misusages are easier to debug.
0 commit comments