Browse Source

startup: define function `split_sequence_by_delimiter`

Fabian Peter Hammerle 3 years ago
parent
commit
f25e8d9efc
1 changed files with 20 additions and 0 deletions
  1. 20 0
      profile_default/startup/init.py

+ 20 - 0
profile_default/startup/init.py

@@ -1,3 +1,4 @@
+import itertools
 import pathlib
 import typing
 
@@ -5,6 +6,25 @@ import numpy
 import scipy.io.wavfile
 
 
+def split_sequence_by_delimiter(
+    sequence: typing.Sequence, delimiter: typing.Any, delimiter_min_length: int = 1
+) -> typing.Iterator[typing.Sequence]:
+    slice_start_index, slice_length = 0, 0
+    for is_delimiter, group in itertools.groupby(
+        sequence, key=lambda item: item == delimiter
+    ):
+        group_length = sum(1 for _ in group)
+        if is_delimiter and group_length >= delimiter_min_length:
+            if slice_length > 0:
+                yield sequence[slice_start_index : slice_start_index + slice_length]
+            slice_start_index += slice_length + group_length
+            slice_length = 0
+        else:
+            slice_length += group_length
+    if slice_length > 0:
+        yield sequence[slice_start_index : slice_start_index + slice_length]
+
+
 def trim_where(
     # https://docs.python.org/3.8/library/collections.abc.html#collections-abstract-base-classes
     sequence: typing.Sequence,