i2.base¶
Tools to provide meta-interfaces (“mints”) of python objects.
A mint is a Mapping view of the (meta-)information describing the interface of
an object: for a callable, its parameters (name, kind, default, annotation) and its
return annotation.
Main entry points:
Mint: mint of any python objectMintOfCallable: mint of a callable, with parameter informationParametersMint: mint of the parameters of a callable
Functions
|
False for |
|
Deprecated alias of |
Classes
|
Expose the keys of a mapping as attributes ( |
|
Expose the attributes of an object as mapping keys ( |
|
Get a Mint object of a python object. |
|
Get a Mint object of a python object. |
Mint attributes computed from a callable's signature (parameters, return annotation, doc). |
|
Placeholder mixin for parsed-docstring mint attributes (not implemented yet). |
|
Type of the |
|
|
Mint of one parameter: its name, kind, default and annotation (and position, if given). |
|
Get mint of the parameters of a callable. |
- class i2.base.AttrFromKey(d)[source]¶
Bases:
objectExpose the keys of a mapping as attributes (
obj.kreadsd[k]).
- class i2.base.KeyFromAttr(d)[source]¶
Bases:
objectExpose the attributes of an object as mapping keys (
obj[k]readsgetattr(d, k)).
- class i2.base.Mint(obj, attrs=None)[source]¶
Bases:
MappingGet a Mint object of a python object. A Mint will provide parameters that provide (meta-)information about the interface of the python object.
>>> from pprint import pprint >>> # Mint of a function >>> def f(my_arg: int = 7) -> int: ... return my_arg + 10 >>> mint = Mint(f) >>> mint.obj_name, mint.type_name, mint.module_name, mint.obj_name ('f', 'function', 'i2.base', 'f') >>> # Mint of a module >>> import os as myos >>> mint = Mint(myos) >>> mint.obj_name, mint.type_name, mint.module_name, mint.obj_name ('os', 'module', 'os', 'os') >>> assert set(list(mint)) == {'module_name', 'module', 'type_name', 'obj_name'} >>> # Mint of a variable >>> v = 10 >>> mint = Mint(v) >>> mint.obj_name, mint.type_name, mint.module_name, mint.obj_name (NotFound, 'int', NotFound, NotFound) >>> assert set(list(mint)) == {'type_name'} # see that there's only one non-null attr!
- class i2.base.MintOfCallable(obj, attrs=None)[source]¶
Bases:
Mint,MintOfCallableMixin,MintOfDocMixinGet a Mint object of a python object. A Mint will provide parameters that provide (meta-)information about the interface of the python object.
>>> from pprint import pprint >>> def f(my_arg: int = 7) -> int: ... return my_arg + 10 >>> f.__doc__ = 'some documentation' >>> >>> mint = MintOfCallable(f) >>> mint.obj_name 'f' >>> mint.type_name 'function' >>> mint.module_name 'i2.base' >>> mint.parameters.my_arg {'name': 'my_arg', 'kind': 'POSITIONAL_OR_KEYWORD', 'default': 7, 'annotation': <class 'int'>, 'position': 0} >>> mint.doc_string 'some documentation' >>> mint.return_annotation <class 'int'> >>> def g(a, b: 'some_string_id_of_a_custom_type', c=1, d: int = 1) -> float: ... return a * b * c * d >>> pprint(dict(MintOfCallable(g).parameters)) {'a': {'name': 'a', 'kind': 'POSITIONAL_OR_KEYWORD', 'position': 0}, 'b': {'name': 'b', 'kind': 'POSITIONAL_OR_KEYWORD', 'annotation': 'some_string_id_of_a_custom_type', 'position': 1}, 'c': {'name': 'c', 'kind': 'POSITIONAL_OR_KEYWORD', 'default': 1, 'position': 2}, 'd': {'name': 'd', 'kind': 'POSITIONAL_OR_KEYWORD', 'default': 1, 'annotation': <class 'int'>, 'position': 3}}
- class i2.base.MintOfCallableMixin[source]¶
Bases:
objectMint attributes computed from a callable’s signature (parameters, return annotation, doc).
- class i2.base.MintOfDocMixin[source]¶
Bases:
objectPlaceholder mixin for parsed-docstring mint attributes (not implemented yet).
- class i2.base.NotFoundType[source]¶
Bases:
objectType of the
not_foundsentinel: falsy, reprNotFound.
- class i2.base.ParameterMint(param, position=None)[source]¶
Bases:
objectMint of one parameter: its name, kind, default and annotation (and position, if given).
Accepts an
inspect.Parameter-like object or a mapping with those keys; missing attributes are set toinspect.Parameter.empty.
- class i2.base.ParametersMint(params={})[source]¶
Bases:
MappingGet mint of the parameters of a callable.
>>> import inspect >>> from pprint import pprint >>> >>> def g(a, b: 'some_type', c=1, d: int = 1) -> float: ... return a * b * c * d >>> mint = ParametersMint(inspect.signature(g).parameters) >>> # mint is a mapping (like a read-only dict), so... >>> list(mint) ['a', 'b', 'c', 'd'] >>> >>> for arg_spec in mint.values(): ... print(arg_spec) {'name': 'a', 'kind': 'POSITIONAL_OR_KEYWORD', 'position': 0} {'name': 'b', 'kind': 'POSITIONAL_OR_KEYWORD', 'annotation': 'some_type', 'position': 1} {'name': 'c', 'kind': 'POSITIONAL_OR_KEYWORD', 'default': 1, 'position': 2} {'name': 'd', 'kind': 'POSITIONAL_OR_KEYWORD', 'default': 1, 'annotation': <class 'int'>, 'position': 3} >>> t = list(mint.items()) >>> t[0] ('a', {'name': 'a', 'kind': 'POSITIONAL_OR_KEYWORD', 'position': 0}) >>> t[1] ('b', {'name': 'b', 'kind': 'POSITIONAL_OR_KEYWORD', 'annotation': 'some_type', 'position': 1}) >>> >>> mint = ParametersMint(inspect.signature(g).parameters) >>> pprint(dict(mint)) {'a': {'name': 'a', 'kind': 'POSITIONAL_OR_KEYWORD', 'position': 0}, 'b': {'name': 'b', 'kind': 'POSITIONAL_OR_KEYWORD', 'annotation': 'some_type', 'position': 1}, 'c': {'name': 'c', 'kind': 'POSITIONAL_OR_KEYWORD', 'default': 1, 'position': 2}, 'd': {'name': 'd', 'kind': 'POSITIONAL_OR_KEYWORD', 'default': 1, 'annotation': <class 'int'>, 'position': 3}}>>> # and now, some cannibalistic fun... >>> # The following is skipped because not working in 3.10 >>> pprint( ... dict(ParametersMint(inspect.signature(ParametersMint).parameters)) ... ) {'args': {'name': 'args', 'kind': 'VAR_POSITIONAL', 'position': 0}, 'kwds': {'name': 'kwds', 'kind': 'VAR_KEYWORD', 'position': 1}} >>> pprint( ... dict(ParametersMint(inspect.signature(ParametersMint.__init__).parameters)) ... ) {'params': {'name': 'params', 'kind': 'POSITIONAL_OR_KEYWORD', 'default': FrozenDict({}), 'position': 1}, 'self': {'name': 'self', 'kind': 'POSITIONAL_OR_KEYWORD', 'position': 0}} >>> pprint( ... dict(ParametersMint(inspect.signature(ParametersMint.__new__).parameters)) ... ) {'args': {'name': 'args', 'kind': 'VAR_POSITIONAL', 'position': 1}, 'cls': {'name': 'cls', 'kind': 'POSITIONAL_OR_KEYWORD', 'position': 0}, 'kwds': {'name': 'kwds', 'kind': 'VAR_KEYWORD', 'position': 2}}