Skip to content

Conditions

minevent.conditions

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

minevent.conditions.BaseCondition

Bases: ABC

Define the base class to implement a condition for ConditionalEventHandler.

A child class has to implement the following methods:

- ``evaluate``
- ``equal``
Example
>>> from minevent import PeriodicCondition
>>> condition = PeriodicCondition(freq=3)
>>> condition.evaluate()
True
>>> condition.evaluate()
False
>>> condition.evaluate()
False
>>> condition.evaluate()
True
>>> condition.evaluate()
False
>>> condition.evaluate()
False
>>> condition.evaluate()
True

minevent.conditions.BaseCondition.equal abstractmethod

equal(other: Any) -> bool

Compare two conditions for equality.

This method should be implemented by child classes to define how conditions are compared. This is used when comparing conditional event handlers.

Parameters:

Name Type Description Default
other Any

The other object to compare with. Can be any type, though typically a condition.

required

Returns:

Type Description
bool

True if the two conditions are considered equal, otherwise False.

Example
>>> from minevent import PeriodicCondition
>>> condition = PeriodicCondition(freq=3)
>>> condition.equal(PeriodicCondition(freq=3))
True
>>> condition.equal(PeriodicCondition(freq=2))
False

minevent.conditions.BaseCondition.evaluate abstractmethod

evaluate() -> bool

Evaluate the condition given the current state.

This method should be implemented by child classes to define the logic for determining whether a conditional event handler should be executed. The method is called without arguments and may maintain internal state between calls.

Returns:

Type Description
bool

True if the condition is satisfied and the event handler logic should be executed, otherwise False.

Example
>>> from minevent import EventHandler
>>> def hello_handler() -> None:
...     print("Hello!")
...
>>> handler = EventHandler(hello_handler)
>>> handler.handle()
Hello!

minevent.conditions.PeriodicCondition

Bases: BaseCondition

Implement a periodic condition.

This condition evaluates to True every freq calls to the evaluate method. It maintains an internal counter that increments with each evaluation. The condition is True when the counter modulo freq equals zero.

Parameters:

Name Type Description Default
freq int

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

required
Example
>>> from minevent import PeriodicCondition
>>> condition = PeriodicCondition(freq=3)
>>> condition.evaluate()
True
>>> condition.evaluate()
False
>>> condition.evaluate()
False
>>> condition.evaluate()
True
>>> condition.evaluate()
False
>>> condition.evaluate()
False
>>> condition.evaluate()
True

minevent.conditions.PeriodicCondition.freq property

freq: int

Get the frequency of the periodic condition.

Returns:

Type Description
int

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