Releases: hiroki0525/autoload_module
Releases · hiroki0525/autoload_module
2.0.0
1.7.2
This is minor fix release.
- 【Remove】deprecated parameter
pkg_name
. - 【Minor Fix】add validations.
1.7.1
By setting singleton=True
and strict=True
in ModuleLoader config , you can know unexpected initializing ModuleLoader instance.
# Strict mode
ModuleLoader.set_setting(singleton=True, strict=True)
# Initialize as singleton object
loader_a = ModuleLoader()
loader_b = ModuleLoader()
assert loader_a is loader_b
# Raise exception
loader_c = ModuleLoader('/tmp')
# -> autoload.exception.LoaderStrictModeError: Now singleton setting. You have already initialized object that has some attributes. Please check constructor variables.
1.7.0
You can now create singleton object by setting global config like below.
# singleton setting
ModuleLoader.set_setting(singleton=True)
loader_a = ModuleLoader()
loader_b = ModuleLoader()
loader_c = ModuleLoader('/test')
assert loader_a is loader_b # OK
assert loader_a is loader_c # OK
print(loader_a.base_path)
# -> '/Users/user1/abc'
# loader_c also has '/Users/user1/abc'.
assert loader_a.base_path is loader_c.base_path # OK
1.6.0
You can add global setting to ModuleLoader
.
import os
from autoload import ModuleLoader
# global setting
ModuleLoader.set_setting(base_path=os.getcwd(), strict=True)
loader_a = ModuleLoader()
loader_b = ModuleLoader()
print(loader_a.base_path)
# -> /Users/user1/abc
print(loader_a.strict)
# -> True
print(loader_b.base_path)
# -> /Users/user1/abc
print(loader_b.strict)
# -> True
1.5.0
load_classes
andload_functions
support to import not only Python package but also Python module.pkg_name
is duplicated. Please change parameterpkg_name
tosrc
if you use keyword argument.
module.py
from autoload import load_config
@load_config(order=1)
class Foo:
pass
@load_config(order=2)
class Bar:
pass
@load_config(order=3)
class Baz:
pass
main.py
from autoload import ModuleLoader
loader = ModuleLoader()
# Return Foo, Bar and Baz classes.
classes = loader.load_classes("module")
# Duplicated
classes = loader.load_classes(pkg_name="module")
# If you want to use keyword argument, please change it like below.
classes = loader.load_classes(src="module")
1.4.0
New parameter is added to ModuleLoader
constructor.
ModuleLoader(
base_path: Optional[str] = None,
strict: bool = False # New!!
)
If you decorate some classes or functions with @load_config
, the loader will load them. However, initialized strict=True
, the loader denies multiple loading as below.
- pkg/validator_a.py
from autoload import load_config
# This will be loaded because of name's rule.
class ValidatorA:
def validate(self):
print("validateA!!")
# Anything goes.
@load_config(order=2)
class Foo:
pass
- main.py
from autoload import ModuleLoader
from autoload.exception import LoaderStrictModeError
loader = ModuleLoader()
# return ValidatorA and Foo class objects.
classes = loader.load_classes("pkg")
# ModuleLoader strictly try to load a class or function object
# per a Python module on a basis of its name.
strict_loader = ModuleLoader(strict=True)
try:
classes = strict_loader.load_classes("pkg")
except LoaderStrictModeError as e:
print(e)
# -> Loader can only load a 'ValidatorA' class in validator_a module.
# -> Please check 'Foo' in validator_a module.
1.3.3
This is minor release.
- Fix how to import
ModuleLoader
andload_config
.
from autoload.module_loader import ModuleLoader, load_config
# You can also import as below
from autoload import ModuleLoader, load_config
1.3.2
Formally support Python >= 3.7
1.3.1
load_config
parameterload
default is True.
from autoload.decorator import load_config
# @load_config(load=True)
@load_config()
class CustomValidator:
def validate(self):
print("validateA!!")
- Fix
load=False
not working problem.