@@ -32,31 +32,50 @@ class Collection(object):
3232 """A Mongo collection.
3333 """
3434
35- def __init__ (self , database , name , options = None ):
35+ def __init__ (self , database , name , options = None , create = False , ** kwargs ):
3636 """Get / create a Mongo collection.
3737
3838 Raises :class:`TypeError` if `name` is not an instance of
3939 :class:`basestring`. Raises
4040 :class:`~pymongo.errors.InvalidName` if `name` is not a valid
41- collection name. Raises :class:`TypeError` if `options` is not
42- an instance of :class:`dict`. If `options` is non-empty a
43- create command will be sent to the database. Otherwise the
44- collection will be created implicitly on first use.
41+ collection name. Any additional keyword arguments will be used
42+ as options passed to the create command. See
43+ :meth:`~pymongo.database.Database.create_collection` for valid
44+ options.
45+
46+ If `create` is ``True`` or additional keyword arguments are
47+ present a create command will be sent. Otherwise, a create
48+ command will not be sent and the collection will be created
49+ implicitly on first use.
4550
4651 :Parameters:
4752 - `database`: the database to get a collection from
4853 - `name`: the name of the collection to get
49- - `options`: dictionary of collection options. see
50- :meth:`~pymongo.database.Database.create_collection` for
51- details.
54+ - `options`: DEPRECATED dictionary of collection options
55+ - `create` (optional): if ``True``, force collection
56+ creation even without options being set
57+ - `**kwargs` (optional): additional keyword arguments will
58+ be passed as options for the create collection command
59+
60+ .. versionchanged:: 1.4+
61+ deprecating `options` in favor of kwargs
62+ .. versionadded:: 1.4+
63+ the `create` parameter
5264
5365 .. mongodoc:: collections
5466 """
5567 if not isinstance (name , basestring ):
5668 raise TypeError ("name must be an instance of basestring" )
5769
58- if options is not None and not isinstance (options , dict ):
59- raise TypeError ("options must be an instance of dict" )
70+ if options is not None :
71+ warnings .warn ("the options argument to Collection is deprecated "
72+ "and will be removed. please use kwargs instead." ,
73+ DeprecationWarning )
74+ if not isinstance (options , dict ):
75+ raise TypeError ("options must be an instance of dict" )
76+ options .update (kwargs )
77+ elif kwargs :
78+ options = kwargs
6079
6180 if not name or ".." in name :
6281 raise InvalidName ("collection names cannot be empty" )
@@ -81,7 +100,7 @@ def __init__(self, database, name, options=None):
81100 "Collection.name" )
82101 self .__full_name_w = helpers .callable_value (self .__full_name ,
83102 "Collection.full_name" )
84- if options is not None :
103+ if create or options is not None :
85104 self .__create (options )
86105
87106 def __create (self , options ):
@@ -90,7 +109,7 @@ def __create(self, options):
90109
91110 # Send size as a float, not an int/long. BSON can only handle 32-bit
92111 # ints which conflicts w/ max collection size of 10000000000.
93- if "size" in options :
112+ if options and "size" in options :
94113 options ["size" ] = float (options ["size" ])
95114
96115 command = SON ({"create" : self .__name })
@@ -659,9 +678,9 @@ def options(self):
659678 """Get the options set on this collection.
660679
661680 Returns a dictionary of options and their values - see
662- ` pymongo.database.Database.create_collection` for more information on
663- the options dictionary . Returns an empty dictionary if the collection
664- has not been created yet.
681+ :meth:`~ pymongo.database.Database.create_collection` for more
682+ information on the possible options . Returns an empty
683+ dictionary if the collection has not been created yet.
665684 """
666685 result = self .__database .system .namespaces .find_one (
667686 {"name" : self .__full_name })
0 commit comments