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!"
Models
DynamicPy provides its own system similar to dataclasses called models which are designed to aid in data validation and type hinting. A model can be defined by simply extending the dynamicpy.Model class and specifying fields. These fields can be populated using the model's constructor.
from dynamicpy import Model
class User(Model):
gid: int
name: str
avatar: str
User(guid=123, name="John Doe", avatar="https://bit.ly/3J73JHU")
Behaviour can be further configured using the dynamicpy.field function.
from dynamicpy import Model, field
class User(Model):
gid: int = field(cast=int)
name: str = field(default="Unnamed")
avatar: str = "https://bit.ly/3J73JHU" # default alternative
User(guid="123")
Models will also recursively load types with a from_dict classmethod (including other models).
from dynamicpy import Model
class User(Model):
gid: int
name: str
class Post(Model):
title: str
author: User
Post.from_dict({"title": "Lorem Ipsum", "author": {"gid": 123, "name": "John Doe"}})
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
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 dynamicpy-1.3.1.tar.gz.
File metadata
- Download URL: dynamicpy-1.3.1.tar.gz
- Upload date:
- Size: 11.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
960f95ad2b95cdc59682325a4125e5e4b7d99dc0e2e322526190d4195c774290
|
|
| MD5 |
17394a77eb632b90641c57bcaf54e806
|
|
| BLAKE2b-256 |
d4835bf262ebb4eb6c43222c383337d2fd7e596962cf2f195782e30e4f26a003
|
File details
Details for the file dynamicpy-1.3.1-py3-none-any.whl.
File metadata
- Download URL: dynamicpy-1.3.1-py3-none-any.whl
- Upload date:
- Size: 13.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f56304268cc4a96d6e41d087c1ce2232e53ef812d850275230a79ae3f163a469
|
|
| MD5 |
37f5d739817e702fcf0ddda6b699f1ac
|
|
| BLAKE2b-256 |
35d56ac3926607ca4bfee6ebd3877ca1acd07426e453a2a4f546ec81002ab360
|