musicscore.accidental

class musicscore.accidental.Accidental(mode='standard', show: bool | None = None, **kwargs)[source]

Bases: MusicTree, XMLWrapper

Parent type: Midi

Child type: None

Accidental is the class for managing musicscore.midi.Midi’s accidental sign and its pitch parameters: step, alter, octave. The parameter mode (‘standard’, ‘enharmonic’, ‘flat’, ‘sharp’, ‘force-flat’, ‘force-sharp’) can be used to set different enharmonic variants of the same pitch.

XMLClass

alias of XMLAccidental

add_child(child: T) T

Tree method

Check and add child to list of children. Child’s parent is set to self.

Parameters:

child

Returns:

child

Return type:

Tree

classmethod create_tree_from_list(tree_list_representation: list[list[Any]], represented_attribute_names: list[str]) T
filter_nodes(key: Callable[[T], Any], return_value: Any) list[T]

Tree method

>>> root.filter_nodes(lambda node: node.get_level(), 2)
[grandchild1, grandchild2, grandchild3]
get_children() list[T]

Tree method

Returns:

list of added children.

get_children_of_type(type_: type) list[T]

Tree method

Returns:

list of added children of type.d

Return type:

list[Tree]

get_distance(reference: T | None = None) int
>>> root.get_distance()
0
>>> greatgrandchild1.get_distance()
3
>>> greatgrandchild1.get_distance(child2)
2
get_farthest_leaf() T
>>> root.get_farthest_leaf()
greatgrandchild1
get_layer(level: int, key: Callable[[T], Any] | None = None) Any

Tree method

Parameters:
  • level – layer number where 0 is the root.

  • key – An optional callable for each node in the layer.

Returns:

All nodes on this level. The leaves of branches which are shorter than the given level will be repeated on this and all following layers.

Return type:

list

get_leaves(key: Callable[[T], Any] | None = None) list[Union[Any, list[Any]]]

Tree method

Parameters:

key – An optional callable to be called on each leaf.

Returns:

nested list of leaves or values of key(leaf) for each leaf

>>> root.get_leaves()
[child1, [[greatgrandchild1, greatgrandchild2], grandchild2], child3, [grandchild3]]
get_level() int

Tree

Returns:

0 for root, 1, 2 etc. for each layer of children

Return type:

nonnegative int

>>> root.get_level()
0
>>> child1.get_level()
1
>>> grandchild1.get_level()
2
>>> greatgrandchild1.get_level()
3
get_list_representation(key: Callable[[Tree[T]], Any]) list[list[Any]]
get_number_of_layers() int
>>> root.get_number_of_layers()
3
get_parent() T | None

Tree method

Returns:

parent. None for root.

Return type:

Tree

get_pitch_parameters(midi_value: int | float | None = None) tuple | None[source]
Param:

a valid midi value or None. If midi_value == 0 (for a rest) return value is None. If midi_value is None and a parent_midi exists, parent_midi’s value will be used.

Returns:

A tuple consisting of pitch step name, alter value and octave value: (step, alter, octave)

See also

mode

get_position_in_tree() str

Tree method

Returns:

0 for root. 1, 2, … for layer 1. Other layers: x.y.z…. Example: 3.2.2 => third child of secod child of second child of the root.

Return type:

str

>>> print(root.get_tree_representation(key=lambda node: node.get_position_in_tree()))
└── 0
    ├── 1
    ├── 2
    │   ├── 2.1
    │   │   ├── 2.1.1
    │   │   └── 2.1.2
    │   └── 2.2
    ├── 3
    └── 4
        └── 4.1
get_reversed_path_to_root() list[T]

Tree method

Returns:

path from self upwards through all ancestors up to the root.

>>> greatgrandchild1.get_reversed_path_to_root()
[greatgrandchild1, grandchild1, child2, root]
get_root() T

Tree method

Returns:

root (upmost node of a tree which has no parent)

Return type:

Tree

>>> greatgrandchild1.get_root() == root
True
>>> child4.get_root() == root
True
>>> root.get_root() == root
True
get_self_with_key(key: Callable[[T], Any] | None = None) Any
get_tree_representation(key: Callable[[T], Any] | None = None, space: int = 3) str

Tree method

Parameters:

key – An optional callable if None string(node) is called.

Returns:

a representation of all nodes as string in tree form.

>>> print(root.get_tree_representation())
└── root
    ├── child1
    ├── child2
    │   ├── grandchild1
    │   │   ├── greatgrandchild1
    │   │   └── greatgrandchild2
    │   └── grandchild2
    ├── child3
    └── child4
        └── grandchild3
