Skip to main content
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


Metadata

Release files for recursive-monkey-patch 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for recursive-monkey-patch 0.4.0
File Size Uploaded Provenance
recursive-monkey-patch-0.4.0.tar.gz 7.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for recursive-monkey-patch 0.4.0
File Interpreter ABI Platform
recursive_monkey_patch-0.4.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 15.8 kB

Release files / recursive-monkey-patch-0.4.0.tar.gz

Download URL recursive-monkey-patch-0.4.0.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
546739fea5be2ea9f98b5ec44fafeb697b5cf9fdcda64a03422582ab03ee24c4
BLAKE2b-256 checksum
How to use checksums
8c7c4e196cf7994830c151eb404f49dc43b403927affd6861f720d9c576f9733
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / recursive_monkey_patch-0.4.0-py2.py3-none-any.whl

Download URL recursive_monkey_patch-0.4.0-py2.py3-none-any.whl
Size 8.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
98922554e77f2e2c85a4f5d873a0f52efdc1b553f32444bd6c788b2ff583bf3e
BLAKE2b-256 checksum
How to use checksums
4b964c122e0340033c57b2107c640dfd8e70f66bdbe5a86d389fdc2318ee2647
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for recursive-monkey-patch 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page