zensols.introspect package¶
Submodules¶
zensols.introspect.imp module¶
A utility class to load classes from modules.
- class zensols.introspect.imp.ClassImporter(class_name, reload=True)[source]¶
Bases:
object
Utility class that reloads a module and instantiates a class from a string class name. This is handy for prototyping code in a Python REPL.
- static full_classname(cls)[source]¶
Return a fully qualified class name string for class
cls
.- Return type:
- get_class_or_global()[source]¶
Like
get_class()
but try globals if the class isn’t fully qualified (i.e. sans module).- Return type:
- static get_module(name, reload=False)[source]¶
Return the module that has
name
.- Parameters:
name (
str
) – the string name, which can have dots (.
) to for sub modules- Return type:
- get_module_class(resolve_module=False)[source]¶
Return the module and class as a tuple of the given class in the initializer.
- Parameters:
resolve_module (
bool
) – ifTrue
then resolve the module from the class rather than the module portion of theclass_name
string- Return type:
- Returns:
a tuple of the module and class represented by
class_name
- instance(*args, **kwargs)[source]¶
Create an instance of the specified class in the initializer.
- Parameters:
args – the arguments given to the initializer of the new class
kwargs – the keyword arguments given to the initializer of the new class
- classmethod is_valid_class_name(class_name)[source]¶
Return whether a string represents a valid class name.
- Return type:
- exception zensols.introspect.imp.ClassImporterError[source]¶
Bases:
Exception
Raised for any run time exceptions during resolving and instantiating classes with
ClassImporter
.- __module__ = 'zensols.introspect.imp'¶
- __weakref__¶
list of weak references to the object
- class zensols.introspect.imp.ClassResolver[source]¶
Bases:
ABC
Used to resolve a class from a string.
- class zensols.introspect.imp.DictionaryClassResolver(instance_classes)[source]¶
Bases:
ClassResolver
Resolve a class name from a list of registered class names without the module part. This is used with the
register
method onConfigFactory
.- See:
ConfigFactory.register
zensols.introspect.insp module¶
Utility classes to help with dataclasses.
- class zensols.introspect.insp.Class(class_type, doc, fields, methods)[source]¶
Bases:
object
- __init__(class_type, doc, fields, methods)¶
-
fields:
Dict
[str
,ClassField
]¶ The fields of the class.
-
methods:
Dict
[str
,ClassMethod
]¶ The methods of the class.
- class zensols.introspect.insp.ClassDoc(text, params=None)[source]¶
Bases:
object
A meta data for documentation at any level of the class code (methods etc).
- PARAM_REGEX = re.compile('^\\s*:param ([^:]+):\\s*(.+)$')¶
documentation.
- Type:
Matches
- Type:
param
- __init__(text, params=None)¶
- exception zensols.introspect.insp.ClassError[source]¶
Bases:
Exception
Raised by
ClassInspector.
when a class can not be inspected or parsed byast
.- __module__ = 'zensols.introspect.insp'¶
- __weakref__¶
list of weak references to the object
- class zensols.introspect.insp.ClassField(name, dtype, doc, kwargs)[source]¶
Bases:
ClassParam
Represents a
dataclasses.dataclass
field.- __init__(name, dtype, doc, kwargs)¶
- class zensols.introspect.insp.ClassInspector(cls, attrs=None, data_type_mapper=None, include_private=False, include_init=False, strict='y')[source]¶
Bases:
object
A utility class to return all
dataclasses.dataclass
attribute (field) documentation.-
DECORATOR_META:
ClassVar
[str
] = 'CLASS_DECORATOR'¶ Attribute to set which must be a
builtins.dict
with the following keys:includes
: as a set of decorator names that can be set on methods to indicate inclusion on introspected method set. Otherwise the decorated method (such as @property) is omitted from the class metadata
-
INSPECT_META:
ClassVar
[str
] = 'CLASS_INSPECTOR'¶ Attribute to set to indicate to traverse superclasses as well. This is set as an empty
dict
to allow future implementations to filter on what’s traversed (i.e.include_fields
).
- __init__(cls, attrs=None, data_type_mapper=None, include_private=False, include_init=False, strict='y')¶
-
attrs:
Tuple
[str
,...
] = None¶ The class attributes to inspect, or all found are returned when
None
.
-
data_type_mapper:
TypeMapper
= None¶ The mapper used for narrowing a type from a string parsed from the Python AST.
-
DECORATOR_META:
- class zensols.introspect.insp.ClassMethod(name, doc, args)[source]¶
Bases:
object
Meta data for a method in a dataclass.
- __init__(name, doc, args)¶
-
args:
Tuple
[ClassMethodArg
,...
]¶ The arguments of the method.
- class zensols.introspect.insp.ClassMethodArg(name, dtype, doc, default, is_positional)[source]¶
Bases:
ClassParam
Meta data for an argument in a method.
- __init__(name, dtype, doc, default, is_positional)¶
- class zensols.introspect.insp.ClassParam(name, dtype, doc)[source]¶
Bases:
object
Represents a
dataclasses.dataclass
field.- __init__(name, dtype, doc)¶
- class zensols.introspect.insp.TypeMapper(cls, data_types=<factory>, default_type=<class 'str'>, allow_class=True)[source]¶
Bases:
object
A utility class to map string types parsed from
ClassInspector
to Python types.-
DEFAULT_DATA_TYPES:
ClassVar
[Dict
[str
,Type
]] = {'IntegerSelection': <class 'zensols.introspect.intsel.IntegerSelection'>, 'Path': <class 'pathlib.Path'>, 'bool': <class 'bool'>, 'dict': <class 'dict'>, 'float': <class 'float'>, 'int': <class 'int'>, 'list': <class 'list'>, 'pathlib.Path': <class 'pathlib.Path'>, 'str': <class 'str'>}¶ Supported data types mapped from data class fields.
- __init__(cls, data_types=<factory>, default_type=<class 'str'>, allow_class=True)¶
-
DEFAULT_DATA_TYPES:
zensols.introspect.intsel module¶
A class that parses a slice or element of an array.
- class zensols.introspect.intsel.IntegerSelection(raw)[source]¶
Bases:
object
Parses an string that selects integers. These (
kind
) include:Kind.single
:<int>
: a singleton integersKind.interval
:<int>-<int>
: all the integers in the inclusive intervalKind.list
:<int>,<int>,...
: a comma separated list (space optional)
To use, create it with
from_string()
and usetuple()
,list()
, then use as an iterable.
zensols.introspect.tester module¶
Runs test cases from the Python REPL.
- class zensols.introspect.tester.UnitTester(module_name, test_path=PosixPath('tests'))[source]¶
Bases:
object
A class that runs
unittest
unit test cases for the rapid prototyping use case. It does this by reloading the unit test case module, and then runs it for every invocation of thistyping.Callable
.- __init__(module_name, test_path=PosixPath('tests'))¶
- run()[source]¶
Run all test cases in the module identified by
module_name
.- Return type:
TextTestResult
Module contents¶
Contains modules for introspect and reflection.