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 1fb6a66..a105530 100644 --- a/pyabc2/pitch.py +++ b/pyabc2/pitch.py @@ -61,10 +61,17 @@ 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_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]+)") +_S_RE_LOWER_PITCH_CLASS = rf"[a-g]{_S_RE_ASCII_ACCIDENTALS}?" +RE_PITCH_CLASS = re.compile(_S_RE_PITCH_CLASS) + +# 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: @@ -73,9 +80,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 +655,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}'")