From 4f430b42b11f61b35fb65e20af4c937f54a69110 Mon Sep 17 00:00:00 2001 From: zmoon Date: Mon, 30 Jun 2025 16:45:48 -0500 Subject: [PATCH 1/3] Sort --- pyabc2/pitch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyabc2/pitch.py b/pyabc2/pitch.py index 1fb6a66..b9f73ca 100644 --- a/pyabc2/pitch.py +++ b/pyabc2/pitch.py @@ -61,9 +61,9 @@ def _gen_pitch_values() -> Dict[str, int]: _S_RE_ASCII_ACCIDENTALS = r"(?:##|bb|b|#|=)" _S_RE_PITCH_CLASS = rf"[A-G]{_S_RE_ASCII_ACCIDENTALS}?" +# _S_RE_PITCH_CLASS_ONE_ACC = r"[A-G][\#|b]?" _S_RE_LOWER_PITCH_CLASS = rf"[a-g]{_S_RE_ASCII_ACCIDENTALS}?" _RE_PITCH_CLASS = re.compile(_S_RE_PITCH_CLASS) -# _S_RE_PITCH_CLASS_ONE_ACC = r"[A-G][\#|b]?" _RE_PITCH = re.compile(rf"(?P{_S_RE_PITCH_CLASS})" r"\s*" r"(?P[0-9]+)") From ad9c38dcc8c7429f0a9c29974061e9961ed914df Mon Sep 17 00:00:00 2001 From: zmoon Date: Mon, 30 Jun 2025 16:51:06 -0500 Subject: [PATCH 2/3] Note regexes as public --- docs/examples/sources.ipynb | 2 +- pyabc2/note.py | 4 ++-- pyabc2/parse.py | 6 +++--- pyabc2/pitch.py | 10 +++++----- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/examples/sources.ipynb b/docs/examples/sources.ipynb index 14d3418..9b9f9a0 100644 --- a/docs/examples/sources.ipynb +++ b/docs/examples/sources.ipynb @@ -181,7 +181,7 @@ "metadata": {}, "outputs": [], "source": [ - "from pyabc2.note import _RE_NOTE as rx\n", + "from pyabc2.note import RE_NOTE as rx\n", "\n", "rx" ] diff --git a/pyabc2/note.py b/pyabc2/note.py index 3fa990b..3f67f61 100644 --- a/pyabc2/note.py +++ b/pyabc2/note.py @@ -17,7 +17,7 @@ r"(?P/+)?" r"(?P[0-9]+)?" ) -_RE_NOTE = re.compile(_S_RE_NOTE) +RE_NOTE = re.compile(_S_RE_NOTE) _ACCIDENTAL_ASCII_TO_ABC = {"#": "^", "b": "_", "=": "="} @@ -159,7 +159,7 @@ def from_abc( but this can be adjusted using the keyword arguments. """ - m = _RE_NOTE.match(abc) + m = RE_NOTE.match(abc) return cls._from_abc_match(m, key=key, octave_base=octave_base, unit_duration=unit_duration) @classmethod diff --git a/pyabc2/parse.py b/pyabc2/parse.py index e399f7a..cefa0fc 100644 --- a/pyabc2/parse.py +++ b/pyabc2/parse.py @@ -6,7 +6,7 @@ from typing import Dict, Iterator, List, NamedTuple, Optional from .key import Key -from .note import _RE_NOTE, Note +from .note import RE_NOTE, Note class InfoField(NamedTuple): @@ -180,7 +180,7 @@ def _find_first_chord(s: str) -> Optional[str]: # NOTE: did see some single notes inside `[]` in The Session data c = m.group() assert c.startswith("[") and c.endswith("]") - n = len(_RE_NOTE.findall(c)) # TODO: maybe just letter count would be suff. + n = len(RE_NOTE.findall(c)) # TODO: maybe just letter count would be suff. if n >= 2: return c else: @@ -375,7 +375,7 @@ def _extract_measures(self, tune_lines: List[str]) -> None: # https://abcnotation.com/wiki/abc:standard:v2.1#broken_rhythm # 3. In note group, find notes - for m_note in _RE_NOTE.finditer(note_group): + for m_note in RE_NOTE.finditer(note_group): # TODO: parse/store rests, maybe have an additional iterator for "rhythmic elements" or something if m_note is None: diff --git a/pyabc2/pitch.py b/pyabc2/pitch.py index b9f73ca..c24aec5 100644 --- a/pyabc2/pitch.py +++ b/pyabc2/pitch.py @@ -63,8 +63,8 @@ def _gen_pitch_values() -> Dict[str, int]: _S_RE_PITCH_CLASS = rf"[A-G]{_S_RE_ASCII_ACCIDENTALS}?" # _S_RE_PITCH_CLASS_ONE_ACC = r"[A-G][\#|b]?" _S_RE_LOWER_PITCH_CLASS = rf"[a-g]{_S_RE_ASCII_ACCIDENTALS}?" -_RE_PITCH_CLASS = re.compile(_S_RE_PITCH_CLASS) -_RE_PITCH = re.compile(rf"(?P{_S_RE_PITCH_CLASS})" r"\s*" r"(?P[0-9]+)") +RE_PITCH_CLASS = re.compile(_S_RE_PITCH_CLASS) +RE_PITCH = re.compile(rf"(?P{_S_RE_PITCH_CLASS})" r"\s*" r"(?P[0-9]+)") def pitch_class_value(pitch: str, root: str = "C", *, mod: bool = False) -> int: @@ -73,9 +73,9 @@ def pitch_class_value(pitch: str, root: str = "C", *, mod: bool = False) -> int: """ pitch = pitch.strip() - if not _RE_PITCH_CLASS.fullmatch(pitch): + if not RE_PITCH_CLASS.fullmatch(pitch): raise ValueError( - f"invalid pitch class specification '{pitch}'; Should match '{_RE_PITCH_CLASS.pattern}'" + f"invalid pitch class specification '{pitch}'; Should match '{RE_PITCH_CLASS.pattern}'" ) # Base value @@ -648,7 +648,7 @@ def from_name(cls, name: str) -> "Pitch": """ name = name.strip() - m = _RE_PITCH.fullmatch(name) + m = RE_PITCH.fullmatch(name) if m is None: raise ValueError(f"invalid pitch name '{name}'") From 76faf469dec7e5a8b28246944dc57f704f4c291f Mon Sep 17 00:00:00 2001 From: zmoon Date: Mon, 30 Jun 2025 16:57:35 -0500 Subject: [PATCH 3/3] Restore desired formatting --- pyabc2/pitch.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pyabc2/pitch.py b/pyabc2/pitch.py index c24aec5..a105530 100644 --- a/pyabc2/pitch.py +++ b/pyabc2/pitch.py @@ -64,7 +64,14 @@ def _gen_pitch_values() -> Dict[str, int]: # _S_RE_PITCH_CLASS_ONE_ACC = r"[A-G][\#|b]?" _S_RE_LOWER_PITCH_CLASS = rf"[a-g]{_S_RE_ASCII_ACCIDENTALS}?" RE_PITCH_CLASS = re.compile(_S_RE_PITCH_CLASS) -RE_PITCH = re.compile(rf"(?P{_S_RE_PITCH_CLASS})" r"\s*" r"(?P[0-9]+)") + +# fmt: off +RE_PITCH = re.compile( + rf"(?P{_S_RE_PITCH_CLASS})" + r"\s*" + r"(?P[0-9]+)" +) +# fmt: on def pitch_class_value(pitch: str, root: str = "C", *, mod: bool = False) -> int: