Patch functions at runtime, the easy way.
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
monki-0.1.1.tar.gz
(5.3 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
monki-0.1.1-py3-none-any.whl
(6.6 kB
view details)
File details
Details for the file monki-0.1.1.tar.gz.
File metadata
- Download URL: monki-0.1.1.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f9231a3fbe40000ebba556417f2e48ea0279ae34b99731560ac5ac6bc1aeb25
|
|
| MD5 |
248be36219d832e0dd76c0fe306c98ab
|
|
| BLAKE2b-256 |
cc3bf02606ceace20508be2405bd059bdb58bead2a4537c3bc859dae3eed0445
|
File details
Details for the file monki-0.1.1-py3-none-any.whl.
File metadata
- Download URL: monki-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f5164cf9010e4ac21c8cde8531289924714a95ff5829638c09710a4aec3d7559
|
|
| MD5 |
d4c75c626164d7ce26d52d5b04dfdcb9
|
|
| BLAKE2b-256 |
74b6e3f65ad757510f4694ea1e24c5b33a57276c00f3d3dbc40df6a9f3e0c7aa
|