Compare

Inheritance diagram of musify.processors.compare

Processor making comparisons between objects and data types.

Classes:

Comparer(condition[, expected, field, ...])

Compares an item or object with another item, object or a given set of expected values to find a match.

class musify.processors.compare.Comparer(condition, expected=None, field=None, reference_required=False)

Bases: DynamicProcessor, Hashable

Compares an item or object with another item, object or a given set of expected values to find a match.

Parameters:
  • condition (str) – The condition to match on e.g. Is, LessThan, InRange, Contains.

  • expected (GenericAlias[Any] | None (default: None)) – Optional list of expected values to match on. Types of the values in this list are automatically converted to the type of the item field’s value.

  • field (Field | None (default: None)) – The field to match on.

  • reference_required (bool (default: False)) – When True, a reference object of type T must be passed to the compare method. When False, reference files given to the compare method will be ignored. An exception will be raised if this is True and reference object is not passed.

Attributes:

condition

String representation of the current condition name of this object

expected

A list of expected values used for most conditions

field

The Field representing the property to extract the comparison value from when an MusifyItem is given

reference_required

Whether to raise an exception when compare() is called and a reference object is not provided.

Methods:

compare(item[, reference])

Compare a item to a reference or, if no reference is given, to this object's list of expected values

as_dict()

Return a dictionary representation of the key attributes of this object.

property condition: str

String representation of the current condition name of this object

property expected: list[Any] | None

A list of expected values used for most conditions

field: Field | None

The Field representing the property to extract the comparison value from when an MusifyItem is given

reference_required

Whether to raise an exception when compare() is called and a reference object is not provided.

compare(item, reference=None)

Compare a item to a reference or, if no reference is given, to this object’s list of expected values

Returns:

bool – True if a match is found, False otherwise.

Raises:

LocalProcessorError – If no reference given and no expected values set for this comparer.

as_dict()

Return a dictionary representation of the key attributes of this object.

The results of this function are used to produce the following:
  • A JSON representation of the object when calling json()

  • The string representation of the object when calling str() on the object

  • The representation of the object when calling repr() on the object