Skip to content

Commit a5cdb2f

Browse files
committed
PYTHON-798 - Improve docs and add examples.
1 parent 648e5be commit a5cdb2f

File tree

5 files changed

+57
-10
lines changed

5 files changed

+57
-10
lines changed

pymongo/collection.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,24 @@ def with_options(
241241
self, codec_options=None, read_preference=None, write_concern=None):
242242
"""Get a clone of this collection changing the specified settings.
243243
244+
>>> coll1.read_preference
245+
Primary()
246+
>>> from pymongo import ReadPreference
247+
>>> coll2 = coll.with_options(read_preference=ReadPreference.SECONDARY)
248+
>>> coll1.read_preference
249+
Primary()
250+
>>> coll2.read_preference
251+
Secondary(tag_sets=None)
252+
244253
:Parameters:
245254
- `codec_options` (optional): An instance of
246255
:class:`~bson.codec_options.CodecOptions`. If ``None`` (the
247256
default) the :attr:`codec_options` of this :class:`Collection`
248257
is used.
249258
- `read_preference` (optional): The read preference to use. If
250259
``None`` (the default) the :attr:`read_preference` of this
251-
:class:`Collection` is used.
260+
:class:`Collection` is used. See :mod:`~pymongo.read_preferences`
261+
for options.
252262
- `write_concern` (optional): An instance of
253263
:class:`~pymongo.write_concern.WriteConcern`. If ``None`` (the
254264
default) the :attr:`write_concern` of this :class:`Collection`

pymongo/common.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,22 +454,27 @@ def __init__(self, codec_options, read_preference, write_concern):
454454

455455
@property
456456
def codec_options(self):
457-
"""An instance of :class:`~bson.codec_options.CodecOptions`."""
457+
"""Read only access to the :class:`~bson.codec_options.CodecOptions`
458+
of this instance.
459+
"""
458460
return self.__codec_options
459461

460462
@property
461463
def write_concern(self):
462-
"""The :class:`~pymongo.write_concern.WriteConcern` for this instance.
464+
"""Read only access to the :class:`~pymongo.write_concern.WriteConcern`
465+
of this instance.
466+
467+
.. versionchanged:: 3.0
468+
The :attr:`write_concern` attribute is now read only.
463469
"""
464470
return self.__write_concern
465471

466472
@property
467473
def read_preference(self):
468-
"""The read preference mode for this instance.
469-
470-
See :mod:`~pymongo.read_preferences` for available options.
474+
"""Read only access to the read preference of this instance.
471475
472-
.. versionadded:: 2.1
476+
.. versionchanged:: 3.0
477+
The :attr:`read_preference` attribute is now read only.
473478
"""
474479
return self.__read_preference
475480

pymongo/database.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,21 @@ def get_collection(self, name, codec_options=None,
230230
"""Get a :class:`~pymongo.collection.Collection` with the given name
231231
and options.
232232
233+
Useful for creating a :class:`~pymongo.collection.Collection` with
234+
different codec options, read preference, and/or write concern from
235+
this :class:`Database`.
236+
237+
>>> db.read_preference
238+
Primary()
239+
>>> coll1 = db.test
240+
>>> coll1.read_preference
241+
Primary()
242+
>>> from pymongo import ReadPreference
243+
>>> coll2 = db.get_collection(
244+
... 'test', read_preference=ReadPreference.SECONDARY)
245+
>>> coll2.read_preference
246+
Secondary(tag_sets=None)
247+
233248
:Parameters:
234249
- `name`: The name of the collection - a string.
235250
- `codec_options` (optional): An instance of
@@ -238,7 +253,8 @@ def get_collection(self, name, codec_options=None,
238253
used.
239254
- `read_preference` (optional): The read preference to use. If
240255
``None`` (the default) the :attr:`read_preference` of this
241-
:class:`Database` is used.
256+
:class:`Database` is used. See :mod:`~pymongo.read_preferences`
257+
for options.
242258
- `write_concern` (optional): An instance of
243259
:class:`~pymongo.write_concern.WriteConcern`. If ``None`` (the
244260
default) the :attr:`write_concern` of this :class:`Database` is

pymongo/mongo_client.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,21 @@ def get_database(self, name, codec_options=None,
10121012
"""Get a :class:`~pymongo.database.Database` with the given name and
10131013
options.
10141014
1015+
Useful for creating a :class:`~pymongo.database.Database` with
1016+
different codec options, read preference, and/or write concern from
1017+
this :class:`MongoClient`.
1018+
1019+
>>> client.read_preference
1020+
Primary()
1021+
>>> db1 = client.test
1022+
>>> db1.read_preference
1023+
Primary()
1024+
>>> from pymongo import ReadPreference
1025+
>>> db2 = client.get_database(
1026+
... 'test', read_preference=ReadPreference.SECONDARY)
1027+
>>> db2.read_preference
1028+
Secondary(tag_sets=None)
1029+
10151030
:Parameters:
10161031
- `name`: The name of the database - a string.
10171032
- `codec_options` (optional): An instance of
@@ -1020,7 +1035,8 @@ def get_database(self, name, codec_options=None,
10201035
used.
10211036
- `read_preference` (optional): The read preference to use. If
10221037
``None`` (the default) the :attr:`read_preference` of this
1023-
:class:`MongoClient` is used.
1038+
:class:`MongoClient` is used. See :mod:`~pymongo.read_preferences`
1039+
for options.
10241040
- `write_concern` (optional): An instance of
10251041
:class:`~pymongo.write_concern.WriteConcern`. If ``None`` (the
10261042
default) the :attr:`write_concern` of this :class:`MongoClient` is

pymongo/read_preferences.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def tag_sets(self):
109109
.. seealso:: `Data-Center Awareness
110110
<http://www.mongodb.org/display/DOCS/Data+Center+Awareness>`_
111111
"""
112-
return self.__tag_sets or [{}]
112+
return list(self.__tag_sets) if self.__tag_sets else [{}]
113113

114114
def __repr__(self):
115115
return "%s(tag_sets=%r)" % (

0 commit comments

Comments
 (0)