Skip to content

Commit

Permalink
core_utilites += del_nested_item_from_index_sequence
Browse files Browse the repository at this point in the history
This commit adds the missing 'del_nested_item_from_index_sequence',
which complements 'get_nested_item_from_index_sequence' and
'set_nested_item_from_index_sequence'. It was a missing counterpart and
has been added as a patch to 'mutwo.abjad' in mutwo-org/mutwo.abjad@bdfb37e.
Now it's officially part of 'mutwo.core'.
  • Loading branch information
levinericzimmermann committed Nov 1, 2023
1 parent 9847ce1 commit be976cb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
29 changes: 29 additions & 0 deletions mutwo/core_utilities/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"find_closest_item",
"get_nested_item_from_index_sequence",
"set_nested_item_from_index_sequence",
"del_nested_item_from_index_sequence",
"find_numbers_which_sums_up_to",
"call_function_except_attribute_error",
"round_floats",
Expand Down Expand Up @@ -396,6 +397,34 @@ def set_nested_item_from_index_sequence(
sequence = sequence[index]


def del_nested_item_from_index_sequence(
index_sequence: typing.Sequence[int],
sequence: typing.MutableSequence,
) -> None:
"""Delete item in nested Sequence.
:param index_sequence: The indices of the nested item which shall be deleted.
:type index_sequence: typing.Sequence[int]
:param sequence: A nested sequence.
:type sequence: typing.MutableSequence[typing.Any]
**Example:**
>>> from mutwo import core_utilities
>>> nested_sequence = [1, 2, [4, [5, 1], [9, [3]]]]
>>> core_utilities.del_nested_item_from_index_sequence((2, 2, 0), nested_sequence)
>>> nested_sequence
[1, 2, [4, [5, 1], [[3]]]]
"""

index_count = len(index_sequence)
for index_index, index in enumerate(index_sequence):
if index_count == index_index + 1:
sequence.__delitem__(index)
else:
sequence = sequence[index]


def round_floats(
number_to_round: core_constants.Real, n_digits: int
) -> core_constants.Real:
Expand Down
10 changes: 10 additions & 0 deletions tests/utilities/tools_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ def test_set_nested_item_from_index_sequence(self):
core_utilities.set_nested_item_from_index_sequence((2, 2, 0), nested_sequence, 100)
self.assertEqual(nested_sequence[2][2][0], 100)

def test_del_nested_item_from_index_sequence(self):
seq = [1, [2, 3, [4]]]
d = core_utilities.del_nested_item_from_index_sequence
d((1, 1), seq)
self.assertEqual(seq, [1, [2, [4]]])
d((1, 1, 0), seq)
self.assertEqual(seq, [1, [2, []]])
d((0,), seq)
self.assertEqual(seq, [[2, []]])

def test_find_numbers_which_sums_up_to(self):
self.assertEqual(
core_utilities.find_numbers_which_sums_up_to(3), ((3,), (1, 2), (1, 1, 1))
Expand Down

0 comments on commit be976cb

Please sign in to comment.