iterate_leaves() Iterator[T]

Tree method

Returns:

A generator iterating over all leaves.

remove(child: T) None

Tree method

Child’s parent will be set to None and child will be removed from list of children.

Parameters:

child

Returns:

None

remove_children() None

Tree method

Calls remove() on all children.

Returns:

None

replace_child(old: T, new: T, index: int = 0) None

Tree method

Parameters:
  • old – child or function

  • new – child

  • index – index of old child in the list of its appearances

Returns:

None

to_string(*args, **kwargs) str

XMLWrapper method

Calls to_string method of self.xml_object

Returns:

musicxml snippet

traverse() Iterator[T]

Tree method

Traverse all tree nodes.

Returns:

generator

property content: Any
default_show_accidental_signs = 'modern'
property is_first_child: bool
property is_last_child: bool
>>> t = TestTree('root')
>>> for node in t.traverse():
...    if node.name in ['root', 'child4', 'grandchild2', 'grandchild3', 'greatgrandchild1']:
...        assert node.is_last_child
...    else:
...        assert not node.is_last_child
property is_leaf: bool

Tree property

Returns:

True if self has no children. False if self has one or more children.

Return type:

bool

property is_root: bool

Tree property

Returns:

True if self has no parent, else False.

Return type:

bool

property mode

permitted modes: standard, flat, sharp, enharmonic, force-sharp, force-flat

Returns:

accidental mode which corresponds to global variables STANDARD, FLAT, SHARP, ENHARMONIC, FORCESHARP, FORCEFLAT

property next: T | None

Tree property

Returns:

next sibling. None if this is the last current child of the parent.

Return type:

Tree

property parent_midi: Midi
Returns:

The midi parent of Accidental in the MusicTree structure. It is equivalent to up or get_parent()

Return type:

musicscore.midi.Midi

property previous: T | None

Tree property

Returns:

previous sibling. None if this is the first child of the parent.

Return type:

Tree

property show: bool

If False xml_object will be None and no accidental is shown in the Score. If True the accidental is shown. If the value of one of the following xml_object XMLAccidental properties:self.parentheses, self.editorial. self.cautionary or self.bracket is set to yes, the accidental is always shown regardless of this property

property show_accidental_signs: str

MusicTree property

  • If show_accidental_signs is set to None the first get_quantized of ancestors which is False or True will be returned.

  • If show_accidental_signs is set to None it will be converted to default_show_accidental_signs

  • Possible show_accidental_signs are: None, ‘modern’, ‘traditional’

Type:

Optional[str]

Return type:

str

property sign: str | None

Converts alter parameter of get_pitch_parameters into an actual sign depending on mode. parent_midi must be set first.

Returns:

Possible values: flat-flat, three-quarters-flat, flat, quarter-flat, natural, quarter-sharp, sharp, three-quarters-sharp, double-sharp

property up: T | None

Tree property

Returns:

get_parent()

Return type:

Tree

property xml_object: XMLAccidental | None

If an attribute is not found directly in a MusicTree class which inherits this class, it wll be passed on to __get_attribute__ and __set__attribute__ methods of xml_object. It can also use the short cut attributes of XMLElement to get or set the first child.

Returns:

wrapped MusicXML element of type XMLClass

