Dirty loader for python 3
Project description
dirty-loader
Easy to use loader library.
Changelog
Version 0.2.1
Three types of instance definition allowed: string, structured and structure simplified.
Version 0.2.0
Custom factories for classes.
Default factories for logging package.
Version 0.1.0
Some refactors.
New function import_class.
Instalation
$ pip install dirty-loader
Documentation
Main loaders
Loader
With Loader you could register sorted python modules. When you ask for a class it will try to load it for each module until it find one.
Example:
from dirty_loader import Loader
loader = Loader()
loader.register_module('tests.fake.namespace1')
loader.register_module('tests.fake.namespace2')
loader.register_module('tests.fake.namespace3')
klass = loader.load_class('FakeClass1')
from tests.fake.namespace1 import FakeClass1
assert klass == FakeClass1
# klass is tests.fake.namespace1.FakeClass1 because it exists in first module registered.
# Also, you could get an instance of class using factory
obj = loader.factory('FakeClass1', var1='a', var2=2)
# You could load classes from packages inside modules registered
klass = loader.load_class('subnamespace.FakeClass1')
from tests.fake.namespace3.subnamespace import FakeClass1 as SubFakeClass1
assert klass == SubFakeClass1
# klass is tests.fake.namespace3.subnamespace.FakeClass1 because it exists in first module registered.
LoaderReversed
It works in same way of Loader but it revers the sort when try to load a class.
Example:
from dirty_loader import LoaderReversed
loader = LoaderReversed()
loader.register_module('tests.fake.namespace1')
loader.register_module('tests.fake.namespace2')
klass = loader.load_class('FakeClass1')
from tests.fake.namespace2 import FakeClass1
assert klass == FakeClass1
# klass is tests.fake.namespace2.FakeClass1 because it exists in last module registered.
LoaderNamespace
With LoaderNamespace you could register sorted namespaces. When you ask for a class it will try to load it for each namespace until it find one. Each namespace has a python module associated. You could use the regular Loader way to load a class or you could specify the namespace you would like to use.
Example:
from dirty_loader import LoaderNamespace
loader = LoaderNamespace()
loader.register_namespace('fake1', 'tests.fake.namespace1')
loader.register_namespace('fake2', 'tests.fake.namespace2')
from tests.fake.namespace1 import FakeClass1, FakeClass2, FakeClass3
klass = loader.load_class('FakeClass1')
from tests.fake.namespace1 import FakeClass1
assert klass == FakeClass1
# klass is tests.fake.namespace1.FakeClass1 because it exists in last module registered.
# Also, you could get a class from specific namespace
klass = loader.load_class('FakeClass1', namespace='fake2)
from tests.fake.namespace2 import FakeClass1
assert klass == FakeClass1
# klass is tests.fake.namespace2.FakeClass1 because you specified it.
# Namespace could be specified in string class, too
klass = loader.load_class('fake2:FakeClass1')
assert klass == FakeClass1
# klass is tests.fake.namespace2.FakeClass1 because you specified it.
LoaderNamespaceReversed
It works in same way of LoaderNamespace but it revers the sort when try to load a class.
LoaderCached
A version of Loader with cache.
LoaderReversedCached
A version of LoaderReversed with cache.
LoaderNamespaceCached
A version of LoaderNamespace with cache.
LoaderNamespaceReversedCached
A version of LoaderNamespaceReversed with cache.
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
File details
Details for the file dirty-loader-0.2.1.tar.gz
.
File metadata
- Download URL: dirty-loader-0.2.1.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26dc933c7ca523578fdb877aa52ec91d34c85cf535960e36808939ac216296e8 |
|
MD5 | a68479685b58bd6f510cca200a7e6715 |
|
BLAKE2b-256 | b54f3d67993ae2d457b5078b8f677c69a4d7862a5026ba30fe16265f9f17849a |