Skip to content

Commit eadb585

Browse files
author
Mike Dirolf
committed
DEPRECATE options dict to Collection/create_collection in favor of kwargs
1 parent e1b0c25 commit eadb585

File tree

2 files changed

+65
-29
lines changed

2 files changed

+65
-29
lines changed

pymongo/collection.py

+34-15
Original file line numberDiff line numberDiff line change
@@ -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})

pymongo/database.py

+31-14
Original file line numberDiff line numberDiff line change
@@ -160,29 +160,46 @@ def __getitem__(self, name):
160160
"""
161161
return self.__getattr__(name)
162162

163-
def create_collection(self, name, options={}):
164-
"""Create a new collection in this database.
165-
166-
Normally collection creation is automatic. This method should only if
167-
you want to specify options on creation. CollectionInvalid is raised
168-
if the collection already exists.
169-
170-
Options should be a dictionary, with any of the following options:
171-
172-
- "size": desired initial size for the collection (in bytes). must be
173-
less than or equal to 10000000000. For capped collections this size
174-
is the max size of the collection.
163+
def create_collection(self, name, options=None, **kwargs):
164+
"""Create a new :class:`~pymongo.collection.Collection` in this
165+
database.
166+
167+
Normally collection creation is automatic. This method should
168+
only be used to specify options on
169+
creation. :class:`~pymongo.errors.CollectionInvalid` will be
170+
raised if the collection already exists.
171+
172+
Options should be passed as keyword arguments to this
173+
method. Any of the following options are valid:
174+
175+
- "size": desired initial size for the collection (in
176+
bytes). must be less than or equal to 10000000000. For
177+
capped collections this size is the max size of the
178+
collection.
175179
- "capped": if True, this is a capped collection
176180
- "max": maximum number of objects if capped (optional)
177181
178182
:Parameters:
179183
- `name`: the name of the collection to create
180-
- `options` (optional): options to use on the new collection
184+
- `options`: DEPRECATED options to use on the new collection
185+
- `**kwargs` (optional): additional keyword arguments will
186+
be passed as options for the create collection command
187+
188+
.. versionchanged:: 1.4+
189+
deprecating `options` in favor of kwargs
181190
"""
191+
opts = {"create": True}
192+
if options is not None:
193+
warnings.warn("the options argument to create_collection is "
194+
"deprecated and will be removed. please use "
195+
"kwargs instead.", DeprecationWarning)
196+
opts.update(options)
197+
opts.update(kwargs)
198+
182199
if name in self.collection_names():
183200
raise CollectionInvalid("collection %s already exists" % name)
184201

185-
return Collection(self, name, options)
202+
return Collection(self, name, **opts)
186203

187204
def _fix_incoming(self, son, collection):
188205
"""Apply manipulators to an incoming SON object before it gets stored.

0 commit comments

Comments
 (0)