Skip to main content

Monki

Patch functions at runtime, the easy way.

Installation

pip install monki

Basic usage

We want to patch the following function:

def func():
    print('First')   # line 0
    print('Second')  # line 1
    print('Third')   # line 2

Different ways to patch it:

import monki

# Add code at beginning or end

monki.patch(func, start="print('Starting')", end="print('Ending')")
func()
    >>> 'Starting'
    >>> 'First'
    >>> 'Second'
    >>> 'Third'
    >>> 'Ending'

# Insert lines at any offset

monki.patch(func, insert_lines={1: "print('Injected line')", 2: "print('Another injection')"})
func()
    >>> 'First'
    >>> 'Injected line'
    >>> 'Second'
    >>> 'Another injection'
    >>> 'Third'

# Let's patch a loop inside the function!
# To do so we need to insert the loop and indent a line to go inside it.

monki.patch(func, insert_lines={1: "for i in range(3):"}, indent_lines=[1])
func()
    >>> 'First'
    >>> 'Second'
    >>> 'Second'
    >>> 'Second'
    >>> 'Third'

Limitations

  • Currently you cannot patch the same function twice
  • Some edge cases will not work with closure functions
  • Will probably only work on CPython 3+. Currently only tested on CPython 3.7

Metadata

Release files for monki 0.1.1

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

Source distribution (sdist)

Source distribution for monki 0.1.1
File Size Uploaded Provenance Actions
monki-0.1.1.tar.gz 5.3 kB Without provenance Details for monki-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for monki 0.1.1
File Interpreter ABI Platform Actions
monki-0.1.1-py3-none-any.whl Python 3 none any Details for monki-0.1.1-py3-none-any.whl

Total release size: 11.8 kB

Release files / monki-0.1.1.tar.gz

Download URL monki-0.1.1.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
8f9231a3fbe40000ebba556417f2e48ea0279ae34b99731560ac5ac6bc1aeb25
BLAKE2b-256 checksum
How to use checksums
cc3bf02606ceace20508be2405bd059bdb58bead2a4537c3bc859dae3eed0445
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/18.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.11

Release files / monki-0.1.1-py3-none-any.whl

Download URL monki-0.1.1-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f5164cf9010e4ac21c8cde8531289924714a95ff5829638c09710a4aec3d7559
BLAKE2b-256 checksum
How to use checksums
74b6e3f65ad757510f4694ea1e24c5b33a57276c00f3d3dbc40df6a9f3e0c7aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/18.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.11

Security for monki 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

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