Sort

Inheritance diagram of musify.processors.sort

Processor that sorts the given collection of items based on given configuration.

Classes:

ShuffleMode(value[, names, module, ...])

Represents the possible shuffle modes to use when shuffling items using ItemSorter.

ItemSorter([fields, shuffle_mode, ...])

Sort items in-place based on given conditions.

class musify.processors.sort.ShuffleMode(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: MusifyEnum

Represents the possible shuffle modes to use when shuffling items using ItemSorter.

Attributes:

RANDOM

HIGHER_RATING

RECENT_ADDED

DIFFERENT_ARTIST

RANDOM = 0
HIGHER_RATING = 1
RECENT_ADDED = 2
DIFFERENT_ARTIST = 3
class musify.processors.sort.ItemSorter(fields=(), shuffle_mode=None, shuffle_weight=0.0)

Bases: Processor

Sort items in-place based on given conditions.

fields may be:
  • List of tags/properties to sort by.

  • Map of {<tag/property>: <reversed>}. If reversed is true, sort the tag/property in reverse.

When shuffle_mode == HIGHER_RATING or RECENT_ADDED:
  • A shuffle_weight of 0 will sort the tracks in order according to the desired shuffle_mode.

  • A positive shuffle_weight shuffles according to the desired shuffle_mode. The shuffle_weight will determine how much randomness is applied to lower ranking items.

  • A negative shuffle_weight works as above but reverses the final sort order.

When shuffle_mode == DIFFERENT_ARTIST:
  • A shuffle_weight of 1 will group the tracks by artist, shuffling artists randomly.

  • A shuffle_weight of -1 will shuffle the items randomly.

Parameters:
  • fields (Union[Field, None, Sequence[Field | None], Mapping[Field | None, bool]] (default: ())) – Fields to sort by. If defined, this value will always take priority over any shuffle settings i.e. shuffle settings will be ignored.

  • shuffle_mode (ShuffleMode | None (default: None)) – The mode to use for shuffling. Only used when no fields are given. WARNING: Currently only RANDOM shuffle mode has been implemented. Any other given value will default to RANDOM shuffling.

  • shuffle_weight (float (default: 0.0)) – The weights (between -1 and 1) to apply to certain shuffling modes. This value will automatically be limited to within the valid range -1 and 1. Only used when no fields are given and shuffle_mode is not None or RANDOM.

Methods:

sort_by_field(items[, field, reverse])

Sort items by the values of a given field.

group_by_field(items[, field])

Group items by the values of a given field.

sort(items)

Sorts a list of items in-place.

as_dict()

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

Attributes:

sort_fields

shuffle_mode

shuffle_weight

classmethod sort_by_field(items, field=None, reverse=False)

Sort items by the values of a given field.

Parameters:
  • items (list[MusifyItem]) – List of items to sort

  • field (Field | None (default: None)) – Tag or property to sort on. If None and reverse is True, reverse the order of the list.

  • reverse (bool (default: False)) – If true, reverse the order of the sort.

Return type:

None

classmethod group_by_field(items, field=None)

Group items by the values of a given field.

Parameters:
  • items (Union[TypeVar(T, bound= MusifyItem), Iterable[TypeVar(T, bound= MusifyItem)]]) – List of items to sort.

  • field (Field | None (default: None)) – Tag or property to group by. None returns map of {None: <items>}.

Returns:

dict[Any, list[TypeVar(T, bound= MusifyItem)]] – Map of grouped items.

sort_fields: dict[Field | None, bool]
shuffle_mode
shuffle_weight
sort(items)

Sorts a list of items in-place.

Return type:

None

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