@@ -32,31 +32,50 @@ class Collection(object):
32
32
"""A Mongo collection.
33
33
"""
34
34
35
- def __init__ (self , database , name , options = None ):
35
+ def __init__ (self , database , name , options = None , create = False , ** kwargs ):
36
36
"""Get / create a Mongo collection.
37
37
38
38
Raises :class:`TypeError` if `name` is not an instance of
39
39
:class:`basestring`. Raises
40
40
: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.
45
50
46
51
:Parameters:
47
52
- `database`: the database to get a collection from
48
53
- `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
52
64
53
65
.. mongodoc:: collections
54
66
"""
55
67
if not isinstance (name , basestring ):
56
68
raise TypeError ("name must be an instance of basestring" )
57
69
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
60
79
61
80
if not name or ".." in name :
62
81
raise InvalidName ("collection names cannot be empty" )
@@ -81,7 +100,7 @@ def __init__(self, database, name, options=None):
81
100
"Collection.name" )
82
101
self .__full_name_w = helpers .callable_value (self .__full_name ,
83
102
"Collection.full_name" )
84
- if options is not None :
103
+ if create or options is not None :
85
104
self .__create (options )
86
105
87
106
def __create (self , options ):
@@ -90,7 +109,7 @@ def __create(self, options):
90
109
91
110
# Send size as a float, not an int/long. BSON can only handle 32-bit
92
111
# ints which conflicts w/ max collection size of 10000000000.
93
- if "size" in options :
112
+ if options and "size" in options :
94
113
options ["size" ] = float (options ["size" ])
95
114
96
115
command = SON ({"create" : self .__name })
@@ -659,9 +678,9 @@ def options(self):
659
678
"""Get the options set on this collection.
660
679
661
680
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.
665
684
"""
666
685
result = self .__database .system .namespaces .find_one (
667
686
{"name" : self .__full_name })
0 commit comments