Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _break_up_large_numbers(self):
for i, group in enumerate(self.groups):
if isinstance(group, int):
if group > 3:
self.groups[i] = MetricStructure(*_decompose_to_twos_and_threes(group))
else:
self.groups[i]._break_up_large_numbers()
return self
current_group_length = 1
for beat_scheme in self.beat_schemes[1:]:
if beat_scheme.length == last_beat_length:
current_group_length += 1
else:
groups.append(current_group_length)
current_group_length = 1
last_beat_length = beat_scheme.length
groups.append(current_group_length)
# for each group make a metric layer out of the prime-factored length, breaking up large primes
# (also, it's possible for a group to be one beat long, in which case it has empty prime factors. In this
# case, we just need to use a MetricStructure(1)
return MetricStructure(*(
MetricStructure.from_string("*".join(str(x) for x in sorted(prime_factor(group))), True)
if group != 1 else MetricStructure(1)
for group in groups
))
def join(self, other_metric_structure) -> 'MetricStructure':
"""
Creates a new MetricStructure with two groups: this MetricStructure and other_metric_structure. For example,
`MetricStructure(3, 3).join(MetricStructure(2, 2))` results in
`MetricStructure(MetricStructure(3, 3), MetricStructure(2, 2))`.
:param other_metric_structure: another MetricStructure
"""
return MetricStructure(self, other_metric_structure)
def to_metric_structure(self, break_up_large_numbers: bool = False) -> 'MetricStructure':
"""
Renders this arithmetic group as a :class:`MetricStructure`.
:param break_up_large_numbers: see :class:`MetricStructure`
"""
if self.operation is None:
return MetricStructure(self.elements[0], break_up_large_numbers=break_up_large_numbers)
elif self.operation == "+":
return MetricStructure(*[x.to_metric_structure(break_up_large_numbers) for x in self.elements],
break_up_large_numbers=break_up_large_numbers)
else:
return functools.reduce(operator.mul, (x.to_metric_structure(break_up_large_numbers) for x in self.elements))
def __init__(self, *groups: Sequence[Union[int, Sequence, 'MetricStructure']],
break_up_large_numbers: bool = False):
if not all(isinstance(x, (int, list, tuple, MetricStructure)) for x in groups):
raise ValueError("Groups must either be integers, list/tuples or MetricStructures themselves")
self.groups = [MetricStructure(*x) if isinstance(x, (tuple, list)) else x for x in groups]
if break_up_large_numbers:
self._break_up_large_numbers()
self._remove_redundant_nesting()
def __mul__(self, other):
if isinstance(other, int):
return self * MetricStructure(other)
else:
return MetricStructure(*(group * other for group in self.groups))
def _increment_nested_list(l, increment):
for i, element in enumerate(l):
if isinstance(element, list):
MetricStructure._increment_nested_list(element, increment)
else:
l[i] += increment
return l