Make functions hot-reloadable with a decorator
Project description
hotfunc
This package provides a decorator for hot-reloading python functions.
It allows to redefine the function in the source code and see the changes immediately without restarting the program.
Installation
Install with pip
$ python -m pip install hotfunc
Usage
The following example uses the REPL, but hotfunc can be used in any python program.
Begin with importing the module and decorating the function that you want to hot-reload.
# myfile.py
from hotfunc import hotreload
name = "Mike"
@hotreload
def say():
print(f"Hello world! My name is {name}.")
Start python REPL, import the source file and call your function.
>>> from myfile import say
>>> say()
Hello world! My name is Mike.
Now, without stopping the interpreter, edit the source file changing the decorated function's body.
# myfile.py
from hotfunc import hotreload
name = "Mike"
@hotreload
def say():
print(f"Ciao mondo! Mi chiamo {name}.")
Switch back to REPL and call the function again.
>>> say()
Ciao mondo! Mi chiamo Mike.
As you can see, the function has been redefined, but its local environment (name variable defined outside the function) was preserved.
Caveats
When hot-reloading a function, its source file is read, definition found and the function itself redefined. Currenty it is done on every call, so is rather slow.
As of now, only top-level functions (defined at indentation level zero) are supported.
By default, exceptions raised when redefining or calling hot-reloaded functions are not re-raised. Instead, they are printed to stdout and the result of last successful function call is returned. This behaviour can be changed by enabling reraise flag:
@hotreload(reraise=True)
def myfunc():
# ...
License
Copyright (c) 2023 Michał Szajbe
Licensed under The MIT License.
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
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
File details
Details for the file hotfunc-0.0.1.tar.gz.
File metadata
- Download URL: hotfunc-0.0.1.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b0468fad262645ade30d6da75a429fb7cd33ecf4104a41da73a903cfc1599c39
|
|
| MD5 |
df310230cf3f48e8c1400fedecea77aa
|
|
| BLAKE2b-256 |
5fccfe864d633d8c0715c15eb6f39a68ed33e671574df4580df2b628179d4dc3
|
File details
Details for the file hotfunc-0.0.1-py3-none-any.whl.
File metadata
- Download URL: hotfunc-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
986ee430e6d5dca90b9710feb1c6b38876945e8cc58b009349413d33fd3b2ece
|
|
| MD5 |
7e3fb6a6842bab1c3573364b46f09d1b
|
|
| BLAKE2b-256 |
f426567800ee57a7455dec9ffff95b2a297573b24eecb431066cef244c9c37b6
|