Recursive Monkey Patching
Project description
Recursive monkey patching for Python
====================================
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 ``sage_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?
====================================
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 ``sage_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?
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
Built Distribution
Close
Hashes for recursive-monkey-patch-0.3.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 14d51afa40ba3483916dbd999568dc1a63086e09a750d8a857cbf241985b8dea |
|
MD5 | a0a67a0f6496200df4eeb5abe1577f02 |
|
BLAKE2b-256 | ba27a44a98292e3ba852f6f1af4416bd603d7baa0893158d6c803870ebe071c1 |
Close
Hashes for recursive_monkey_patch-0.3.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1a87bd17ace27c9e25b42c377dc7f8e4abefb09920792921fd44c65d746900a |
|
MD5 | 43e442307a60d9a441e4b8486f774a55 |
|
BLAKE2b-256 | 3b67d0dc0df5d0dd0fd1bce434c6b903058896e64d914d27f966d20db9f557c5 |