Skip to main content
Build status Coverage Status PyPI latest version Documentation status License

Gorilla is a Python library that provides a convenient approach to monkey patching.

Monkey patching is the process of modifying module and class attributes at runtime with the purpose of replacing or extending third-party code.

Although not a recommended practice, it is sometimes useful to fix or modify the behaviour of a piece of code from a third-party library, or to extend its public interface while making the additions feel like they are built-in into the library.

The Python language makes monkey patching extremely easy but the advantages of Gorilla are multiple, not only in assuring a consistent behaviour on both Python 2 and Python 3 versions, but also in preventing common source of errors, and making the process both intuitive and convenient even when faced with large numbers of patches to create.

Features

  • intuitive and convenient decorator approach to create patches.

  • can create patches for all class or module members at once.

  • compatible with both Python 2 and Python 3.

  • customizable behaviour.

Usage

Thanks to the dynamic nature of Python that makes monkey patching possible, the process happens at runtime without ever having to directly modify the source code of the third-party library:

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

The code above creates two patches, one for each member of the class MyClass, but does not apply them yet. In other words, they define the information required to carry on the operation but are not yet inserted into the specified destination class destination.Class.

Such patches created with the decorators can then be automatically retrieved by recursively scanning a package or a module, then applied:

>>> import gorilla
>>> import mypackage
>>> patches = gorilla.find_patches([mypackage])
>>> for patch in patches:
...     gorilla.apply(patch)

See the Tutorial section from the documentation for more detailed examples and explanations on how to use Gorilla.

Documentation

Read the documentation online at gorilla.readthedocs.io or check its source in the doc directory.

Out There

Projects using Gorilla include:

Author

Christopher Crouzet <christophercrouzet.com>

Release files for gorilla 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 gorilla 0.4.0
File Size Uploaded
gorilla-0.4.0.tar.gz 29.8 kB Details

Built distribution (wheel)

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

Total release size: 39.3 kB

Release files / gorilla-0.4.0.tar.gz

Download URL gorilla-0.4.0.tar.gz
Size 29.8 kB
Tags Source
SHA-256 checksum
How to use checksums
005ab8853b037162a7c77bb824604c6e081878ee03c09ad01ef41744856019d3
BLAKE2b-256 checksum
How to use checksums
0f000a7f837b9ea8ad3e0a98aa1d6ac52c13db9bc01ce9ccdca4d3072b8ea272
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.5

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

Download URL gorilla-0.4.0-py2.py3-none-any.whl
Size 9.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
25a407c783823db06a90f25303a49b2ab553b8527f4c826878ce5d6d2e25cb5c
BLAKE2b-256 checksum
How to use checksums
b32ad0eab79744c9bc32b916226e8cb71e275eb1c4e73ed6d682d99f907e5d10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.5

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

1 release file

0.0.1

1 release file

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