Source code for orbichord.chord

"""Implement identified chords."""

from music21.chord import Chord
from typing import Callable


[docs]class IdentifiedChord(Chord): """Extend Chord to be a hashable object. The hash is contructed from a identity function that map a chord in to a string. Parameters ---------- identify : Callable[[Chord], str], optional Funtion to indentify chords. notes : Argument pass to chord constructor. keywords : Argument pass to chord constructor. Examples -------- >>> from orbichord.chord import IdentifiedChord >>> C = IdentifiedChord(notes = 'C4 E4 G4') >>> print(C.orderedPitchClassesString) <047> >>> print(C.identity) <047> >>> print(C.identify(C)) <047> >>> print(hash(C)) 5261361699489349224 """ def __init__(self, identify : Callable[[Chord], str] = \ lambda chord: chord.orderedPitchClassesString, notes=None, **keywords ): """Constructor""" super().__init__(notes, **keywords) self._identify = identify
[docs] def __hash__(self): """Return a has of the string.""" return hash(self._identify(self))
[docs] def __eq__(self, other): """Overload comparison based hashable implementation.""" if not isinstance(other, IdentifiedChord): return False return hash(self) == hash(other)
@property def identify(self): """Return identify function.""" return self._identify @property def identity(self): """Return identity string.""" return self._identify(self)