Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

modwrap-1.1.7.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

modwrap-1.1.7-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file modwrap-1.1.7.tar.gz.

File metadata

  • Download URL: modwrap-1.1.7.tar.gz
  • Upload date:
  • Size: 5.8 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

Hashes for modwrap-1.1.7.tar.gz
Algorithm Hash digest
SHA256 cd020e5a0d3fa7f9101740793929b6ec8d86cf0987fb10e1b70772599d273812
MD5 113925da6cf71ab53f3d2bef81fb1af0
BLAKE2b-256 05adb0a4eeca2c0eedd57861928e43a18f459b987f4d92445038aa531e42f487

See more details on using hashes here.

File details

Details for the file modwrap-1.1.7-py3-none-any.whl.

File metadata

  • Download URL: modwrap-1.1.7-py3-none-any.whl
  • Upload date:
  • Size: 6.9 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

Hashes for modwrap-1.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 5f32bb074607c322c0f2514a0b51843788b9cd6617cfcf8ddfa5ac4c48407ca7
MD5 1303276963dbfe82542dc228ac48dce0
BLAKE2b-256 963adb4553e74aa05967822cb8e0c917f28ececbdcfb95d8a1a3d5769a745a67

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page