Skip to main content
Author:

Zero Piraeus

Contact:
z@etiol.net

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.

Example

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

import itertools
from ook import patch

# http://docs.python.org/2.7/library/itertools.html#itertools.compress

@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)

Usage

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."""
    pass

… 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."""
    pass

… which can be combined:

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

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:

@patch(some.module.or.class)
def method_or_function(signature):
    """Do something, no matter what."""
    pass

Installation

This should do the trick:

pip install ook

Release files for ook 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ook 1.0.2
File Size Uploaded
ook-1.0.2.tar.gz 18.0 kB Details

Built distribution (wheel)

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

Total release size: 24.1 kB

Release files / ook-1.0.2.tar.gz

Download URL ook-1.0.2.tar.gz
Size 18.0 kB
Tags Source
SHA-256 checksum
How to use checksums
e693d58fb3e8c804228a5bb22f64e601569b54bec448d6cabdf9e5beb1cfc844
BLAKE2b-256 checksum
How to use checksums
36221b0f2d4de75b3af470f4e05c4de6970d16ab08815c4150d335113a46faa4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / ook-1.0.2-py2.py3-none-any.whl

Download URL ook-1.0.2-py2.py3-none-any.whl
Size 6.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
7d26a1f6f7d2b707c313e263622793ecbce5345483b87eccf0d8a7247ffd286e
BLAKE2b-256 checksum
How to use checksums
8eec682b4996085fafe9e612f6d3cb87f237bb38eebcc82dfaf1d998c3ce7898
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
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