.. highlight:: python .. default-domain:: py **************** Python Libraries **************** Overview ======== The Python libraries (versions 2 and 3) for WebMapReduce expect the mapper and reducer to be defined by the following functions: .. function:: mapper(key, value) Map the input pair composed of *key* and *value*. Both arguments are strings. .. function:: reducer(key, values) Reduce the intermediate *values* associated with *key*. *values* is an iterator that yields string values, and *key* is a string. Output is given through the :meth:`Wmr.emit` function. API === Class Wmr --------- .. class:: Wmr The Wmr class contains one static method: .. method:: emit(key, value) Outputs a pair composed of *key* and *value* from the mapper or reducer. Both arguments may have any type, and will be converted to strings using the built-in :func:`str()` function. Example Usage ============= The following mapper and reducer demonstrate WordCount in Python 3:: def mapper(key, value): words = key.split() for word in words: Wmr.emit(word, 1) :: def reducer(key, values): count = 0 for value in values: count += int(value) Wmr.emit(key, count)