Add more type hints, fix from_string for tonality

This commit is contained in:
Jonathan Harker 2024-10-02 08:31:50 +13:00
parent b0e0fe94ee
commit 929813c587

View file

@ -61,7 +61,7 @@ class SetClass(list):
return sum([hash(i) for i in self.pitch_classes])
@property
def pitch_classes(self) -> list:
def pitch_classes(self) -> list(int):
return list(self)
@cache_property
@ -83,7 +83,7 @@ class SetClass(list):
return sum(self.pitch_classes)
@cache_property
def adjacency_intervals(self) -> list:
def adjacency_intervals(self) -> list(int):
"""Adjacency intervals between the pitch classes, used for Leonard notation subscripts."""
if not self.pitch_classes:
return list()
@ -138,7 +138,7 @@ class SetClass(list):
return min(SetClass.ordered_interval(a, b), SetClass.ordered_interval(b, a))
@cache_property
def versions(self) -> list:
def versions(self) -> list(SetClass):
"""
Returns all possible zero-normalised versions (clock rotations) of this set class,
sorted by brightness. See Rahn (1980) Set types, Tₙ
@ -309,12 +309,12 @@ class SetClass(list):
# Class methods -----------------------------------------------------------
@classmethod
def from_string(this, string: str) -> SetClass:
def from_string(this, string: str, tonality: int = 12) -> SetClass:
"""
Attempt to create a SetClass from any string containing a sequence of zero or more integers.
A useful convenience function, e.g. SetClass.from_string('{0,3,7,9}')
"""
return SetClass(*re.findall(r'\d+', string))
return SetClass(*re.findall(r'\d+', string), tonality=tonality)
@classmethod
def all_of_cardinality(cls, cardinality: int, tonality: int = 12) -> set:
@ -363,7 +363,7 @@ class SetClass(list):
"""
cases = set()
for C in range(13): # 0 to 12
for sc in SetClass.all_of_cardinality(C):
for sc in this.all_of_cardinality(C):
if sc.darkest_form.brightness < sc.rahn_normal_form.brightness:
cases.add((C, sc.darkest_form, sc.rahn_normal_form))
return cases
@ -379,7 +379,7 @@ class SetClass(list):
"""
cases = set()
for C in range(13): # 0 to 12
for sc in SetClass.all_of_cardinality(C):
for sc in this.all_of_cardinality(C):
if sc.darkest_form.brightness < sc.prime_form.brightness:
cases.add((C, sc.darkest_form, sc.prime_form))
return cases