"""Abstractions of `tuning commas <https://en.wikipedia.org/wiki/Comma_(music)>`_The tuning commas are helpful for :class:`mutwo.parameters.pitches.JustIntonationPitch`which owns a :attr:`commas` attribute. By default :mod:`mutwo` makes use of commasdefined by the`Helmholtz-Ellis JI Pitch Notation <https://marsbat.space/pdfs/notation.pdf>`_."""importfunctoolsimportoperatorimporttypingtry:importquicktionsasfractions# type: ignoreexceptImportError:importfractions# type: ignore__all__=("Comma","CommaCompound")
[docs]classCommaCompound(typing.Iterable[Comma]):"""Collection of tuning commas."""# basically a frozen dict?def__init__(self,prime_to_exponent_dict:dict[int,int],prime_to_comma_dict:typing.Optional[dict[int,Comma]],):# TODO(make sure all primes in 'prime_to_exponent_dict' are also in# 'prime_to_comma_dict')self._prime_to_exponent_dict=prime_to_exponent_dictself._prime_to_comma_dict=prime_to_comma_dictdef__repr__(self)->str:return"{}({})".format(type(self).__name__,self._prime_to_exponent_dict)def__len__(self)->int:returnsum((abs(exponent)forexponentinself._prime_to_exponent_dict.values()))def__iter__(self):return(self._prime_to_comma_dict[prime].ratio**exponentforprime,exponentinself._prime_to_exponent_dict.items())@propertydefprime_to_exponent_dict(self)->dict[int,int]:returndict(self._prime_to_exponent_dict)@propertydefratio(self)->fractions.Fraction:ifself.prime_to_exponent_dict:returnfunctools.reduce(operator.mul,iter(self))else:returnfractions.Fraction(1,1)