Skip to main content

Recursive Monkey Patching

Project description

Recursive monkey patching for Python
====================================

.. image:: https://travis-ci.org/nthiery/recursive-monkey-patch.svg?branch=master
:target: https://travis-ci.org/nthiery/recursive-monkey-patch

Motivating use case
-------------------

Let ``foo`` be a Python package, built on top of another Python
package ``bar``. Sometimes ``foo`` may wish to extend the ``bar``
package by adding features (e.g. methods) to existing classes or
modules. Sometimes the whole package is meant as a temporary location
where experimental features can mature and be distributed early until
they get merged into the ``bar`` package.

In such cases, it's desirable to write the source code of those
features as closely as if they were in the ``bar`` package.

``recursive-monkey-patch`` enables this by providing a tool to
recursively monkey patch the ``bar`` package. Let's assume for example
that we are writing a package ``bar-foo`` that requires the addition
of a method ``f`` to the class ``bar.x.y.z.Z``.

To achieve this, one writes a module ``foo.x.y.z.Z`` containing a
dummy ``Z`` class::

class Z:
def f(self):
return "f"

And then, upon initializing the package, one runs::

import bar
import foo
from recursive_monkey_patch import monkey_patch
monkey_patch(foo, bar)

which will recursively crawl through ``foo``, and insert methods like
``f`` at the corresponding location in ``bar``. If a class or module
in ``foo`` does not exist in ``bar``, then the module is inserted at
the corresponding location in ``bar``

Relation with SageMath
----------------------

This package is primarily meant for writing (experimental) packages on
top of `SageMath <http://sagemath.org>`_, an open source software for
mathematics that includes a large Python library. However, the
dependency upon the ``SageMath`` package is actually light:

- Running the doctests requires ``sage``;
- When the SageMath package is present, the monkey patching involves a
few additional Sage specific hacks.

ToDo
----

- Support for lazy imports
- Where is the natural place for running ``monkey_patch` in a package?

Related tools
-------------

- `patchy <https://github.com/adamchainz/patchy>`_: runtime application of source patches at the granularity of functions
- `unittest.mock.patch <https://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch>`_: temporary runtime monkey patching of functions; meant primarily for testing purposes


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

recursive-monkey-patch-0.4.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

recursive_monkey_patch-0.4.0-py2.py3-none-any.whl (8.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file recursive-monkey-patch-0.4.0.tar.gz.

File metadata

  • Download URL: recursive-monkey-patch-0.4.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.9.1 pkginfo/1.4.1 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.5

File hashes

Hashes for recursive-monkey-patch-0.4.0.tar.gz
Algorithm Hash digest
SHA256 546739fea5be2ea9f98b5ec44fafeb697b5cf9fdcda64a03422582ab03ee24c4
MD5 24376711dc5c6d7ff4e986f6ec6eab5a
BLAKE2b-256 8c7c4e196cf7994830c151eb404f49dc43b403927affd6861f720d9c576f9733

See more details on using hashes here.

File details

Details for the file recursive_monkey_patch-0.4.0-py2.py3-none-any.whl.

File metadata

  • Download URL: recursive_monkey_patch-0.4.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.9.1 pkginfo/1.4.1 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.5

File hashes

Hashes for recursive_monkey_patch-0.4.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 98922554e77f2e2c85a4f5d873a0f52efdc1b553f32444bd6c788b2ff583bf3e
MD5 3de67c5760aedfe633c17047a4fe4966
BLAKE2b-256 4b964c122e0340033c57b2107c640dfd8e70f66bdbe5a86d389fdc2318ee2647

See more details on using hashes here.

Supported by

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