Skip to content

Core

core

Foundation re-exports + small utility helpers.

The Core namespace re-exports the lowest-level building blocks: pitch arithmetic, the note and interval parsers, and the transposition/distance functions. Most users won't touch Core directly — the high-level namespaces (Note, Interval, Chord, ...) wrap these.

Two convenience helpers live here:

  • fill_str — repeat a string abs(n) times.
  • deprecate — wrap a function so calls emit a DeprecationWarning.
Example

from tonal_py.core import note, transpose, distance note("C4").midi 60 transpose("C4", "M3") 'E4' distance("C4", "G4") '5P'

Source parity: @tonaljs/core

fill_str

fill_str(s: str, n: int) -> str

Repeat string s abs(n) times.

Parameters:

Name Type Description Default
s str

String to repeat.

required
n int

Repeat count (sign ignored).

required

Returns:

Type Description
str

s * abs(n).

Example

from tonal_py.core import fill_str fill_str("a", 3) 'aaa' fill_str("ab", 2) 'abab' fill_str("a", -3) # sign ignored 'aaa'

Source code in src/tonal_py/core.py
def fill_str(s: str, n: int) -> str:
    """Repeat string `s` `abs(n)` times.

    Args:
        s: String to repeat.
        n: Repeat count (sign ignored).

    Returns:
        `s * abs(n)`.

    Example:
        >>> from tonal_py.core import fill_str
        >>> fill_str("a", 3)
        'aaa'
        >>> fill_str("ab", 2)
        'abab'
        >>> fill_str("a", -3)        # sign ignored
        'aaa'
    """
    return s * abs(n)

deprecate

deprecate(original: str, alternative: str, fn: F) -> F

Wrap fn so calls emit a DeprecationWarning.

Parameters:

Name Type Description Default
original str

The deprecated name (used in the warning message).

required
alternative str

The recommended replacement name.

required
fn F

The function to wrap.

required

Returns:

Type Description
F

A wrapped function with the same signature.

Example

import warnings from tonal_py.core import deprecate def add(a, b): return a + b deprecated_add = deprecate("add", "newAdd", add) with warnings.catch_warnings(record=True) as w: ... warnings.simplefilter("always") ... deprecated_add(2, 3) ... issubclass(w[0].category, DeprecationWarning) 5 True

Source code in src/tonal_py/core.py
def deprecate(original: str, alternative: str, fn: F) -> F:
    """Wrap `fn` so calls emit a `DeprecationWarning`.

    Args:
        original: The deprecated name (used in the warning message).
        alternative: The recommended replacement name.
        fn: The function to wrap.

    Returns:
        A wrapped function with the same signature.

    Example:
        >>> import warnings
        >>> from tonal_py.core import deprecate
        >>> def add(a, b): return a + b
        >>> deprecated_add = deprecate("add", "newAdd", add)
        >>> with warnings.catch_warnings(record=True) as w:
        ...     warnings.simplefilter("always")
        ...     deprecated_add(2, 3)
        ...     issubclass(w[0].category, DeprecationWarning)
        5
        True
    """
    def wrapper(*args: Any, **kwargs: Any) -> Any:
        warnings.warn(
            f"{original} is deprecated. Use {alternative}.",
            DeprecationWarning,
            stacklevel=2,
        )
        return fn(*args, **kwargs)

    wrapper.__name__ = getattr(fn, "__name__", original)
    wrapper.__doc__ = fn.__doc__
    return wrapper  # type: ignore[return-value]