Skip to main content
Help us improve PyPI by participating in user testing. All experience levels needed!

Python-version-sensitive monkeypatching.

Project description

Author: Zero Piraeus

ook is a simple library to assist with monkeypatching Python methods and functions on a per-Python-version basis. It provides one decorator, patch, which conditionally patches callables depending on which version of Python is running.


Adding the compress() function to Python 2.6’s itertools module:

import itertools
from ook import patch


@patch(itertools, 2.6)
def compress(data, selectors):
    # compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F
    return (d for d, s in itertools.izip(data, selectors) if s)


patch accepts an arbitrary number of version arguments, in a variety of formats:

@patch(some.module.or.class, 2.7, "2.6.8", (3, 2, 5), 3)
def method_or_function(signature):
    """Do something."""

… as well as min and max keyword arguments:

@patch(some.module.or.class, min="2.6.5", max=2.7)
def method_or_function(signature):
    """Do something."""

… which can be combined:

@patch(some.module.or.class, "3.3.5", max=3.3)
def method_or_function(signature):
    """Do something."""

Note: If you specify both keyword and non-keyword version arguments, the patch will only take effect if both the explicitly specified versions and the implied version ranges are satisfied.

With no arguments other than the module or class to be patched, patch applies the patch regardless of Python version:

def method_or_function(signature):
    """Do something, no matter what."""


This should do the trick:

pip install ook

Project details

Release history Release notifications

This version
History Node


History Node


History Node


History Node


History Node


History Node


History Node


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
ook-1.0.2-py2.py3-none-any.whl (6.1 kB) Copy SHA256 hash SHA256 Wheel py2.py3 Apr 11, 2017
ook-1.0.2.tar.gz (18.0 kB) Copy SHA256 hash SHA256 Source None Apr 11, 2017

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page