typedargs.typeinfo module¶
Define the global type system that allows adding strong type information to python objects.
Summary¶
Classes:
TypeSystem |
TypeSystem permits the inspection of defined types and supports converted string and binary values to and from these types. |
Functions:
iprint |
A simple function to only print text if in an interactive session. |
Data:
type_system |
TypeSystem permits the inspection of defined types and supports converted string and binary values to and from these types. |
Reference¶
-
class
typedargs.typeinfo.
TypeSystem
(*args)[source]¶ Bases:
object
TypeSystem permits the inspection of defined types and supports converted string and binary values to and from these types.
-
register_type_source
(source, name=None)[source]¶ Register an external source of types.
This function does not actually load any external types, it just keeps track of the source, which must either be a str that is interpreted as a python entry_point group or a callable that will be passed an instance of this type system.
External type sources will only be loaded in an as needed basis when a type is encountered (and needed) that is not currently known. At that point external type sources will be considered until the type in question is found.
If an external type source fails to load for some reason, it is logged but the error is not fatal.
Parameters: - source (str or callable) – Either a pkg_resources entry_point group that will be searched for external types or a callable function that will be called as source(self) where self refers to this TypeSystem object.
- name (str) – Optional short name to use for reporting errors having to do with this source of types. This is most useful to pass when source is a callable.
-
convert_to_type
(value, type_or_name, **kwargs)[source]¶ Convert value to type ‘type_or_name’
If the conversion routine takes various kwargs to modify the conversion process, **kwargs is passed through to the underlying conversion function
-
convert_from_binary
(binvalue, type, **kwargs)[source]¶ Convert binary data to type ‘type’.
‘type’ must have a convert_binary function. If ‘type’ supports size checking, the size function is called to ensure that binvalue is the correct size for deserialization
-
get_type_size
(type)[source]¶ Get the size of this type for converting a hex string to the type. Return 0 if the size is not known.
-
format_value
(value, type_or_name, formatter=None, sub_formatters=None, **kwargs)[source]¶ Convert value to type specified by type_or_name and format it as a string.
type_or_name must be a known type in the type system or a type class. And format, if given, must specify a valid formatting option for the specified type.
-
is_known_type
(type_or_name)[source]¶ Check if type is known to the type system.
Returns: True if the type is a known instantiated simple type, False otherwise Return type: bool
-
split_type
(type_or_name)[source]¶ Given a potentially complex type, split it into its base type and specializers
-
get_proxy_for_type
(type_or_name)[source]¶ Return the type object corresponding to a given type_or_name.
type_or_name could be: - a simple builtin type like str, int, etc - a string name of a known type - a string name of an unknown complex type where base type is a known type factory - a complex type class from typing module: Dict[T, T] or List[T] - a string name of an unknown type (maybe a complex where base type is unknown type factory) If type_or_name does not fit these criteria then None would be returned.
If type_or_name is a string type name and it is not found in known types, this triggers the loading of external types until a matching type is found or until there are no more external type sources.
-
is_known_format
(type, format)[source]¶ Check if format is known for given type.
Returns boolean indicating if format is valid for the specified type.
-
inject_type
(type_or_name, typeobj)[source]¶ Given a module-like object that defines a type, add it to our type system so that it can be used with the iotile tool and with other annotated API functions.
type_or_name could be a string name or a type from typing module
-
load_type_module
(module)[source]¶ Given a module that contains a list of some types find all symbols in the module that do not start with _ and attempt to import them as types.
-
load_external_types
(path)[source]¶ Given a path to a python package or module, load that module, search for all defined variables inside of it that do not start with _ or __ and inject them into the type system. If any of the types cannot be injected, silently ignore them unless verbose is True. If path points to a module it should not contain the trailing .py since this is added automatically by the python import system
-