Skip to main content

Thin MapReduce-like layer that wraps the Python multiprocessing library.

Project description

Thin MapReduce-like layer that wraps the Python multiprocessing library.

PyPI version and link. Read the Docs documentation status. GitHub Actions status. Coveralls test coverage summary.

Purpose

This package provides a streamlined interface for the built-in Python multiprocessing library. The interface makes it possible to parallelize in a succinct way (sometimes using only one line of code) a data workflow that can be expressed in a MapReduce-like form. More background information about this package’s design and implementation, as well a detailed use case, can be found in a related article.

Installation and Usage

This library is available as a package on PyPI:

python -m pip install mr4mp

The library can be imported in the usual way:

import mr4mp

Examples

In addition to the use case in a related article and the example below, smaller examples for each of the methods can be found in the documentation.

Word-Document Index

Assume there exists a collection of documents and that each document contains a collection of 7-character “words”. This example demonstrates how a dictionary that associates each word to the collection of documents in which that word appears can be built by leveraging multiprocessing and the MapReduce paradigm. Suppose the function definitions below are found within a module example.py:

from random import choice
from string import ascii_lowercase
from uuid import uuid4

def word():
    """Generate a random 7-character 'word'."""
    return ''.join(choice(ascii_lowercase) for _ in range(7))

def doc():
    """Generate a random 25-word 'document' and its identifier."""
    return ([word() for _ in range(25)], uuid4())

def docs():
    """Generate list of 50 random 'documents'."""
    return [doc() for _ in range(50)]

def word_to_doc_id_dict(document):
    """Build a dictionary mapping the 'words' in a 'document' to its identifier."""
    (words, identifier) = document
    return {w: {identifier} for w in words}

def merge_dicts(d, e):
    """Merge two dictionaries ``d`` and ``e``."""
    return {w: (d.get(w, set()) | e.get(w, set())) for w in d.keys() | e.keys()}

The code below (also included in example.py) constructs a dictionary that maps each individual word to the set of document identifiers in which that word appears. The code does so by incrementally building up larger and larger dictionaries (starting from one dictionary per document via the word_to_doc_id_dict function and merging them via the merge_dicts function), all while using the maximum number of processes supported by the system:

if __name__ == '__main__':
    import mr4mp
    from timeit import default_timer

    start = default_timer()
    p = mr4mp.pool()
    p.mapreduce(word_to_doc_id_dict, merge_dicts, docs())
    p.close()
    print(
        "Finished in " + str(default_timer()-start) + "s " +
        "using " + str(len(p)) + " process(es)."
    )

Note that any code invoking library methods must be protected inside an if __name__ == '__main__': block to ensure that the multiprocessing library methods can safely load the module without causing side effects. Executing the module might yield the output below:

python example.py
Finished in 0.664681524217187s using 2 process(es).

Suppose that it is explicitly indicated (by adding processes=1 to the invocation of pool) that only one process can be used:

p = mr4mp.pool(processes=1)

After the above modification, executing the module might yield the output below:

python example.py
Finished in 2.23329004518571s using 1 process(es).

Development

All installation and development dependencies are fully specified in pyproject.toml. The project.optional-dependencies object is used to specify optional requirements for various development tasks. This makes it possible to specify additional options (such as docs, lint, and so on) when performing installation using pip:

python -m pip install .[docs,lint]

Documentation

The documentation can be generated automatically from the source files using Sphinx:

python -m pip install .[docs]
cd docs
sphinx-apidoc -f -E --templatedir=_templates -o _source .. && make html

Testing and Conventions

All unit tests are executed and their coverage is measured when using pytest (see the pyproject.toml file for configuration details):

python -m pip install .[test]
python -m pytest

Some unit tests are included in the module itself and can be executed using doctest:

python src/mr4mp/mr4mp.py -v

Style conventions are enforced using Pylint:

python -m pip install .[lint]
python -m pylint src/mr4mp test/test_mr4mp.py

Contributions

In order to contribute to the source code, open an issue or submit a pull request on the GitHub page for this library.

Versioning

Beginning with version 0.1.0, the version number format for this library and the changes to the library associated with version number increments conform with Semantic Versioning 2.0.0.

Publishing

This library can be published as a package on PyPI by a package maintainer. First, install the dependencies required for packaging and publishing:

python -m pip install .[publish]

Ensure that the correct version number appears in pyproject.toml, and that any links in this README document to the Read the Docs documentation of this package (or its dependencies) have appropriate version numbers. Also ensure that the Read the Docs project for this library has an automation rule that activates and sets as the default all tagged versions. Create and push a tag for this version (replacing ?.?.? with the version number):

git tag ?.?.?
git push origin ?.?.?

Remove any old build/distribution files. Then, package the source into a distribution archive:

rm -rf build dist src/*.egg-info
python -m build --sdist --wheel .

Finally, upload the package distribution archive to PyPI:

python -m twine upload dist/*

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mr4mp-2.7.1.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

mr4mp-2.7.1-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file mr4mp-2.7.1.tar.gz.

File metadata

  • Download URL: mr4mp-2.7.1.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.2

File hashes

Hashes for mr4mp-2.7.1.tar.gz
Algorithm Hash digest
SHA256 217878bad42b3cbda64313097851c4d7433b5156f88958fccccc0a38a700136c
MD5 73e4cd81c25eb7a0d904f95ddde0beca
BLAKE2b-256 fd257fe4f3861e43a13a06aca716b4ad2b178014da20c244eca85291cabf9a37

See more details on using hashes here.

Provenance

File details

Details for the file mr4mp-2.7.1-py3-none-any.whl.

File metadata

  • Download URL: mr4mp-2.7.1-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.2

File hashes

Hashes for mr4mp-2.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e7fa344f0ab8839b9712cf16e2a7835b349217481ea181c98a615a3ada924421
MD5 dc33e8c8be43c6247aa4c24c80998108
BLAKE2b-256 f9dcb0f3a3fe7b1e9036ed0caf0fe693c7258c470d491324371014604cd310e1

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page