booldog.utils.decorators

General-purpose function/method decorators used across BoolDog.

See the Primer on Python Decorators for background on the functools.wraps-based decorator pattern used throughout this module.

Functions

timer(func)

Print the runtime of the decorated function.

debug(func)

Print the function signature and return value.

silence_stdout(func)

Silence the standard output of the decorated function.

validate_node_argument(func)

Decorator that normalizes and validates the node argument of a

Module Contents

booldog.utils.decorators.timer(func)

Print the runtime of the decorated function.

Parameters:

func (callable) – The function to time.

Returns:

wrapper_timer – A wrapped version of func that, when called, runs func, prints its elapsed wall-clock time (via time.perf_counter) to stdout, and returns func’s return value unchanged.

Return type:

callable

booldog.utils.decorators.debug(func)

Print the function signature and return value.

Parameters:

func (callable) – The function to debug.

Returns:

wrapper_debug – A wrapped version of func that, when called, prints func’s name and the repr of each positional/keyword argument before calling it, prints the repr of its return value afterwards, and returns that value unchanged.

Return type:

callable

booldog.utils.decorators.silence_stdout(func)

Silence the standard output of the decorated function.

Redirects sys.stdout to os.devnull for the duration of the call to func, restoring the original sys.stdout afterwards (even if func raises).

Parameters:

func (callable) – The function whose stdout output should be suppressed.

Returns:

wrapper_silence_stdout – A wrapped version of func that runs it with stdout silenced and returns its return value unchanged.

Return type:

callable

booldog.utils.decorators.validate_node_argument(func)

Decorator that normalizes and validates the node argument of a Boolean-network instance method.

Intended for methods with signature (self, node_id, *args, **kwargs) on classes that expose a self.node_ids collection (as booldog.network.BoolDogModel and its mixins do). Before calling func, the wrapper:

  • if node_id is a booldog.classes.BoolDogNode, replaces it with its identifier attribute;

  • checks that the resulting identifier is a member of self.node_ids, raising ValueError if it is not.

func is then called with the (possibly replaced) plain identifier as its second positional argument, plus any remaining *args/**kwargs unchanged. This lets decorated methods assume node_id is always a valid, plain node identifier string, and lets callers pass either a booldog.classes.BoolDogNode or its identifier interchangeably.

Parameters:

func (callable) – The instance method to wrap; must accept (self, node_id, *args, **kwargs).

Returns:

wrapper_validate_node_argument – The wrapped method.

Return type:

callable

Raises:

ValueError – If the (normalized) node_id is not in self.node_ids.