providah
providah is a small, simple utility. It is an opinionated approach to creating object factories in Python without adding the additional complexity of maintaining factories.
Concept
The library is utilized as a basic global (to package) Iversion of Control (IoC) Container (Dependency Injection Container). The idea as presented is to provide an approach where all class in a package will automatically be registered. You can also override a specific registered class when you have monkey patched the class. When a new class is added it can manually be added to the registry, and this is helpful for application level classes, local extensions to a library, and so on. In even other situations you may want to add a a class when you want to retain the default version of a class, but want to support an alternative version as well.
Design
Examples
Example 1 - Library Factory
Within an __init__ .py - often at the root of a library's source code. This is ideal when you want to register all classes in the library in a single factory.
from providah.factories.package_factory import PackageFactory
PackageFactory.fill_registry()
Example 2 - Patching
When You want to monkey patch the functionality of a class.
from X import Y
from providah.factories.package_factory import PackageFactory
def new_method(params):
print('Hello there')
Y.print = new_method()
PackageFactory.register(key=str('Y'), library=str('X'), class_def=Y)
Example 3 - Ad Hoc Registration
When you want to add a class that will later be referenced by a label - in the case you are creating more than one class with the same key, but not using library identifiers.
class Writer:
@classmethod
def write(cls) -> None:
raise NotImplementedError('Base class method - must be implemented.')
class A(Writer):
@classmethod
def write(cls) -> None:
print("Hello World")
import sqlite3
class B(Writer):
__db_conn: sqlite3.connect('B.db')
@classmethod
def write(cls):
cls.__write_to_db('Hello World')
@classmethod
def __write_to_db(cls, data: str) -> None:
with cls.db_conn.cursor() as cursor:
cursor.execute(f"INSERT INTO logs VALUES ({data})")
cursor.commit()
from providah.factories.package_factory import PackageFactory
PackageFactory.register(key=str('A'), class_def=A, label=str('print_writer'))
PackageFactory.register(key=str('B'), class_def=A, label=str('db_writer'))
Example 4 - Using IoC Factory
from providah.factories.package_factory import PackageFactory
# Must import library - when library classes added to factory as in Example 1 (above) the
# runtime execution of this will happen when the library is imported.
import some_package
import another_package
# To make sure we get the right version - Exceptions occur otherwise.
some_obj = PackageFactory.create('bob', library='some_package')
another_obj = PackageFactory.create('bob', library='another_package')
Metadata
Release files for providah 0.1.15.0
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 |
|---|---|---|---|---|
| providah-0.1.15.0.tar.gz | 5.6 kB | Without provenance | Details for providah-0.1.15.0.tar.gz |
Release files / providah-0.1.15.0.tar.gz
| Download URL | providah-0.1.15.0.tar.gz |
|---|---|
| Size | 5.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b9f814e8b1b00070f2eb4432af4a04322e41914b7414249324973cc658fd9578
|
|
BLAKE2b-256 checksum How to use checksums |
f9d205b5f6fa470a3a29e77c2489ada3d6c4d6475a2f57bc758169ffacc9c7ba
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7
|
Security for providah 0.1.15.0
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.