flib.base.otree module

Helper functions for objects and object trees.

call_attr(obj: object, attr: str, *args, **kwds) → Any

Call an object attribute with given arguments.

Parameters:
  • obj – Arbitrary object
  • attr – Name of callable object 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.

get_lang_repr(obj: object, separator: str = 'and') → str

Get enumerated representation of a collection’s items.

Parameters:seperator – String separator for collection items.
Returns:Natural language representation of object.
get_members(obj: object, pattern: Optional[str] = None, classinfo: Union[Type[Any], Tuple[Type[Any], ...]] = <class 'object'>, rules: Optional[Dict[str, Callable[[Any, Any], bool]]] = None, **kwds) → list

List members of an object.

This is a wrapper function to get_members_dict(), but only returns the names of the members instead of the respective dictionary of attributes.

get_members_dict(obj: object, pattern: Optional[str] = None, classinfo: Union[Type[Any], Tuple[Type[Any], ...]] = <class 'object'>, rules: Optional[Dict[str, Callable[[Any, Any], bool]]] = None, **kwds) → dict

Get dictionary with an object’s members dict attributes.

Parameters:
  • obj – Arbitrary object
  • pattern – Only members 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. By default all names are allowed.
  • 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.
  • rules

    Dictionary with custom test functions, which are used for the comparison of the attribute value against the argument value. The dictionary items are of the form <attribute>: <test>, where <attribute> is the attribute name and <test> is a boolean valued lambda function, with two arguments <arg> and <attr>, which respectively give the value of the keyword argument and the member attribute. A member passes the rules, if all <test> functions evaluate to True against the given keyword arguments:

    rules = {'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.

  • **kwds – Keyword arguments, that define the attribute filter for the returned dictionary. For example if the argument “tags = [‘test’]” is given, then only members 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 member, with attribute ‘tags’ and a corresponding tag list, that comprises ‘test’ is returned.
Returns:

Dictionary with fully qualified object names as keys and attribute dictinaries as values.

get_methods(obj: object, pattern: Optional[str] = None, groupby: Optional[str] = None, key: Optional[str] = None, val: Optional[str] = None) → Union[Dict[str, Any], Dict[Any, Dict[str, Any]], Dict[Any, Dict[Any, Dict[str, Any]]]]

Get methods from a given class instance.

Parameters:
  • obj – Class object
  • pattern – Only methods, 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.
  • groupby – Name of attribute which value is used to group the results. If groupby is None, then the results are not grouped. Default: None
  • key – Name of the attribute which is used as the key for the returned dictionary. If key is None, then the method names are used as key. Default: None
  • val – Name of attribute which is used as the value for the returned dictionary. If val is None, then all attributes of the respective methods are returned. Default: None
Returns:

Dictionary containing all methods of a given class instance, which names satisfy a given filter pattern.

get_name(obj: object) → str

Get name identifier for an object.

This function returns the name identifier of an object. If the object does not have a name attribute, the name is retrieved from the object class.

Parameters:obj – Arbitrary object
Returns:Name of an object.
get_summary(obj: object) → str

Get summary line for an object.

This function returns the summary line of the documentation string for an object as specified in PEP 257. If the documentation string is not provided the summary line is retrieved from the inheritance hierarchy.

Parameters:obj – Arbitrary object
Returns:Summary line for an object.
has_base(obj: object, base: Union[type, str]) → bool

Return true if the object has the given base class.

Parameters:
  • obj – Arbitrary object
  • base – Class name of base class
Returns:

True if the given object has the named base as base