def test_rhythmtreetools_RhythmTreeNode_duration_01(): tree = rhythmtreetools.RhythmTreeContainer(preprolated_duration=1, children=[ rhythmtreetools.RhythmTreeLeaf(preprolated_duration=1), rhythmtreetools.RhythmTreeContainer(preprolated_duration=2, children=[ rhythmtreetools.RhythmTreeLeaf(preprolated_duration=3), rhythmtreetools.RhythmTreeLeaf(preprolated_duration=2) ]), rhythmtreetools.RhythmTreeLeaf(preprolated_duration=2) ]) assert tree.duration == abjad.Duration(1) assert tree[0].duration == abjad.Duration(1, 5) assert tree[1].duration == abjad.Duration(2, 5) assert tree[1][0].duration == abjad.Duration(6, 25) assert tree[1][1].duration == abjad.Duration(4, 25) assert tree[2].duration == abjad.Duration(2, 5) tree[1].append(tree.pop()) assert tree.duration == abjad.Duration(1) assert tree[0].duration == abjad.Duration(1, 3) assert tree[1].duration == abjad.Duration(2, 3) assert tree[1][0].duration == abjad.Duration(2, 7) assert tree[1][1].duration == abjad.Duration(4, 21) assert tree[1][2].duration == abjad.Duration(4, 21) tree.preprolated_duration = 19 assert tree.duration == abjad.Duration(19) assert tree[0].duration == abjad.Duration(19, 3) assert tree[1].duration == abjad.Duration(38, 3) assert tree[1][0].duration == abjad.Duration(38, 7) assert tree[1][1].duration == abjad.Duration(76, 21) assert tree[1][2].duration == abjad.Duration(76, 21)
def test_rhythmtreetools_RhythmTreeContainer___eq___03(): a = rhythmtreetools.RhythmTreeContainer(children=[]) b = rhythmtreetools.RhythmTreeContainer(preprolated_duration=2, children=[]) c = rhythmtreetools.RhythmTreeContainer( preprolated_duration=1, children=[rhythmtreetools.RhythmTreeLeaf(preprolated_duration=1)]) d = rhythmtreetools.RhythmTreeContainer( preprolated_duration=2, children=[rhythmtreetools.RhythmTreeLeaf(preprolated_duration=1)]) e = rhythmtreetools.RhythmTreeContainer( preprolated_duration=2, children=[rhythmtreetools.RhythmTreeLeaf(preprolated_duration=2)]) assert a != b assert a != c assert a != d assert a != e assert b != c assert b != d assert b != e assert c != d assert c != e assert d != e
def test_rhythmtreetools_RhythmTreeContainer___eq___01(): a = rhythmtreetools.RhythmTreeContainer(children=[]) b = rhythmtreetools.RhythmTreeContainer(children=[]) assert format(a) == format(b) assert a != b
def test_rhythmtreetools_RhythmTreeNode_depth_01(): container = rhythmtreetools.RhythmTreeContainer() assert container.depth == 0 leaf = rhythmtreetools.RhythmTreeLeaf() assert leaf.depth == 0 container.append(leaf) assert leaf.depth == 1 subcontainer = rhythmtreetools.RhythmTreeContainer() assert subcontainer.depth == 0 container.append(subcontainer) assert subcontainer.depth == 1 subcontainer.append(leaf) assert leaf.depth == 2 subsubcontainer = rhythmtreetools.RhythmTreeContainer() assert subsubcontainer.depth == 0 subcontainer.append(subsubcontainer) assert subsubcontainer.depth == 2 subsubcontainer.append(leaf) assert leaf.depth == 3
def test_rhythmtreetools_RhythmTreeContainer___eq___02(): a = rhythmtreetools.RhythmTreeContainer( children=[rhythmtreetools.RhythmTreeLeaf()]) b = rhythmtreetools.RhythmTreeContainer( children=[rhythmtreetools.RhythmTreeLeaf()]) assert a == b
def test_rhythmtreetools_RhythmTreeContainer_children_01(): leaf_a = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=3) leaf_b = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=3) leaf_c = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=2) subcontainer = rhythmtreetools.RhythmTreeContainer( preprolated_duration=2, children=[leaf_b, leaf_c]) leaf_d = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=1) container = rhythmtreetools.RhythmTreeContainer( preprolated_duration=1, children=[leaf_a, subcontainer, leaf_d]) assert container.children == (leaf_a, subcontainer, leaf_d)
def test_rhythmtreetools_RhythmTreeContainer_rtm_format_01(): leaf_a = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=3) leaf_b = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=3) leaf_c = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=2) subcontainer = rhythmtreetools.RhythmTreeContainer( preprolated_duration=2, children=[leaf_b, leaf_c]) leaf_d = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=1) container = rhythmtreetools.RhythmTreeContainer( preprolated_duration=1, children=[leaf_a, subcontainer, leaf_d]) assert subcontainer.rtm_format == '(2 (3 2))' assert container.rtm_format == '(1 (3 (2 (3 2)) 1))'
def test_rhythmtreetools_RhythmTreeNode_improper_parentage_01(): container = rhythmtreetools.RhythmTreeContainer() assert container.improper_parentage == (container, ) leaf = rhythmtreetools.RhythmTreeLeaf() assert leaf.improper_parentage == (leaf, ) container.append(leaf) assert leaf.improper_parentage == (leaf, container) subcontainer = rhythmtreetools.RhythmTreeContainer(preprolated_duration=1, children=[leaf]) container.append(subcontainer) assert leaf.improper_parentage == (leaf, subcontainer, container)
def test_rhythmtreetools_RhythmTreeContainer___contains___01(): leaf_a = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=1) leaf_b = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=1) leaf_c = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=1) subcontainer = rhythmtreetools.RhythmTreeContainer(preprolated_duration=1, children=[leaf_b]) container = rhythmtreetools.RhythmTreeContainer( preprolated_duration=1, children=[leaf_a, subcontainer]) assert leaf_a in container assert leaf_b not in container assert leaf_c not in container
def test_rhythmtreetools_RhythmTreeNode_root_01(): leaf = rhythmtreetools.RhythmTreeLeaf() subsubcontainer = rhythmtreetools.RhythmTreeContainer() subcontainer = rhythmtreetools.RhythmTreeContainer() container = rhythmtreetools.RhythmTreeContainer() container.append(subcontainer) subcontainer.append(subsubcontainer) subsubcontainer.append(leaf) assert leaf.root == container assert subsubcontainer.root == container assert subcontainer.root == container assert container.root == container
def test_rhythmtreetools_RhythmTreeNode_proper_parentage_01(): leaf = rhythmtreetools.RhythmTreeLeaf() subsubcontainer = rhythmtreetools.RhythmTreeContainer() subcontainer = rhythmtreetools.RhythmTreeContainer() container = rhythmtreetools.RhythmTreeContainer() container.append(subcontainer) subcontainer.append(subsubcontainer) subsubcontainer.append(leaf) assert leaf.proper_parentage == (subsubcontainer, subcontainer, container) assert subsubcontainer.proper_parentage == (subcontainer, container) assert subcontainer.proper_parentage == (container, ) assert container.proper_parentage == ()
def test_rhythmtreetools_RhythmTreeContainer___init___03(): leaf_a = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=1) leaf_b = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=2) leaf_c = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=1) assert leaf_a.start_offset == 0 assert leaf_a.parent is None assert leaf_b.start_offset == 0 assert leaf_b.parent is None assert leaf_c.start_offset == 0 assert leaf_c.parent is None container = rhythmtreetools.RhythmTreeContainer( preprolated_duration=4, children=[leaf_a, leaf_b, leaf_c]) assert container.children == (leaf_a, leaf_b, leaf_c) assert container.preprolated_duration == 4 assert container.start_offset == 0 assert container.parent is None assert leaf_a.start_offset == 0 assert leaf_a.parent is container assert leaf_b.start_offset == 1 assert leaf_b.parent is container assert leaf_c.start_offset == 3 assert leaf_c.parent is container
def test_rhythmtreetools_RhythmTreeContainer___init___01(): container = rhythmtreetools.RhythmTreeContainer() assert container.children == () assert container.preprolated_duration == 1 assert container.start_offset == 0 assert container.parent is None
def test_rhythmtreetools_RhythmTreeContainer_index_01(): leaf_a = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=3) leaf_b = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=3) leaf_c = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=2) subcontainer = rhythmtreetools.RhythmTreeContainer( preprolated_duration=2, children=[leaf_b, leaf_c]) leaf_d = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=1) container = rhythmtreetools.RhythmTreeContainer( preprolated_duration=1, children=[leaf_a, subcontainer, leaf_d]) assert container.index(leaf_a) == 0 assert container.index(subcontainer) == 1 assert container.index(leaf_d) == 2 pytest.raises(ValueError, 'container.index(leaf_b)') pytest.raises(ValueError, 'container.index(leaf_c)')
def p_container__LPAREN__DURATION__node_list_closed__RPAREN(self, p): r'''container : LPAREN DURATION node_list_closed RPAREN ''' from abjad.tools import rhythmtreetools p[0] = rhythmtreetools.RhythmTreeContainer( children=p[3], preprolated_duration=abs(p[2]), )
def test_rhythmtreetools_RhythmTreeContainer___iter___01(): leaf_a = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=3) leaf_b = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=2) leaf_c = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=1) container = rhythmtreetools.RhythmTreeContainer( preprolated_duration=1, children=[leaf_a, leaf_b, leaf_c]) assert [x for x in container] == [leaf_a, leaf_b, leaf_c]
def test_rhythmtreetools_RhythmTreeNode_offset_01(): tree = rhythmtreetools.RhythmTreeContainer(preprolated_duration=1, children=[ rhythmtreetools.RhythmTreeLeaf(preprolated_duration=1), rhythmtreetools.RhythmTreeContainer(preprolated_duration=2, children=[ rhythmtreetools.RhythmTreeLeaf(preprolated_duration=3), rhythmtreetools.RhythmTreeLeaf(preprolated_duration=2) ]), rhythmtreetools.RhythmTreeLeaf(preprolated_duration=2) ]) assert tree.start_offset == abjad.Offset(0) assert tree[0].start_offset == abjad.Offset(0) assert tree[1].start_offset == abjad.Offset(1, 5) assert tree[1][0].start_offset == abjad.Offset(1, 5) assert tree[1][1].start_offset == abjad.Offset(11, 25) assert tree[2].start_offset == abjad.Offset(3, 5) node = tree.pop() assert node.start_offset == abjad.Offset(0) tree[1].append(node) assert tree.start_offset == abjad.Offset(0) assert tree[0].start_offset == abjad.Offset(0) assert tree[1].start_offset == abjad.Offset(1, 3) assert tree[1][0].start_offset == abjad.Offset(1, 3) assert tree[1][1].start_offset == abjad.Offset(13, 21) assert tree[1][2].start_offset == abjad.Offset(17, 21) tree.preprolated_duration = 19 assert tree.start_offset == abjad.Offset(0) assert tree[0].start_offset == abjad.Offset(0) assert tree[1].start_offset == abjad.Offset(19, 3) assert tree[1][0].start_offset == abjad.Offset(19, 3) assert tree[1][1].start_offset == abjad.Offset(247, 21) assert tree[1][2].start_offset == abjad.Offset(323, 21)
def test_rhythmtreetools_RhythmTreeNode_parent_01(): leaf = rhythmtreetools.RhythmTreeLeaf() container = rhythmtreetools.RhythmTreeContainer() subcontainer = rhythmtreetools.RhythmTreeContainer() assert leaf.parent is None assert container.parent is None assert subcontainer.parent is None container.append(leaf) assert leaf.parent is container container.append(subcontainer) assert subcontainer.parent is container assert leaf.parent is container assert container.parent is None subcontainer.append(leaf) assert leaf.parent is subcontainer assert subcontainer.parent is container assert container.parent is None pytest.raises(AssertionError, 'subcontainer.append(container)')
def test_rhythmtreetools_RhythmTreeContainer_insert_01(): leaf_a = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=3) leaf_b = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=3) leaf_c = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=2) container = rhythmtreetools.RhythmTreeContainer() assert container.children == () container.insert(0, leaf_a) assert container.children == (leaf_a,) container.insert(0, leaf_b) assert container.children == (leaf_b, leaf_a) container.insert(1, leaf_c) assert container.children == (leaf_b, leaf_c, leaf_a)
def test_rhythmtreetools_RhythmTreeContainer_extend_01(): leaf_a = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=3) leaf_b = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=3) leaf_c = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=2) leaf_d = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=1) container = rhythmtreetools.RhythmTreeContainer() assert container.children == () container.extend([leaf_a]) assert container.children == (leaf_a, ) container.extend([leaf_b, leaf_c, leaf_d]) assert container.children == (leaf_a, leaf_b, leaf_c, leaf_d) container.extend([leaf_a, leaf_c]) assert container.children == (leaf_b, leaf_d, leaf_a, leaf_c)
def test_rhythmtreetools_RhythmTreeContainer_remove_01(): leaf_a = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=3) leaf_b = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=3) leaf_c = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=2) container = rhythmtreetools.RhythmTreeContainer( preprolated_duration=1, children=[leaf_a, leaf_b, leaf_c]) assert container.children == (leaf_a, leaf_b, leaf_c) assert leaf_a.parent is container assert leaf_b.parent is container assert leaf_c.parent is container container.remove(leaf_a) assert container.children == (leaf_b, leaf_c) assert leaf_a.parent is None container.remove(leaf_c) assert container.children == (leaf_b,) assert leaf_c.parent is None
def test_rhythmtreetools_RhythmTreeContainer___getitem___01(): leaf_a = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=3) leaf_b = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=2) leaf_c = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=1) container = rhythmtreetools.RhythmTreeContainer( preprolated_duration=1, children=[leaf_a, leaf_b, leaf_c]) assert container[0] is leaf_a assert container[1] is leaf_b assert container[2] is leaf_c pytest.raises(Exception, 'container[3]') assert container[-1] is leaf_c assert container[-2] is leaf_b assert container[-3] is leaf_a pytest.raises(Exception, 'container[-4]')
def test_rhythmtreetools_RhythmTreeContainer_pop_01(): leaf_a = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=3) leaf_b = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=3) leaf_c = rhythmtreetools.RhythmTreeLeaf(preprolated_duration=2) container = rhythmtreetools.RhythmTreeContainer( preprolated_duration=1, children=[leaf_a, leaf_b, leaf_c]) assert container.children == (leaf_a, leaf_b, leaf_c) assert leaf_a.parent is container assert leaf_b.parent is container assert leaf_c.parent is container result = container.pop() assert container.children == (leaf_a, leaf_b) assert result is leaf_c assert result.parent is None result = container.pop(0) assert container.children == (leaf_b, ) assert result is leaf_a assert result.parent is None
def recurse( node, factors, denominator, decrease_durations_monotonically, ): if factors: factor, factors = factors[0], factors[1:] preprolated_duration = \ node.preprolated_duration.__div__(factor) #if factor in (2, 3, 4, 5): if factor in (2, 3, 4): if factors: for _ in range(factor): child = rhythmtreetools.RhythmTreeContainer( preprolated_duration=preprolated_duration) node.append(child) recurse( child, factors, denominator, decrease_durations_monotonically, ) else: for _ in range(factor): node.append( rhythmtreetools.RhythmTreeLeaf( preprolated_duration=(1, denominator))) else: parts = [3] total = 3 while total < factor: if decrease_durations_monotonically: parts.append(2) else: parts.insert(0, 2) total += 2 for part in parts: grouping = rhythmtreetools.RhythmTreeContainer( preprolated_duration=part * preprolated_duration) if factors: for _ in range(part): child = rhythmtreetools.RhythmTreeContainer( preprolated_duration=preprolated_duration) grouping.append(child) recurse( child, factors, denominator, decrease_durations_monotonically, ) else: for _ in range(part): grouping.append( rhythmtreetools.RhythmTreeLeaf( preprolated_duration=(1, denominator))) node.append(grouping) else: node.extend([ rhythmtreetools.RhythmTreeLeaf( preprolated_duration=(1, denominator)) for _ in range(node.preprolated_duration.numerator) ])
def __init__( self, arg=None, decrease_durations_monotonically=True, preferred_boundary_depth=None, ): arg = arg or (4, 4) assert isinstance(preferred_boundary_depth, (int, type(None))) self._preferred_boundary_depth = preferred_boundary_depth def recurse( node, factors, denominator, decrease_durations_monotonically, ): if factors: factor, factors = factors[0], factors[1:] preprolated_duration = \ node.preprolated_duration.__div__(factor) #if factor in (2, 3, 4, 5): if factor in (2, 3, 4): if factors: for _ in range(factor): child = rhythmtreetools.RhythmTreeContainer( preprolated_duration=preprolated_duration) node.append(child) recurse( child, factors, denominator, decrease_durations_monotonically, ) else: for _ in range(factor): node.append( rhythmtreetools.RhythmTreeLeaf( preprolated_duration=(1, denominator))) else: parts = [3] total = 3 while total < factor: if decrease_durations_monotonically: parts.append(2) else: parts.insert(0, 2) total += 2 for part in parts: grouping = rhythmtreetools.RhythmTreeContainer( preprolated_duration=part * preprolated_duration) if factors: for _ in range(part): child = rhythmtreetools.RhythmTreeContainer( preprolated_duration=preprolated_duration) grouping.append(child) recurse( child, factors, denominator, decrease_durations_monotonically, ) else: for _ in range(part): grouping.append( rhythmtreetools.RhythmTreeLeaf( preprolated_duration=(1, denominator))) node.append(grouping) else: node.extend([ rhythmtreetools.RhythmTreeLeaf( preprolated_duration=(1, denominator)) for _ in range(node.preprolated_duration.numerator) ]) decrease_durations_monotonically = \ bool(decrease_durations_monotonically) if isinstance(arg, type(self)): root = arg.root_node numerator, denominator = arg.numerator, arg.denominator decrease_durations_monotonically = \ arg.decrease_durations_monotonically elif isinstance(arg, (str, rhythmtreetools.RhythmTreeContainer)): if isinstance(arg, str): parsed = rhythmtreetools.RhythmTreeParser()(arg) assert len(parsed) == 1 root = parsed[0] else: root = arg for node in root.nodes: assert node.prolation == 1 numerator = root.preprolated_duration.numerator denominator = root.preprolated_duration.denominator elif (isinstance(arg, (tuple, scoretools.Measure)) or (hasattr(arg, 'numerator') and hasattr(arg, 'denominator'))): if isinstance(arg, tuple): fraction = mathtools.NonreducedFraction(arg) elif isinstance(arg, scoretools.Measure): time_signature = arg._get_effective( indicatortools.TimeSignature) fraction = mathtools.NonreducedFraction( time_signature.numerator, time_signature.denominator) else: fraction = mathtools.NonreducedFraction( arg.numerator, arg.denominator) numerator, denominator = fraction.numerator, fraction.denominator factors = mathtools.factors(numerator) # group two nested levels of 2s into a 4 if 1 < len(factors) and factors[0] == factors[1] == 2: factors[0:2] = [4] root = rhythmtreetools.RhythmTreeContainer( preprolated_duration=fraction) recurse( root, factors, denominator, decrease_durations_monotonically, ) else: message = 'can not initialize {}: {!r}.' message = message.format(type(self).__name__, arg) raise ValueError(message) self._root_node = root self._numerator = numerator self._denominator = denominator self._decrease_durations_monotonically = \ decrease_durations_monotonically