Table of content
Overview
dynamic_factory is package for dynamic implementations of factory pattern. Currently it has implemented only one class. FuncFactory that is designed to work with functions. Project repo can be found on github page and package can be found on pypi page.
Installation
Via pip:
pip install dynamic-factory
Via poetry:
poetry add dynamic-factory
Usage tutorial
Setup
Lets define two python files ./src1.py and ./src2.py
# ./src1.py
def func1():
print("greetings from src1 func1")
def func2(arg):
print(f"greetings from src1 func2 with arg {arg}")
def func12():
print("greetings from src1 func12")
def unregistered_func(arg):
print(f"greetings from src1 unregistered_func with arg {arg}")
# ./src2.py
def func1():
print("greetings from src2 func1")
def func2(arg):
print(f"greetings from src2 func2 with arg {arg}")
Quick start
If you want to register those functions in factory, decorate them with @FuncFactory.register() method.
It has two arguments
aliasoptional identifier that you can use instead of function name to execute function from factory levelgrouphelp distinguish which factory should register which function. If you use only one factory, don't have to change it. After that functions will be visible from factory.
# ./src1.py
from dynamic_factory import FuncFactory
@FuncFactory.register()
def func1():
print("greetings from src1 func1")
@FuncFactory.register(group="TWO")
def func2(arg):
print(f"greetings from src1 func2 with arg {arg}")
@FuncFactory.register()
@FuncFactory.register(group="TWO")
def func12():
print("greetings from src1 func12")
def unregistered_func(arg):
print(f"greetings from src1 unregistered_func with arg {arg}")
# ./src2.py
from dynamic_factory import FuncFactory
@FuncFactory.register(alias="src2_func1")
def func1():
print("greetings from src2 func1")
@FuncFactory.register(alias="func_two", group="TWO")
def func2(arg):
print(f"greetings from src2 func2 with arg {arg}")
To create factory simply initialize FuncFactory object.
from dynamic_factory import FuncFactory
import src1
import src2
base_factory = FuncFactory([src1])
print(
"base factory registered aliases:",
*base_factory.registered_aliases
)
for alias in base_factory.registered_aliases:
base_factory.execute(alias)
base factory registered aliases: func1 func12
greetings from src1 func1
greetings from src1 func12
Note that base_factory don't recorded functions from src2, thats happened because scr2 was not passed as argument in initialization. To fix that just extend modules list:
func1_factory = FuncFactory([src1, src2])
print(
"func1 factory registered aliases:",
*func1_factory.registered_aliases
)
for alias in func1_factory.registered_aliases:
func1_factory.execute(alias)
func1 factory registered aliases: func1 func12 src2_func1
greetings from src1 func1
greetings from src1 func12
greetings from src2 func1
Groups
Factories only register functions that belong to the same group as factory. If you want create more factories it may be good practice to define new groups. To define new group simply pass new unique string in register decorator. If you wish to create single function that belong to two group simply add two decorators like in ./src1.py:func12 example.
func2_factory = FuncFactory([src1, src2], group="TWO")
print(
"func2 factory registered aliases:",
*func2_factory.registered_aliases
)
func2_factory.execute("func12")
func2 factory registered aliases: func12 func2 func_two
greetings from src1 func12
Functions with arguments
If you want execute functions with arguments pass them to FuncFactory.execute function as args and kwargs
func2_factory.execute("func2", "argument")
func2_factory.execute("func_two", arg=123)
greetings from src1 func2 with arg argument
greetings from src2 func2 with arg 123
Aliases
Default alias for function is its name. but if you prefer it is possible to change it during registration process with alias param. Sometimes it is necessary like in ./src2.py:func1 because it has the same name as ./src1.py:func1, so without alias those functions would overwrite each other.
Limitations
Current implementation is adding field to functions that store record metadata, keep it in mind if you would do some advanced function manipulations like iteration trough func.__dict__
ToDo
- Warn user if he have the same alias for two different functions.
- implement
__getitem__that will access to factory function by alias.
Metadata
Release files for dynamic-factory 0.1.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| dynamic_factory-0.1.9.tar.gz | 4.2 kB | Without provenance | Details for dynamic_factory-0.1.9.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dynamic_factory-0.1.9-py3-none-any.whl | Python 3 | none | any | Details for dynamic_factory-0.1.9-py3-none-any.whl |
Total release size: 8.9 kB
Release files / dynamic_factory-0.1.9.tar.gz
| Download URL | dynamic_factory-0.1.9.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
48cb48c05592e1e9556b4617d7a1231f3d747b5feec5208a884d05aa8565e0ba
|
|
BLAKE2b-256 checksum How to use checksums |
375a9179e28e02b302d51c70772cabe05bf8d7e44ecde0bdf78f5ac32a519dfe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.10.9 Linux/5.19.0-41-generic
|
Release files / dynamic_factory-0.1.9-py3-none-any.whl
| Download URL | dynamic_factory-0.1.9-py3-none-any.whl |
|---|---|
| Size | 4.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5dfb3be21828d5a2e9b1f50f96bf8e305059b799014c8bcec72d5d2b0c7be4f0
|
|
BLAKE2b-256 checksum How to use checksums |
60abd9e8807ce5e2b7e2528213eb809aa55c1bf089f8a43fa49de7b6c30a9e92
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.10.9 Linux/5.19.0-41-generic
|
Security for dynamic-factory 0.1.9
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.