Skip to content

Commit 49ebd97

Browse files
wbolsterbehackett
authored andcommitted
Don't overwrite built-in names in map/reduce examples
Avoid assigning the MongoDB map/reduce function definitions to Python variables that have the same name as the built-in Python functions map() and reduce(), since that will effectively remove the possibility of using those built-ins in surrounding code.
1 parent 4f579e6 commit 49ebd97

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

doc/examples/map_reduce.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ the array:
4949
.. doctest::
5050

5151
>>> from bson.code import Code
52-
>>> map = Code("function () {"
52+
>>> mapper = Code("function () {"
5353
... " this.tags.forEach(function(z) {"
5454
... " emit(z, 1);"
5555
... " });"
@@ -59,7 +59,7 @@ The **reduce** function sums over all of the emitted values for a given key:
5959

6060
.. doctest::
6161

62-
>>> reduce = Code("function (key, values) {"
62+
>>> reducer = Code("function (key, values) {"
6363
... " var total = 0;"
6464
... " for (var i = 0; i < values.length; i++) {"
6565
... " total += values[i];"
@@ -75,7 +75,7 @@ iterate over the result collection:
7575

7676
.. doctest::
7777

78-
>>> result = db.things.map_reduce(map, reduce, "myresults")
78+
>>> result = db.things.map_reduce(mapper, reduce, "myresults")
7979
>>> for doc in result.find():
8080
... print doc
8181
...
@@ -90,14 +90,14 @@ PyMongo's API supports all of the features of MongoDB's map/reduce engine. One i
9090

9191
.. doctest::
9292

93-
>>> db.things.map_reduce(map, reduce, "myresults", full_response=True)
93+
>>> db.things.map_reduce(mapper, reducer, "myresults", full_response=True)
9494
{u'counts': {u'input': 4, u'reduce': 2, u'emit': 6, u'output': 3}, u'timeMillis': ..., u'ok': ..., u'result': u'...'}
9595

9696
All of the optional map/reduce parameters are also supported, simply pass them as keyword arguments. In this example we use the `query` parameter to limit the documents that will be mapped over:
9797

9898
.. doctest::
9999

100-
>>> result = db.things.map_reduce(map, reduce, "myresults", query={"x": {"$lt": 3}})
100+
>>> result = db.things.map_reduce(mapper, reducer, "myresults", query={"x": {"$lt": 3}})
101101
>>> for doc in result.find():
102102
... print doc
103103
...
@@ -109,7 +109,7 @@ With MongoDB 1.8.0 or newer you can use :class:`~bson.son.SON` to specify a diff
109109
.. doctest::
110110

111111
>>> from bson.son import SON
112-
>>> db.things.map_reduce(map, reduce, out=SON([("replace", "results"), ("db", "outdb")]), full_response=True)
113-
{u'counts': {u'input': 4, u'reduce': 2, u'emit': 6, u'output': 3}, u'timeMillis': ..., u'ok': ..., u'result': {u'db': ..., u'collection': ...}}
112+
>>> db.things.map_reduce(mapper, reducer, out=SON([("replace", "results"), ("db", "outdb")]), full_response=True)
113+
{u'counts': {u'input': 4, u'reducer': 2, u'emit': 6, u'output': 3}, u'timeMillis': ..., u'ok': ..., u'result': {u'db': ..., u'collection': ...}}
114114

115115
.. seealso:: The full list of options for MongoDB's `map reduce engine <http://www.mongodb.org/display/DOCS/MapReduce>`_

0 commit comments

Comments
 (0)