Skip to content

brainhops._core.properties

Properties that compute their value.

A [smartproperty][brainhops._core.properties.smartproperty] stores its value in a private attribute and computes the value when nothing is stored. A [lazyproperty][brainhops._core.properties.lazyproperty] computes its value on first access and caches it. Assigning either property, through any setter, deletes its own cached value, and the cached values that it names in invalidates. Code outside this module deletes a cached value with drop_cache, and reads one without computing it with cached_value. An alias reads, and may write, another attribute under a second name.

Functions:

alias

alias(source: str, *, readonly: bool = False) -> property

Return a property that reads another attribute under a second name.

An alias gives an attribute a second name, such as matrix for the data of an affine transformation, or makes an attribute read what another object holds, such as the degree of an inverse field that is the degree of its forward field. Reading the alias reads the source, and assigning it assigns the source, unless the alias is read-only.

The source may be a dotted path, such as "forward.degree". Such an alias is always read-only, because an object that reports a value of another object does not write into it.

Parameters:

Name Type Description Default
source str

The name of the attribute that the alias reads, or a dotted path to it.

required
readonly bool

Whether assigning the alias is refused.

False

Returns:

Type Description
property

The alias.

drop_cache

drop_cache(obj: object, *names: str) -> None

Delete the cached values of properties of an object.

A cached [smartproperty][brainhops._core.properties.smartproperty] or [lazyproperty][brainhops._core.properties.lazyproperty] deletes its cached value when it is assigned. This function is for code that changes the state from which a property is computed without assigning the property, for example a writer that points rawdata at the file it has just written. A property that has not cached a value is skipped.

Parameters:

Name Type Description Default
obj object

The object whose cached values are deleted.

required
*names str

The names of the properties.

()

cached_value

cached_value(obj: object, name: str) -> Any

Return the cached value of a property of an object, or None.

A cached [smartproperty][brainhops._core.properties.smartproperty] or [lazyproperty][brainhops._core.properties.lazyproperty] stores the value that it computes apart from a value that is assigned to it. This function is for code that needs the computed value only when it has already been computed, for example a writer that compares a decoded view, which may have been changed in place, with a fresh decode, without computing that view itself.

Parameters:

Name Type Description Default
obj object

The object whose cached value is read.

required
name str

The name of the property.

required

Returns:

Type Description
Any

The cached value, or None when the property has cached nothing, or when one of the attributes that the value was computed from holds another object (see the option invalidated_by of [smartproperty][brainhops._core.properties.smartproperty]).