Python module wrapper for dynamic loading and callable signature validation.
Project description
modwrap is a pure Python 3 utility (no external dependencies) that lets you dynamically load and execute functions from any Python module. 🐍
📦 Installation
Install directly from PyPI:
pip install modwrap
Could be add to your project using poetry:
poetry add modwrap
🔧 Programmatic Usage
Use modwrap in your Python code to load modules, introspect callable signatures, and execute functions dynamically:
Basic Usage
from modwrap import ModuleWrapper
wrapper = ModuleWrapper("./examples/shell.py")
# Load and call a function
func = wrapper.get_callable("execute")
result = func(command="whoami")
print(result)
# Access the raw module object
mod = wrapper.module
print(mod.execute("whoami"))
Signature Validation
Validate function signatures with type checking:
# Validate with type hints
wrapper.validate_signature("execute", {"command": str, "timeout": float})
# Or use the non-raising version
if wrapper.has_signature("execute", {"command": str}):
func = wrapper.get_callable("execute")
result = func(command="ls")
Validate only argument names (no type checking):
# Validate argument names only
wrapper.validate_args("execute", ["command", "timeout"])
# Or use the non-raising version
if wrapper.has_args("execute", ["command", "timeout"]):
func = wrapper.get_callable("execute")
result = func(command="ls", timeout=30)
Dependency Analysis
Check what packages need to be installed:
deps = wrapper.get_dependencies()
print("Standard library:", deps['stdlib'])
print("Third-party packages:", deps['third_party'])
print("Local imports:", deps['local'])
# Check for missing dependencies
if deps['missing']:
print(f"Install missing packages: pip install {' '.join(deps['missing'])}")
Introspection
# Get function signature details
sig = wrapper.get_signature("execute")
print(sig) # {'command': {'type': 'str', 'default': None}, ...}
# Get docstrings
doc = wrapper.get_doc("execute") # Full docstring
summary = wrapper.get_doc_summary("execute") # First line only
# Check if callable exists
if wrapper.has_callable("execute"):
print("Function exists!")
# Get classes from module
cls = wrapper.get_class("MyClass") # Get specific class
cls = wrapper.get_class(must_inherit=BaseClass) # Get class by inheritance
Working with Classes
# Load and call class methods
wrapper.get_callable("MyClass.method_name")
# Get a class and instantiate it
MyClass = wrapper.get_class("MyClass")
instance = MyClass()
Utility Functions
Discover all modules in a directory:
from modwrap import list_modules, iter_modules
# Get all modules as a list
modules = list_modules("./my_modules")
for wrapper in modules:
print(wrapper.name)
# Or iterate lazily (memory efficient)
for wrapper in iter_modules("./my_modules"):
if wrapper.has_callable("main"):
wrapper.get_callable("main")()
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 modwrap-1.1.6.tar.gz.
File metadata
- Download URL: modwrap-1.1.6.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.9 Linux/6.16.8+kali-amd64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d1eba082515107d8b24173939e8367ddfdb53fb48cb013bda5b9de937400c135
|
|
| MD5 |
d98062a926b24c44af7353bbeefbf8e1
|
|
| BLAKE2b-256 |
553401e86fe2e55b342490cc53c8f809dd84714bd25e4bb97db10bc33d494942
|
File details
Details for the file modwrap-1.1.6-py3-none-any.whl.
File metadata
- Download URL: modwrap-1.1.6-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.9 Linux/6.16.8+kali-amd64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
acd6830f8c51102536ba46bd192b920f6cc6e435f0637dfab04cc9816771fe99
|
|
| MD5 |
cb9c4acb290129da0aa6ece9bd974c74
|
|
| BLAKE2b-256 |
285ee6ea67bd8839bdb7b4a283827cc032d94448dab430360480b89b9e1f04ed
|