cognition.util.misc

Misc utility code

Classes

TypedMixin

Provides convenient access to class type name

MutableWrapper

Supports in-place modification

AttrReferral

Read-only .key access to a value via external mapping { key:value }

ImplementsLessThan

A type with < implementation (useful for sorting)

StringifiedFunction

A callable object that wraps a function and provides a custom str() value.

Functions

is_set(tp)

Determines if the supplied type annotation is a set

is_list(tp)

Determines if the supplied type annotation is a list

stringify(str_value)

Decorator for providing a callable with an str() value

optionally_name(f, name)

Shorthand for optionally providing a callable an str() value

timed(f)

Decorator to time the execution of the supplied function

Module Contents

class cognition.util.misc.TypedMixin

Provides convenient access to class type name

property type: str

Access to type name

Returns:

class name

Return type:

str

cognition.util.misc.is_set(tp)

Determines if the supplied type annotation is a set

Parameters:

tp (type[Any]) – type to check

Returns:

True if supplied a set type

Return type:

bool

cognition.util.misc.is_list(tp)

Determines if the supplied type annotation is a list

Parameters:

tp (type[Any]) – type to check

Returns:

True if supplied a list type

Return type:

bool

class cognition.util.misc.MutableWrapper[T](value)

Supports in-place modification of (potentially) immutable types given a shared reference

Parameters:

value (T) – initial value

value: T
__str__()
Return type:

str

__repr__()
Return type:

str

class cognition.util.misc.AttrReferral(external_source)

Read-only .key access to a value via external mapping { key:value }

Parameters:

external_source (collections.abc.Mapping[str, Any]) – mapping reference

static view(obj)
Parameters:

obj (AttrReferral) – object of interest

Returns:

read-only mapping of available key/value pairs

Return type:

types.MappingProxyType[str, Any]

__getattr__(key)
Parameters:

key (str)

Return type:

Any

__setattr__(_key, _value)
Raises:

AttributeError – read-only access

Parameters:
  • _key (str)

  • _value (Any)

Return type:

None

__repr__()
Return type:

str

class cognition.util.misc.ImplementsLessThan

Bases: Protocol

A type with < implementation (useful for sorting)

__lt__(other)
Parameters:

other (Any)

Return type:

bool

class cognition.util.misc.StringifiedFunction[**P, R](func, str_value)

A callable object that wraps a function and provides a custom str() value.

Parameters:
  • func (collections.abc.Callable[P, R]) – wrapped function

  • str_value (str) – to supply upon str(self)

__call__(*args, **kwargs)

Calls the wrapped function

Parameters:
  • args (P.args)

  • kwargs (P.kwargs)

Return type:

R

__str__()

Returns the custom string value

Return type:

str

cognition.util.misc.stringify[**P, R](str_value)

Decorator for providing a callable with an str() value

Parameters:

str_value (str) – value to return upon str()

Returns:

resulting callable

Return type:

collections.abc.Callable[[collections.abc.Callable[P, R]], collections.abc.Callable[P, R]]

cognition.util.misc.optionally_name[**P, R](f, name)

Shorthand for optionally providing a callable an str() value

Parameters:
  • f (collections.abc.Callable[P, R]) – callable to optionally augment

  • name (str | None) – value to return upon str(), if supplied

Returns:

stringify’d function if a name is supplied; otherwise the original function

Return type:

collections.abc.Callable[P, R]

cognition.util.misc.timed[**P, R](f)

Decorator to time the execution of the supplied function

Parameters:

f (collections.abc.Callable[P, R]) – function to decorate

Returns:

function that times each invocation

Return type:

collections.abc.Callable[P, tuple[R, float]]