Skip to content

Filter rules

Filter nodes based on a set of rules.

Classes:

Name Description
BinaryOperator

Operators used for filtering nodes, taking two arguments.

FilterRule

Filter an object based on attribute named 'id' and apply the operator with the value.

FilterRuleEncoder

JSON encoder for FilterRule objects.

UnaryOperator

Operators used for filtering nodes, taking no argument.

Functions:

Name Description
dict_to_rule

JSON decoder for FilterRule objects.

BinaryOperator

Bases: Enum

Operators used for filtering nodes, taking two arguments.

Attributes:

Name Type Description
EQ

Equal operator.

GE

Greater than or equal operator.

GT

Greater than operator.

LE

Less than or equal operator.

LT

Less than operator.

NE

Not equal operator.

EQ

EQ = '=='

Equal operator.

GE

GE = '>='

Greater than or equal operator.

GT

GT = '>'

Greater than operator.

LE

LE = '<='

Less than or equal operator.

LT

LT = '<'

Less than operator.

NE

NE = '!='

Not equal operator.

FilterRule

FilterRule(
    enable: bool,
    name: str,
    id: str,
    operator: BinaryOperator | UnaryOperator,
    value: str | int | float | bool | None = None,
)

Filter an object based on attribute named 'id' and apply the operator with the value.

Attributes:

Name Type Description
enable bool

Enable or disable the rule.

id str

attribute to apply the filter on.

name str

Human readable string describing the attribute.

operator BinaryOperator | UnaryOperator

Operator used for filtering.

value str | int | float | bool | None

Comparison value for the filter (if operator is not UnaryOperator).

enable

enable: bool

Enable or disable the rule.

id

id: str

attribute to apply the filter on.

name

name: str

Human readable string describing the attribute.

operator

Operator used for filtering.

value

value: str | int | float | bool | None = None

Comparison value for the filter (if operator is not UnaryOperator).

FilterRuleEncoder

Bases: JSONEncoder

JSON encoder for FilterRule objects.

UnaryOperator

Bases: Enum

Operators used for filtering nodes, taking no argument.

Attributes:

Name Type Description
NOT_PRESENT

Attribute is not present (attribute value == 0).

PRESENT

Attribute is present (attribute value >= 1).

NOT_PRESENT

NOT_PRESENT = '❌'

Attribute is not present (attribute value == 0).

PRESENT

PRESENT = '✅'

Attribute is present (attribute value >= 1).

dict_to_rule

dict_to_rule(data: dict) -> FilterRule

JSON decoder for FilterRule objects.

Source code in src/slurm_viewer/data/filter_rules.py
def dict_to_rule(data: dict) -> FilterRule:
    """ JSON decoder for FilterRule objects. """
    op_name = data['operator']

    try:
        op: BinaryOperator | UnaryOperator = BinaryOperator[op_name]
    except KeyError:
        op = UnaryOperator[op_name]

    value = data['value']
    try:
        value = int(value)
    except ValueError:
        try:
            value = float(value)
        except ValueError:
            value = bool(value)

    enable = data['enable']
    if isinstance(enable, str):
        enable = enable.lower() == 'true'

    return FilterRule(
        enable=enable,
        name=data['name'],
        id=data['id'],
        operator=op,
        value=value
    )

Unit test for the filter rules class.

Functions:

Name Description
test_binary_rules

Test binary rules

test_unary_rules

Test unary rules

test_binary_rules

test_binary_rules() -> None

Test binary rules

Source code in tests/test_rules.py
def test_binary_rules() -> None:
    """ Test binary rules """
    num_cpu_eq = FilterRule(True, 'CPU', 'cpu_tot', BinaryOperator.EQ, 6)
    num_cpu_ge = FilterRule(True, 'CPU', 'cpu_tot', BinaryOperator.GE, 4)
    num_cpu_lt = FilterRule(True, 'CPU', 'cpu_tot', BinaryOperator.LT, 8)

    @dataclass
    class TestData:
        cpu_tot: int

    test_data = TestData(cpu_tot=6)

    assert num_cpu_eq(test_data)
    assert num_cpu_ge(test_data)
    assert num_cpu_lt(test_data)

    assert all(x(test_data) for x in [num_cpu_eq, num_cpu_ge, num_cpu_lt])

test_unary_rules

test_unary_rules() -> None

Test unary rules

Source code in tests/test_rules.py
def test_unary_rules() -> None:
    """ Test unary rules """
    cpu_present = FilterRule(True, 'CPU', 'cpu_tot', UnaryOperator.PRESENT)
    cpu_not_present = FilterRule(True, 'CPU', 'cpu_tot', UnaryOperator.NOT_PRESENT)

    @dataclass
    class TestData:
        cpu_tot: int

    test_data = TestData(cpu_tot=1)

    assert cpu_present(test_data)
    assert not cpu_not_present(test_data)

    # disable failing rules
    cpu_not_present.enable = False

    assert all(x(test_data) for x in [cpu_present, cpu_not_present])