A python module for dynamically interacting with objects to improve expandability.
Project description
DynamicPy
A python module for dynamically interacting with objects to improve expandability.
Features
Dynamic Loader
The DynamicLoader
class allows for the dynamic import of modules and the scraping of their globals.
To provide functionality with the scraped globals you must register a handler using the register_handler
method or the handler
decorator. Both methods take an optional selector
parameter which is a predicate to determine wether the handler should be called.
Example:
from dynamicpy import DynamicLoader
loader = DynamicLoader()
@loader.handler()
def handler(name: str, value: object):
print(name) # Prints the name of every global imported
loader.load_module("package.module")
DynamicPy provides a handful of utility functions to traverse modules in the stack which can be useful for streamlining this process.
Dependency Library
The DependencyLibrary
class allows you to create a library of objects which can then be injected into function parameters using type annotations.
Dependencies are added using the add
method, only a single dependency per type can be added to the library. Dependencies can be retrived using square brackets. You can check if an object has been added to the library using the in
operator which can also be used to check if there are any dependencies of a certain type in the library.
Example
from dynamicpy import DependencyLibrary
library = DependencyLibrary()
library.add("Hello World!")
print(library[str]) # Hello World!
print(str in library) # True
print("Hello World!" in library) # True
print("Lorem Ipsum" in library) # False
print(int in library) # False
The DependencyLibrary
class also allows for dependencies to be injected into function parameters using the inject
method.
Example
from dynamicpy import DependencyLibrary
library = DependencyLibrary()
library.add("Hello World!")
def injected(message: str):
print(message) # Hello World!
library.inject(injected)
Widgets
DynamicPy contains a helper for making 'widgets' with callback functions which are created using a decorator. Configure your widget by extending the BaseWidget
class. Set the type parameter to configure the expected callback type.
from typing import Any, Callable
from dynamicpy import BaseWidget
class ExampleWidget(BaseWidget[Callable[[str], Any]]):
def __init__(self, callback: Callable[[str], Any], enabled: bool) -> None:
super().__init__(callback)
DynamicPy will automatically generate a BaseWidget.decorate
function based off your constructor.
@ExampleWidget.decorate(enabled=False)
def example(message: str):
print(message)
This will add an attribute to the function, containing an instance of your widget. This can be easily retrieved using a Dynamic Loader's register_widget_handler
method or widget_handler
decorator.
from dynamicpy import DynamicLoader
loader = DynamicLoader()
@loader.widget_handler(ExampleWidget)
def widget_handler(widget: ExampleWidget):
widget.callback("Hello World!") # prints "Hello World!"
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
Built Distribution
Hashes for dynamicpy-1.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa9afcb22b0ececd930d82b017df031f8b8812e19b705c63917dac062b0add6d |
|
MD5 | 3b88b7cf195844c56df13f7613669fc5 |
|
BLAKE2b-256 | 87531a577c86a37eb6c43b6c7c7bdee7d527cb9d153ae3d335fd4761e02a29cc |