Package for reloading other packages and modules while Python is running
Project description
module_hot_reload
Package for reloading other packages and modules while Python is running
Installation
pip install module-hot-reload
Usage
Instantiate reloader you need, import module that you want to be reloaded, register it in reloader, start reloader
from module_hot_reload.reloaders import (
NewModuleAwareAllModulesRecursiveAutomaticReloader,
)
import example
r = NewModuleAwareAllModulesRecursiveAutomaticReloader()
example = r.register(example)
r.start()
while True:
print('example.e ', example.e)
input('waiting..........')
There are 2 types of module wrappers: (confusingly)ModuleWrapper
- used
primarily for reloading of module and its submodules and does it thread-safely;
ModuleAttributeAccessor
- provides thread-safe .
operator and prevents
collisions of module and wrapper class attribute names.
Reloader's register()
method returns module wrapped
with ModuleAttributeAccessor
.
The "API-methods" accept modules and know how to extract one form ModuleWrapper or ModuleAttributeAccessor. So you can safely do something like
from module_hot_reload.reloaders import (
NewModuleAwareAllModulesRecursiveAutomaticReloader,
NewModuleAwareAllModulesRecursiveManualReloader,
)
from module_hot_reload.module_wrappers import (
ModuleAttributeAccessor,
NewModuleAwareAllModulesRecursiveStandardModuleWrapper,
)
import example
r = NewModuleAwareAllModulesRecursiveAutomaticReloader()
w = r.module_wrapper_class
example = w(example)
# example is a ModuleWrapperBase instance now
example = r.register(example)
# example is a ModuleAttributeAccessor instance now
example = w(example)
# example is a ModuleWrapperBase instance now
# \/ code sample continues \/
As mentioned above, ModuleAttributeAccessor
provides thread-safe .
operator.
ModuleWrapper
does provide same functionality as well but with bulkier
syntax.
# /\ code sample continuation /\
example = ModuleAttributeAccessor(example)
print(example.e) # with ModuleAttributeAccessor
example = w(example)
print(example.locked_get('e')) # with ModuleWrapperBase
# \/ code sample continues \/
The question is: "Why would you need to use ModuleWrapper
then?".
You really don't need to. Using it you have the ability to manually reload
the module and access its attributes; but there are manual reoaders whose
register()
method still returns ModuleAttributeAccessor
.
# /\ code sample continuation /\
mr = NewModuleAwareAllModulesRecursiveManualReloader()
example = mr.register(example)
mr.reload()
print(example.e)
# is equivalent to
mw = mr.module_wrapper_class
example = mw(example)
example.reload()
print(example.locked_get('e'))
How it works?
Actual reloading of module(s) is done with importlib.reload()
so reed the
docks
to learn about reloaded modules behaviour.
Automatic reloaders use watchdog to watch file system events. It works with Windows as well as Linux.
ModuleWrapper
s and ModuleAttributeAccessor
s use sort of singleton pattern
but there is an instance of a particular class per wrapped module, so that
from module_hot_reload.module_wrappers import (
ModuleAttributeAccessor,
NewModuleAwareAllModulesRecursiveStandardModuleWrapper,
)
import example
maa_1 = ModuleAttributeAccessor(example)
maa_2 = ModuleAttributeAccessor(example)
w_1 = NewModuleAwareAllModulesRecursiveStandardModuleWrapper(example)
w_2 = NewModuleAwareAllModulesRecursiveStandardModuleWrapper(example)
print(maa_1 is maa_2) # True
print(w_1 is w_2) # True
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
File details
Details for the file module_hot_reload-0.0.8.tar.gz
.
File metadata
- Download URL: module_hot_reload-0.0.8.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a03913ac120dbbe744f48bfdee04f92e7c6873418d8f209068543bb583074fa |
|
MD5 | 2f4c28cd59c6d7a290e1578ec635aa27 |
|
BLAKE2b-256 | 931339ad851f9921aaeec03e8c388221ce8cf28cfa100fd2c6b6e7ba31cab018 |