hup.base.pkg module

Package tree helper functions.

call_attr(name: str, *args, **kwds) → Any

Call an attribute of current module with given arguments.

Parameters:
  • name – Name of callable attribute
  • *args – Arbitrary arguments, that are passed to the call
  • *kwds – Arbitrary keyword arguments, that are passes to the call, if supported by the member attribute.
Returns:

Result of call.

crop_functions(prefix: str, module: Optional[module] = None) → list

Get list of cropped function names that satisfy a given prefix.

Parameters:
  • prefix – String conatining the initial prefix of the returned functions
  • module – Module reference. By default the current callers module is used.
Returns:

List of functions, that match a given prefix.

get_attr(name: str, default: Any = None, module: Optional[module] = None) → Any

Get an attribute of current module.

Parameters:
  • name – Name of attribute.
  • default – Default value, which is returned, if the attribute does not exist.
  • module – Optional reference to module, which is used to search for the given attribute. By default the current callers module is used.
Returns:

Value of attribute.

get_module(name: Optional[str] = None, errors: bool = True) → Optional[module]

Get reference to a module instance.

Parameters:
  • name – Optional name of module- If provided, the name is required to be a fully qualified name. By default a refrence to the module of the current caller is returned.
  • errors – Boolean value which determines if an error is raised, if the module could not be found. By default errors are raised.
Returns:

Module reference or None, if the name does not point to a valid module.

get_parent(module: Optional[module] = None) → module

Get parent module.

Parameters:module – Optional reference to module. By default the current callers module is used.
Returns:Module reference to the parent module of the current callers module.
get_root(module: Optional[module] = None) → module

Get top level module.

Parameters:module – Optional reference to module. By default the current callers module is used.
Returns:Module reference to the top level module of the current callers module.
get_root_name(fqn: str) → str
get_submodule(name: str, parent: Optional[module] = None) → Optional[module]

Get instance from the name of a submodule of the current module.

Parameters:
  • name – Name of submodule of given module.
  • parent – Optional reference to module, which has to be searched for submodules. By default the current callers module is used.
Returns:

Module reference of submodule or None, if the current module does not contain the given module name.

get_submodules(parent: Optional[module] = None, recursive: bool = False) → List[str]

Get list with submodule names.

Parameters:
  • parent – Optional reference to module, which has to be searched for submodules. By default the current callers module is used.
  • recursive – Boolean value which determines, if the search is performed recursively within all submodules. By default the returned list only comprises immediate submodules.
Returns:

List with fully qualified names of submodules.

has_attr(name: str, module: Optional[module] = None) → bool

Determine if a module has an attribute of given name.

Parameters:
  • name – Name of attribute
  • module – Optional reference to module, which is used to search for the given attribute. By default the current callers module is used.
Returns:

Result of call.

search(module: Optional[module] = None, pattern: Optional[str] = None, classinfo: Union[Type[Any], Tuple[Type[Any], ...]] = <class 'function'>, key: Optional[str] = None, val: Optional[str] = None, groupby: Optional[str] = None, recursive: bool = True, rules: Optional[Dict[str, Callable[[Any, Any], bool]]] = None, errors: bool = False, **kwds) → dict

Recursively search for objects within submodules.

Parameters:
  • module – Optional reference to module, which is used to search objects. By default the current callers module is used.
  • pattern – Only objects which names satisfy the wildcard pattern given by ‘pattern’ are returned. The format of the wildcard pattern is described in the standard library module fnmatch. If pattern is None, then all objects are returned. Default: None
  • classinfo – Classinfo given as a class, a type or a tuple containing classes, types or other tuples. Only members, which are ether an instance or a subclass of classinfo are returned. By default all types are allowed.
  • key – Name of function attribute which is used as the key for the returned dictionary. If ‘key’ is None, then the fully qualified function names are used as keys. Default: None
  • val – Name of function attribute which is used as the value for the returned dictionary. If ‘val’ is None, then all attributes of the respective objects are returned. Default: None
  • groupby – Name of function attribute which is used to group the results. If ‘groupby’ is None, then the results are not grouped. Default: None
  • recursive – Boolean value which determines if the search is performed recursively within all submodules. Default: True
  • rules – Dictionary with individual filter rules, used by the attribute filter. The form is {<attribute>: <lambda>, …}, where: <attribute> is a string with the attribute name and <lambda> is a boolean valued lambda function, which specifies the comparison of the attribute value against the argument value. Example: {‘tags’: lambda arg, attr: set(arg) <= set(attr)} By default any attribute, which is not in the filter rules is compared to the argument value by equality.
  • errors – Boolean value which determines if an error is raised, if the module could not be found. By default errors are not raised.
  • **kwds – Keyword arguments, that define the attribute filter for the returned dictionary. For example if the argument “tags = [‘test’]” is given, then only objects are returned, which have the attribute ‘tags’ and the value of the attribute equals [‘test’]. If, however, the filter rule of the above example is given, then any function, with attribute ‘tags’ and a corresponding tag list, that comprises ‘test’ is returned.
Returns:

Dictionary with function information as specified in the arguments ‘key’ and ‘val’.