musicscore.accidental.ENHARMONIC = {0: ('C', 0, 0), 0.5: ('D', -1.5, 0), 1: ('D', -1, 0), 1.5: ('D', -0.5, 0), 2: ('D', 0, 0), 2.5: ('D', 0.5, 0), 3: ('D', 1, 0), 3.5: ('D', 1.5, 0), 4: ('E', 0, 0), 4.5: ('F', -0.5, 0), 5: ('F', 0, 0), 5.5: ('G', -1.5, 0), 6: ('G', -1, 0), 6.5: ('G', -0.5, 0), 7: ('G', 0, 0), 7.5: ('G', 0.5, 0), 8: ('G', 1, 0), 8.5: ('G', 1.5, 0), 9: ('A', 0, 0), 9.5: ('A', 0.5, 0), 10: ('A', 1, 0), 10.5: ('A', 1.5, 0), 11: ('B', 0, 0), 11.5: ('B', 0.5, 0)}
musicscore.accidental.FLAT = {0: ('C', 0, 0), 0.5: ('D', -1.5, 0), 1: ('D', -1, 0), 1.5: ('D', -0.5, 0), 2: ('D', 0, 0), 2.5: ('E', -1.5, 0), 3: ('E', -1, 0), 3.5: ('E', -0.5, 0), 4: ('E', 0, 0), 4.5: ('F', -0.5, 0), 5: ('F', 0, 0), 5.5: ('G', -1.5, 0), 6: ('G', -1, 0), 6.5: ('G', -0.5, 0), 7: ('G', 0, 0), 7.5: ('A', -1.5, 0), 8: ('A', -1, 0), 8.5: ('A', -0.5, 0), 9: ('A', 0, 0), 9.5: ('B', -1.5, 0), 10: ('B', -1, 0), 10.5: ('B', -0.5, 0), 11: ('B', 0, 0), 11.5: ('C', -0.5, 1)}
musicscore.accidental.FORCEFLAT = {0: ('D', -2, 0), 0.5: ('D', -1.5, 0), 1: ('D', -1, 0), 1.5: ('D', -0.5, 0), 2: ('E', -2, 0), 2.5: ('E', -1.5, 0), 3: ('F', -2, 0), 3.5: ('F', -1.5, 0), 4: ('F', -1, 0), 4.5: ('F', -0.5, 0), 5: ('G', -2, 0), 5.5: ('G', -1.5, 0), 6: ('G', -1, 0), 6.5: ('G', -0.5, 0), 7: ('A', -2, 0), 7.5: ('A', -1.5, 0), 8: ('A', -1, 0), 8.5: ('A', -0.5, 0), 9: ('B', -2, 0), 9.5: ('B', -1.5, 0), 10: ('C', -2, 1), 10.5: ('C', -1.5, 1), 11: ('C', -1, 1), 11.5: ('C', -0.5, 1)}
musicscore.accidental.FORCESHARP = {0: ('B', 1, -1), 0.5: ('B', 1.5, -1), 1: ('B', 2, -1), 1.5: ('C', 1.5, 0), 2: ('C', 2, 0), 2.5: ('D', 0.5, 0), 3: ('D', 1, 0), 3.5: ('D', 1.5, 0), 4: ('D', 2, 0), 4.5: ('E', 0.5, 0), 5: ('E', 1, 0), 5.5: ('E', 1.5, 0), 6: ('E', 2, 0), 6.5: ('F', 1.5, 0), 7: ('F', 2, 0), 7.5: ('G', 0.5, 0), 8: ('G', 1, 0), 8.5: ('G', 1.5, 0), 9: ('G', 2, 0), 9.5: ('A', 0.5, 0), 10: ('A', 1, 0), 10.5: ('A', 1.5, 0), 11: ('A', 2, 0), 11.5: ('B', 0.5, 0)}
musicscore.accidental.SHARP = {0: ('C', 0, 0), 0.5: ('C', 0.5, 0), 1: ('C', 1, 0), 1.5: ('C', 1.5, 0), 2: ('D', 0, 0), 2.5: ('D', 0.5, 0), 3: ('D', 1, 0), 3.5: ('D', 1.5, 0), 4: ('E', 0, 0), 4.5: ('E', 0.5, 0), 5: ('F', 0, 0), 5.5: ('F', 0.5, 0), 6: ('F', 1, 0), 6.5: ('F', 1.5, 0), 7: ('G', 0, 0), 7.5: ('G', 0.5, 0), 8: ('G', 1, 0), 8.5: ('G', 1.5, 0), 9: ('A', 0, 0), 9.5: ('A', 0.5, 0), 10: ('A', 1, 0), 10.5: ('A', 1.5, 0), 11: ('B', 0, 0), 11.5: ('B', 0.5, 0)}
musicscore.accidental.SIGNS = {-2: 'flat-flat', -1.5: 'three-quarters-flat', -1: 'flat', -0.5: 'quarter-flat', 0: 'natural', 0.5: 'quarter-sharp', 1: 'sharp', 1.5: 'three-quarters-sharp', 2: 'double-sharp'}
musicscore.accidental.STANDARD = {0: ('C', 0, 0), 0.5: ('C', 0.5, 0), 1: ('C', 1, 0), 1.5: ('C', 1.5, 0), 2: ('D', 0, 0), 2.5: ('E', -1.5, 0), 3: ('E', -1, 0), 3.5: ('E', -0.5, 0), 4: ('E', 0, 0), 4.5: ('E', 0.5, 0), 5: ('F', 0, 0), 5.5: ('F', 0.5, 0), 6: ('F', 1, 0), 6.5: ('F', 1.5, 0), 7: ('G', 0, 0), 7.5: ('A', -1.5, 0), 8: ('A', -1, 0), 8.5: ('A', -0.5, 0), 9: ('A', 0, 0), 9.5: ('B', -1.5, 0), 10: ('B', -1, 0), 10.5: ('B', -0.5, 0), 11: ('B', 0, 0), 11.5: ('C', -0.5, 1)}