From a7da9795a5c7f03304031cd004657335d3dc2384 Mon Sep 17 00:00:00 2001 From: Miika Alonen Date: Thu, 16 Feb 2023 22:58:54 +0200 Subject: [PATCH] Added string replacement function for future use --- .gitignore | 2 +- examples/music21_example.py | 15 ++++++++++++++ pyproject.toml | 3 +++ test.py | 10 ---------- ziffers/common.py | 39 +++++++++++++++++++++++++++++++++++-- 5 files changed, 56 insertions(+), 13 deletions(-) create mode 100644 examples/music21_example.py delete mode 100644 test.py diff --git a/.gitignore b/.gitignore index 9a7f66e..7e7e060 100644 --- a/.gitignore +++ b/.gitignore @@ -166,6 +166,6 @@ cython_debug/ .vscode # Debugging files -debug.py +debug*.py test.py diff --git a/examples/music21_example.py b/examples/music21_example.py new file mode 100644 index 0000000..a4a8f61 --- /dev/null +++ b/examples/music21_example.py @@ -0,0 +1,15 @@ +from music21 import * +from sardine import * +from ziffers import * + + +s = to_music21('i v vi vii^dim',time="4/4") + +s.show() +s.show('midi') + +parsed = zparse('1 2 qr e 124') +s2 = to_music21(parsed,time="4/4") + +s2.show() +s2.show('midi') \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index f70b3f8..9a8949c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,6 +27,9 @@ homepage = "https://github.com/Bubobubobubobubo/ziffers-python" documentation = "https://github.com/Bubobubobubobubo/ziffers-python" repository = "https://github.com/Bubobubobubobubo/ziffers-python" +[tool.setuptools] +py-modules = ['ziffers'] + [tool.black] target_version = ['py311'] include = '\.pyi?$' diff --git a/test.py b/test.py deleted file mode 100644 index 044f699..0000000 --- a/test.py +++ /dev/null @@ -1,10 +0,0 @@ -from music21 import * -from sardine import * -from ziffers import * - -a = zparse('1 2 qr 124') -print(list(a)) -s = to_music21('1 2 qr 124',octave=-2,time="3/4") - -s.show() -s.show('midi') diff --git a/ziffers/common.py b/ziffers/common.py index 147592b..93bc955 100644 --- a/ziffers/common.py +++ b/ziffers/common.py @@ -1,9 +1,13 @@ """ Common methods used in parsing """ +import re + + def flatten(arr: list) -> list: """Flattens array""" return ( flatten(arr[0]) + (flatten(arr[1:]) if len(arr) > 1 else []) - if isinstance(arr, list) else [arr] + if isinstance(arr, list) + else [arr] ) @@ -17,4 +21,35 @@ def sum_dict(arr: list[dict]) -> dict: else: result[key] = element[key] return result - \ No newline at end of file + + +def string_rewrite(axiom: str, rules: dict): + """String rewrite / Lindemeyer system for rule based text manipulation + + Args: + axiom (str): Input string + rules (dict): String manipulation rules in dict: + + Example: + rules = { + "1": "2", + "[2-9]": "45", + "4": lambda: str(randint(1, 7)), + "([1-9])(5)": lambda a, b: str(int(a)*int(b)) + } + + for i range(10): + print(string_rewrite("1", rules)) + """ + + def _apply_rules(match): + for key, value in rules.items(): + if re.match(key, match.group(0)): + if callable(value): + yield value( + *match.groups() + ) if value.__code__.co_argcount > 0 else value() + yield value + + pattern = re.compile("|".join(rules.keys())) + return pattern.sub(lambda m: next(_apply_rules(m)), axiom)