Skip to content

attr_ module

Utilities for working with class/instance attributes.


MISSING Missing { #vectorbtpro.utils.attr.MISSING data-toc-label='MISSING' }

Sentinel that represents a missing value.


deep_getattr function

deep_getattr(
    obj,
    attr_chain,
    getattr_func=<function default_getattr_func>,
    call_last_attr=True
)

Retrieve attribute consecutively.

The attribute chain attr_chain can be:

  • string -> get variable/property or method without arguments
  • tuple of string -> call method without arguments
  • tuple of string and tuple -> call method and pass positional arguments (unpacked)
  • tuple of string, tuple, and dict -> call method and pass positional and keyword arguments (unpacked)
  • iterable of any of the above

Use getattr_func to overwrite the default behavior of accessing an attribute (see default_getattr_func()).

Hint

If your chain includes only attributes and functions without arguments, you can represent this chain as a single (but probably long) string.


default_getattr_func function

default_getattr_func(
    obj,
    attr,
    args=None,
    kwargs=None,
    call_attr=True
)

Default getattr_func.


get_dict_attr function

get_dict_attr(
    obj,
    attr
)

Get attribute without invoking the attribute lookup machinery.


parse_attrs function

parse_attrs(
    obj=None,
    own_only=False,
    sort_by=None
)

Parse attributes of a class, object, or a module, and return a DataFrame with types and paths.


AttrResolverMixin class

AttrResolverMixin()

Class that implements resolution of self and its attributes.

Resolution is getattr that works for self, properties, and methods. It also utilizes built-in caching.

Subclasses


cls_dir method

Get set of attribute names.


deep_getattr method

AttrResolverMixin.deep_getattr(
    *args,
    **kwargs
)

See deep_getattr().


post_resolve_attr method

AttrResolverMixin.post_resolve_attr(
    attr,
    out,
    final_kwargs=None
)

Post-process an object after resolution.

Must return an object.


pre_resolve_attr method

AttrResolverMixin.pre_resolve_attr(
    attr,
    final_kwargs=None
)

Pre-process an attribute before resolution.

Must return an attribute.


resolve_attr method

AttrResolverMixin.resolve_attr(
    attr,
    args=None,
    cond_kwargs=None,
    kwargs=None,
    custom_arg_names=None,
    cache_dct=None,
    use_caching=True,
    passed_kwargs_out=None,
    use_shortcuts=True
)

Resolve an attribute using keyword arguments and built-in caching.

  • If there is a get_{arg} method, uses get_{arg} as attr.
  • If attr is a property, returns its value.
  • If attr is a method, passes *args, **kwargs, and **cond_kwargs with keys found in the signature.
  • If use_shortcuts is True, resolves the potential shortcuts using AttrResolverMixin.resolve_shortcut_attr().

Won't cache if use_caching is False or any passed argument is in custom_arg_names.

Use passed_kwargs_out to get keyword arguments that were passed.


resolve_self method

AttrResolverMixin.resolve_self(
    cond_kwargs=None,
    custom_arg_names=None,
    impacts_caching=True,
    silence_warnings=False
)

Resolve self.

Note

cond_kwargs can be modified in-place.


resolve_shortcut_attr method

AttrResolverMixin.resolve_shortcut_attr(
    attr,
    *args,
    **kwargs
)

Resolve an attribute that may have shortcut properties.


self_aliases property

Names to associate with this object.


DefineMixin class

DefineMixin(
    *args,
    **kwargs
)

Mixin class for define.

Superclasses

Inherited members

Subclasses


asdict method

DefineMixin.asdict(
    full=True
)

Convert this instance to a dictionary.

If full is False, won't include fields with MISSING as value.


assert_field_not_missing method

DefineMixin.assert_field_not_missing(
    field_or_name
)

Raise an error if a field is missing.


fields class variable


fields_dict class variable


get_field class method

DefineMixin.get_field(
    field_name
)

Get field.


is_field_missing method

DefineMixin.is_field_missing(
    field_or_name
)

Raise an error if a field is missing.


is_field_optional class method

DefineMixin.is_field_optional(
    field_or_name
)

Return whether a field is optional.


is_field_required class method

DefineMixin.is_field_required(
    field_or_name
)

Return whether a field is required.


merge_over method

DefineMixin.merge_over(
    other,
    **changes
)

Merge this instance over another instance.


merge_with method

DefineMixin.merge_with(
    other,
    **changes
)

Merge this instance with another instance.


replace method

DefineMixin.replace(
    **changes
)

Create a new instance by making changes to the attribute values.


resolve method

DefineMixin.resolve(
    assert_not_missing=True
)

Resolve a field or all fields.


resolve_field method

DefineMixin.resolve_field(
    field_or_name
)

Resolve a field.


define class

define(
    *args,
    **kwargs
)

Prepare a class decorated with define.

Attaches DefineMixin as a base class (if not present) and applies attr.define.


field class method

define.field(
    **kwargs
)

Alias for attr.field.


optional_field class method

define.optional_field(
    *,
    default=_Missing.MISSING,
    metadata=None,
    **kwargs
)

Alias for attr.field with MISSING as default.

Has default in metadata.


required_field class method

define.required_field(
    **kwargs
)

Alias for attr.field with MISSING as default.

Doesn't have default in metadata.