haive.core.utils.getter_mixin

Collection utilities for Haive Core - GetterMixin.

This module includes powerful retrieval and filtering utilities for collection classes.

Classes

GetterMixin

A mixin providing rich lookup and filtering capabilities for collections.

Module Contents

class haive.core.utils.getter_mixin.GetterMixin[source]

Bases: Generic[T]

A mixin providing rich lookup and filtering capabilities for collections.

This mixin can be added to any collection class that implements _get_items() to provide powerful querying capabilities.

field_values(field_name)[source]

Get all values for a specific field across items.

Parameters:

field_name (str) – Field name to collect

Returns:

List of field values

Return type:

list[Any]

filter(**kwargs)[source]

Filter items by multiple attribute criteria.

Parameters:

**kwargs – Field name and value pairs to match

Returns:

List of matching items

Return type:

list[T]

find(predicate)[source]

Find first item matching a custom predicate function.

Parameters:

predicate (collections.abc.Callable[[T], bool]) – Function that takes item and returns boolean

Returns:

First matching item or None

Return type:

T | None

find_all(predicate)[source]

Find all items matching a custom predicate function.

Parameters:

predicate (collections.abc.Callable[[T], bool]) – Function that takes item and returns boolean

Returns:

List of matching items

Return type:

list[T]

first(**kwargs)[source]

Get first item matching criteria.

Parameters:

**kwargs – Field name and value pairs to match

Returns:

First matching item or None

Return type:

T | None

get_all_by_attr(attr_name, value)[source]

Get all items where attribute equals value.

Parameters:
  • attr_name (str) – Attribute name to check

  • value (Any) – Value to match

Returns:

List of matching items

Return type:

list[T]

get_by_attr(attr_name, value, default=None)[source]

Get first item where attribute equals value.

Parameters:
  • attr_name (str) – Attribute name to check

  • value (Any) – Value to match

  • default (T | None) – Default value if not found

Returns:

First matching item or default

Return type:

T | None

get_by_type(type_cls)[source]

Get all items of specified type.

Parameters:

type_cls (type) – Type to match

Returns:

List of matching items

Return type:

list[T]