Skip to content

Conditions

minevent.conditions

Implement some conditions that can be used in the event system.

A condition is any function without argument that returns a boolean, see the Condition protocol. The conditions of this module keep a state between calls.

minevent.conditions.AfterNCondition

Implement a condition that is False for the first n calls and True afterwards.

The condition is stateful, like PeriodicCondition: each call advances the counter, so do not share one instance between several handlers, and call it from one thread because the counter is not protected by a lock. Use reset to restart the counter.

Parameters:

Name Type Description Default
n int

The number of calls to skip. Must be a non-negative integer.

required

Raises:

Type Description
TypeError

if n is not an integer.

ValueError

if n is negative.

Example
>>> from minevent import AfterNCondition
>>> condition = AfterNCondition(2)
>>> [condition() for _ in range(4)]
[False, False, True, True]

minevent.conditions.AfterNCondition.n property

n: int

Get the number of skipped calls.

Returns:

Type Description
int

The number of calls that evaluate to False.

minevent.conditions.AfterNCondition.reset

reset() -> None

Reset the internal counter, as if the condition was just created.

Example
>>> from minevent import AfterNCondition
>>> condition = AfterNCondition(1)
>>> condition(), condition()
(False, True)
>>> condition.reset()
>>> condition()
False

minevent.conditions.Condition

Bases: Protocol

Define the protocol of a condition for EventHandler.

A condition is any function without argument that returns a boolean: a lambda, a method, or an object with a __call__ method like the conditions of this module. It is called each time the event is triggered, before the handler, and the handler is executed only if it returns True. A condition can keep a state between calls.

To combine conditions, write a function. The function decides which conditions are evaluated: lambda: a() and b() skips b when a is False, while lambda: all([a(), b()]) evaluates both, so stateful conditions advance together.

Example
>>> from minevent import EventHandler, PeriodicCondition
>>> condition = PeriodicCondition(freq=3)
>>> handler = EventHandler(print, ["Hello!"], condition=condition)
>>> handler.handle()
Hello!
>>> handler.handle()
>>> handler = EventHandler(print, ["Bye!"], condition=lambda: False)
>>> handler.handle()

minevent.conditions.Condition.__call__

__call__() -> bool

Evaluate the condition given the current state.

Returns:

Type Description
bool

True if the event handler should be executed, otherwise False.

minevent.conditions.PeriodicCondition

Implement a periodic condition.

This condition evaluates to True on the first call and then every freq calls. It maintains an internal counter that increments with each call. The condition is True when the counter modulo freq equals zero.

Note that a call is not idempotent: each call advances the counter, so the condition is stateful. Do not share one instance between several handlers, because they would advance the same counter. Use reset to restart the counter. The counter is not protected by a lock, so a condition should be called from one thread.

Parameters:

Name Type Description Default
freq int

The frequency (interval) at which the condition evaluates to True. Must be a positive integer.

required
skip_first bool

If True, the first call evaluates to False, and then the condition evaluates to True every freq calls. Default is False.

False

Raises:

Type Description
TypeError

if freq is not an integer.

ValueError

if freq is lower than 1.

Example
>>> from minevent import PeriodicCondition
>>> condition = PeriodicCondition(freq=3)
>>> [condition() for _ in range(7)]
[True, False, False, True, False, False, True]
>>> condition = PeriodicCondition(freq=2, skip_first=True)
>>> [condition() for _ in range(3)]
[False, False, True]

minevent.conditions.PeriodicCondition.freq property

freq: int

Get the frequency of the periodic condition.

Returns:

Type Description
int

The number of calls between each True result. The condition evaluates to True every freq calls.

minevent.conditions.PeriodicCondition.skip_first property

skip_first: bool

Indicate if the first call is skipped.

Returns:

Type Description
bool

True if the first call returns False, otherwise False.

minevent.conditions.PeriodicCondition.reset

reset() -> None

Reset the internal counter, as if the condition was just created.

Example
>>> from minevent import PeriodicCondition
>>> condition = PeriodicCondition(freq=2)
>>> condition(), condition()
(True, False)
>>> condition.reset()
>>> condition()
True