API Reference¶
Locations
Arbitrary importable location. |
|
Package-based importable location, e.g. |
|
Filesystem-based importable location, e.g. |
Utils
Get object members that are instances of specified type. |
|
Get object members that are subclasses of specified class (excluding the class itself). |
|
Get nested attribute value. |
|
Generate random module name based on UUID4. |
Locations¶
To use any of supported concrete location types, use generic Location class.
Upon construction, it will return one of specific location objects supported:
ModuleLocation or PathLocation. Alternatively, construct specific location types
directly to enforce corresponding location type.
Location |
Format |
|---|---|
|
|
|
Location |
Examples |
|---|---|
|
|
|
Location
+spec : str
+obj : str | None
+load(modname, on_conflict, rename)
+match(spec: str)
ModuleLocation
+module : str
PathLocation
+path : Path
Location
+spec : str
+obj : str | None
+load(modname, on_conflict, rename)
+match(spec: str)
ModuleLocation
+module : str
PathLocation
+path : Path
- class ConflictResolution(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]¶
Conflict resolution strategy when module with given name is already imported.
- REUSE = 'reuse'¶
Don’t import again, use existing module from
sys.modules.
- RELOAD = 'reload'¶
Don’t import again, apply
importlib.reloadto existing module insys.modules.
- REPLACE = 'replace'¶
Delete existing module and use the imported one.
- RENAME = 'rename'¶
Retry module import with new generated name, and raise
ModuleNameConflictexception if conflict appears again.
- RAISE = 'raise'¶
Raise
ModuleNameConflictexception.
- class Location(self, spec: str)[source]¶
Arbitrary importable location.
- Parameters:
spec – location specification string.
- Raises:
InvalidLocation – when location string format is incorrect.
- classmethod match(spec: str) Match[str] | None[source]¶
Match location specification string with regular expression for specific location type.
- Parameters:
spec – location specification string.
- load(modname: str | Callable[[Self], str] | None = None, on_conflict: ConflictResolution | str = 'raise', rename: Callable[[str, Self], str] | None = None) object | ModuleType[source]¶
Import requested object or the whole module object from location.
This operation is atomic:
on import error, previous module with the same name is restored
on import error, new partially initialized module is removed from
sys.modules
- Parameters:
modname – name under which the module will be imported; if
str, usemodnameitself; ifCallable, use result of callingmodname()with currentLocationobject; otherwise, use default value (seeModuleLocation.loadandPathLocation.loadfor details).on_conflict – behaviour if
modnameis already present insys.modules(seeConflictResolutionfor details).rename – callable used to generate new module name on name conflict and if
on_conflictisrename; first string argument ismodnamethat leads to conflict, second argument is currentLocation.
- Raises:
TypeError | ValueError – when passed arguments of wrong type.
ModuleNameConflict – see
ConflictResolutionfor details.Exception – see specific location classes.
- Returns:
objectwhenobjpart was specified, otherwiseModuleType.
- class ModuleLocation(self, spec: str)[source]¶
- class ModuleLocation(self, *, module: str, obj: str | None = None)
Package-based importable location, e.g.
foo.bar:obj- Parameters:
spec – location specification string; if
specis passed, other arguments must be absent orNone.module – importable module name; required, if
specis not passed.obj – dot-separated object name to be imported; when missing, the whole module will be loaded
- Raises:
ValueError – when passed incorrect arguments.
InvalidLocation – when location string format is incorrect.
- classmethod match(spec: str) Match[str] | None[source]¶
Match location specification string with corresponding regular expression.
- Parameters:
spec – location specification string.
- load(modname: str | Callable[[Self], str] | None = None, on_conflict: ConflictResolution | str = 'raise', rename: Callable[[str, Self], str] | None = None) object | ModuleType[source]¶
Import requested object or the whole module object from importable module.
This operation is atomic:
on import error, previous module with the same name is restored
on import error, new partially initialized module is removed from
sys.modules
- Parameters:
modname – name under which the module will be imported; if
str, usemodnameitself; ifCallable, use result of callingmodname()with currentLocationobject; by default, usemodulefromspec.on_conflict – behaviour if
modnameis already present insys.modules(seeConflictResolutionfor details).rename – callable used to generate new module name on name conflict and if
on_conflictisrename; first string argument ismodnamethat leads to conflict, second argument is currentLocation.
- Raises:
TypeError | ValueError – when passed arguments of wrong type.
ModuleNameConflict – see
ConflictResolutionfor details.ModuleNotFoundError – when
modnameis not discoverable.ImportError – when module import fails.
AttributeError – when
objname can’t be found in imported module.
- Returns:
objectwhenobjpart was specified, otherwiseModuleType.
- class PathLocation(self, spec: str)[source]¶
- class PathLocation(self, *, path: Path | str, obj: str | None = None)
Filesystem-based importable location, e.g.
foo/bar.py:obj- Parameters:
- Raises:
ValueError – when passed incorrect arguments.
InvalidLocation – when location string format is incorrect.
- classmethod match(spec: str) Match[str] | None[source]¶
Match location specification string with corresponding regular expression.
- Parameters:
spec – location specification string.
- load(modname: str | Callable[[Self], str] | None = None, on_conflict: ConflictResolution | str = 'raise', rename: Callable[[str, Self], str] | None = None) object | ModuleType[source]¶
Import requested object or the whole module object from location.
This operation is atomic:
on import error, previous module with the same name is restored
on import error, new partially initialized module is removed from
sys.modules
- Parameters:
modname – name under which the module will be imported; if
str, usemodnameitself; ifCallable, use result of callingmodname()with currentLocationobject; by default, usepathstem fromspec.on_conflict – behaviour if
modnameis already present insys.modules(seeConflictResolutionfor details).rename – callable used to generate new module name on name conflict and if
on_conflictisrename; first string argument ismodnamethat leads to conflict, second argument is currentLocation.
- Raises:
TypeError | ValueError – when passed arguments of wrong type.
ModuleNameConflict – see
ConflictResolutionfor details.FileNotFoundError – when
pathdoes not exist.IsADirectoryError – when
pathis a directory.ImportError – when module import fails.
AttributeError – when
objname can’t be found in imported module.
- Returns:
objectwhenobjpart was specified, otherwiseModuleType.
- unload(module: str | ModuleType) None[source]¶
Unload previously imported module. The module is not guaranteed to be garbage collected (other objects might be referencing it).
This function roughly equals to
del sys.modules[module] del module
- Parameters:
module – imported module name or module object to be deleted.
- Raises:
KeyError – when there is no imported module with given name.
Exceptions¶
Utils¶
- get_instances(obj: object, cls: type[T], order: OrderBy | str | Callable[[T], Any] = 'name') list[T][source]¶
Get object members that are instances of specified type. Uses
inspect.getmembersandisinstance.- Parameters:
obj (
object) – object to get members from.cls (
type) – type of members to be returned.order (
OrderBy|str|Callable[[T], Any]) – sorting method or sort key function; defaults to'name'.
- Raises:
TypeError – or other
Exceptionraised byinspect.getfileandinspect.getsourcelineswhen sortingorder='source'.- Return type:
list[object]
Example
>>> import app.plugins >>> plugins = get_instances(app.plugins, Plugin)
- get_subclasses(obj: object, cls: type[T], order: OrderBy | str | Callable[[T], Any] = 'name') list[type[T]][source]¶
Get object members that are subclasses of specified class (excluding the class itself). Uses
inspect.getmembersandissubclass.- Parameters:
- Raises:
TypeError – or other
Exceptionraised byinspect.getfileandinspect.getsourcelineswhen sortingorder='source'.- Return type:
list[object]
Example
>>> from unittest import TestCase >>> from tests import test_usage >>> cases = get_subclasses(test_usage, TestCase, order='source')
- getattr_nested(obj: object, name: str, default: type[Exception] | ~typing.Any = <class 'AttributeError'>) object[source]¶
Get nested attribute value. If attribute chain does not exist, raise exception or return default value.
- Parameters:
obj (
object) – object to get attribute value from.name (
str) – dot-separated nested attribute name.default (
Exception|Any) – raise exception ifdefaultis anExceptiontype (AttributeErrorby default); otherwise returndefaultvalue.
- Raises:
AttributeError – when nested attribute chain does not exist and
defaultwas not adjusted.
- Returns:
nested attribute value or
defaultvalue, if specified and not anExceptionsubclass.
Example
>>> from app import config >>> options = getattr_nested(config, f'{config.primary}.options') >>> missing = getattr_nested(config, 'does.not.exist', None)
- random_name(*args: Any, **kwargs: Any) str[source]¶
Generate random module name based on UUID4.
All arguments passed to this function will be ignored.
Example
>>> random_name() 'ufoh3xjrrozfcvfheyktg62pzia'
- class OrderBy(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]¶
Objects sorting method.
- NAME = 'name'¶
Order by object name.
- SOURCE = 'source'¶
Order by definition order in the source file, first by source filename path, then by first line number. This sorting method uses
inspect.getsourcefileandinspect.getsourcelines, and inherits their requirements on objects and exceptions raised.