Skip to main content

Utility functions for monkey-patching python code

Project description

About

python-monkey-business is a package that provides a decorator for monkey-patching python functions at runtime.

Installation

Using pip:

pip install python-monkey-business

Or, to install from source:

pip install -e git+https://github.com/theatlantic/python-monkey-business.git#egg=python-monkey-business

If the source is already checked out, use setuptools:

python setup.py develop

Usage

To use python-monkey-business to swap out a function in a class or module at runtime:

import monkeybiz

from foomodule import FooClass
import barmodule


# This replaces FooClass.bar with our method
@monkeybiz.patch(FooClass)
def bar(original_fn, *args, **kwargs):
    print "Patched!"
    return original_fn(*args, **kwargs)

# This replaces barmodule.baz with our function
@monkeybiz.patch(barmodule)
def baz(original_fn, *args, **kwargs):
    #...

The first argument to monkeybiz.patch can be either a module, a class, or a list of modules and/or classes. The decorator also takes optional name and avoid_doublewrap keyword arguments. If name is omitted, the name of the function being patched will be the name of the function being decorated. If avoid_doublewrap is True (the default), then functions and methods can only be patched once using this function.

Use monkeybiz.unpatch() to revert a monkey-patched function to its original.

License

This code is licensed under the Simplified BSD License. View the LICENSE file under the root directory for complete license and copyright information.

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

python-monkey-business-1.0.0.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

python_monkey_business-1.0.0-py2.py3-none-any.whl (5.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file python-monkey-business-1.0.0.tar.gz.

File metadata

File hashes

Hashes for python-monkey-business-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9976522989766f00b2aaa24ec96eacb91a6de7b7001d1452079323b071988e0e
MD5 7489fd3df813ed7a1fc70c142b2a3715
BLAKE2b-256 6e9b50f42469190ef986850a6078c591640cadac1341883eeee8d847d04e1382

See more details on using hashes here.

File details

Details for the file python_monkey_business-1.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for python_monkey_business-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6d4cf47f011945db838ccf04643acd49b82f7ad6ab7ecba4c8165385687a828a
MD5 eae4bc58ec27b53550f66a6afdbe40fa
BLAKE2b-256 3121ebee71d10599a02b3f85716428d33bd51a48253a99b923bcb8bf3f874233

See more details on using hashes here.

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