Skip to main content

Convenient approach to monkey patching

Project description

Gorilla is a library providing a convenient approach to monkey patching.

Monkey patching is the process of modifying modules and classes attributes at runtime with the purpose of replacing of extending third-party code. See Wikipedia’s Monkey patch page for a more complete definition.

Features

  • creation of patches using decorators.

  • creation of batches of patches using the members of either classes or modules.

  • warns when patching an existing attribute and/or store the overriden attribute to make it accessible.

  • customizable behaviour.

  • allows the dynamic creation of patches at runtime.

  • compatible with both Python 2 and Python 3.

Usage

Marking a function my_function() as being a patch for a module destination is as easy as:

>>> import gorilla
>>> import destination
>>> @gorilla.patch(destination)
... def my_function():
...     print("Hello world!")

Marking a class destination.Class to be patched with the members of another class is no sweat either:

>>> import gorilla
>>> import destination
>>> @gorilla.patches(destination.Class)
... class MyClass(object):
...     def method(self):
...         print("Hello")
...     @classmethod
...     def class_method(cls):
...         print("world!")

See the overview and tutorial sections of the documentation for more details.

Documentation

Read the documentation online at <http://gorilla.readthedocs.org> or check their source from the doc folder.

The documentation can be built in different formats using Sphinx.

Running the Tests

A suite of unit tests is available from the tests directory. You can run it by firing:

$ python tests/run.py

To run specific tests, it is possible to pass names to match in the command line.

$ python tests/run.py TestCase test_my_code

This command will run all the tests within the TestCase class as well as the individual tests which contains test_my_code in their name.

Get the Source

The source code is available from the GitHub project page.

Contributing

Found a bug or got a feature request? Don’t keep it for yourself, log a new issue on GitHub.

Author

Christopher Crouzet <christophercrouzet.com>

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

gorilla-0.2.0.tar.gz (22.7 kB view hashes)

Uploaded Source

Built Distribution

gorilla-0.2.0-py2.py3-none-any.whl (8.3 kB view hashes)

Uploaded Python 2 Python 3

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