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 — either via code or command line. 🐍

📦 Installation

Install directly from PyPI:

pip install 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.5.tar.gz (18.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.5-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modwrap-1.1.5.tar.gz
  • Upload date:
  • Size: 18.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.7 Linux/6.16.8+kali-amd64

File hashes

Hashes for modwrap-1.1.5.tar.gz
Algorithm Hash digest
SHA256 f9a9082a3847c9138f0e78fca59b5a25761f2fe1d9d09a76b7fff538565a8656
MD5 1ccb87b22b0a7ba37069287139a5bfcd
BLAKE2b-256 f812739d32ca6fa77c5c41e8edbe72b953dd710bca74cd4ee8ff580eeb85a1b7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modwrap-1.1.5-py3-none-any.whl
  • Upload date:
  • Size: 20.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.7 Linux/6.16.8+kali-amd64

File hashes

Hashes for modwrap-1.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 6edf207d64bfe9ab2381a15aff208fec02c7988d81267e5986c7e67f07cadbf1
MD5 7bf624eacd0986e0d178bb5a5bcc83a8
BLAKE2b-256 7fc296535869f27d2980c347c604a08e956cdb3a3cead88e80b3b8e4428980c0

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