Skip to content

Commit

Permalink
ruff format, Python 3.10 as default (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
hagenw authored Jun 18, 2024
1 parent 4ee1dba commit 8e4a242
Show file tree
Hide file tree
Showing 18 changed files with 251 additions and 238 deletions.
10 changes: 0 additions & 10 deletions .flake8

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
matrix:
os: [ ubuntu-latest ]
python-version: [ '3.8' ]
python-version: [ '3.10' ]

steps:
- uses: actions/checkout@v3
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Set up Python 3.8
- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: '3.8'
python-version: '3.10'

- name: Install pre-commit hooks
run: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ jobs:
with:
fetch-depth: 2

- name: Set up Python 3.8
- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: '3.8'
python-version: '3.10'

- name: Install build dependencies
run: |
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ jobs:
strategy:
matrix:
os: [ ubuntu-20.04, macOS-latest, windows-latest ]
python-version: [ '3.8' ]
python-version: [ '3.10' ]
include:
- os: ubuntu-latest
python-version: '3.9'
python-version: '3.8'
- os: ubuntu-latest
python-version: '3.10'
python-version: '3.9'
- os: ubuntu-latest
python-version: '3.11'
- os: ubuntu-latest
Expand Down
6 changes: 4 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
#
#
default_language_version:
python: python3.8
python: python3.10

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.276
rev: v0.1.8
hooks:
- id: ruff
args: [ --fix ]
- id: ruff-format
- repo: https://github.com/codespell-project/codespell
rev: v2.2.4
hooks:
Expand Down
5 changes: 3 additions & 2 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Coding Convention
-----------------

We follow the PEP8_ convention for Python code
and check for correct syntax with ruff_.
and use ruff_ as a linter and code formatter.
In addition,
we check for common spelling errors with codespell_.
Both tools and possible exceptions
Expand All @@ -61,7 +61,8 @@ You can also install ruff_ and codespell_
and call it directly::

pip install ruff codespell # consider system wide installation
ruff check .
ruff check --fix . # lint all Python files, and fix any fixable errors
ruff format . # format code of all Python files
codespell

It can be restricted to specific folders::
Expand Down
1 change: 1 addition & 0 deletions audresample/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# Dynamically get the version of the installed module
try:
import importlib.metadata

__version__ = importlib.metadata.version(__name__)
except Exception: # pragma: no cover
importlib = None # pragma: no cover
Expand Down
60 changes: 32 additions & 28 deletions audresample/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@


def _check_signal(
signal: np.ndarray,
signal: np.ndarray,
) -> np.ndarray:
r"""Ensure float32 and two dimensions."""
if signal.ndim > 2:
raise RuntimeError(
f"Input signal must have 1 or 2 dimension, "
f"got {signal.ndim}."
f"Input signal must have 1 or 2 dimension, " f"got {signal.ndim}."
)
return np.atleast_2d(signal)


def am_fm_synth(
num_samples: int,
num_channels: int,
sampling_rate: int,
*,
dtype=np.float32,
num_samples: int,
num_channels: int,
sampling_rate: int,
*,
dtype=np.float32,
) -> np.ndarray:
r"""Synthesizes an AM/FM signal.
Expand Down Expand Up @@ -56,19 +55,19 @@ def am_fm_synth(
# No reinitialisation (to get true stereo)
for t in range(num_samples):
sig[idx, t] = g * np.cos(ph_fm)
sig[idx, t] *= ((1 - g_am) + g_am * np.square(np.cos(ph_am)))
sig[idx, t] *= (1 - g_am) + g_am * np.square(np.cos(ph_am))
ph_am += omega_am / 2
ph_fm += omega0_car + omega_dev * np.cos(omega_mod * t)
return sig


def remix(
signal: np.ndarray,
channels: typing.Union[int, typing.Sequence[int]] = None,
mixdown: bool = False,
*,
upmix: str = None,
always_copy: bool = False,
signal: np.ndarray,
channels: typing.Union[int, typing.Sequence[int]] = None,
mixdown: bool = False,
*,
upmix: str = None,
always_copy: bool = False,
) -> np.ndarray:
r"""Remix a signal.
Expand Down Expand Up @@ -136,14 +135,14 @@ def remix(
f"You can use the 'upmix' argument "
f"to increase available channels."
)
elif upmix == 'zeros':
elif upmix == "zeros":
signal_ex = np.zeros(
(max_channel, signal.shape[1]),
dtype=signal.dtype,
)
signal_ex[:signal.shape[0], :] = signal
signal_ex[: signal.shape[0], :] = signal
signal = signal_ex
elif upmix == 'repeat':
elif upmix == "repeat":
# Upmix signal with [0, 1, 0, 1, ...]
num_repetitions = int(np.ceil(max_channel / signal.shape[0]))
signal_ex = np.concatenate([signal] * num_repetitions, axis=0)
Expand All @@ -166,12 +165,12 @@ def remix(


def resample(
signal: np.ndarray,
original_rate: int,
target_rate: int,
*,
quality: define.ResampleQuality = config.DEFAULT_RESAMPLE_QUALITY,
always_copy: bool = False,
signal: np.ndarray,
original_rate: int,
target_rate: int,
*,
quality: define.ResampleQuality = config.DEFAULT_RESAMPLE_QUALITY,
always_copy: bool = False,
) -> np.ndarray:
r"""Resample signal to a new sampling rate.
Expand All @@ -198,8 +197,7 @@ def resample(
# We can only handle float32 signals
if signal.dtype != np.float32:
raise RuntimeError(
'Input signal must be of type float32/single, '
f'got {signal.dtype}.'
"Input signal must be of type float32/single, " f"got {signal.dtype}."
)

if original_rate == target_rate or signal.size == 0:
Expand All @@ -209,7 +207,9 @@ def resample(
return signal

converter_config = lib.init_converter_config(
float(original_rate), float(target_rate), ord(quality),
float(original_rate),
float(target_rate),
ord(quality),
)

channels = signal.shape[0]
Expand All @@ -224,7 +224,11 @@ def resample(
signal_in_p = x.ctypes.data_as(ctypes.POINTER(ctypes.c_float))
signal_out_p = y.ctypes.data_as(ctypes.POINTER(ctypes.c_float))
lib.audresample_oneshot(
converter_config, signal_in_p, num_in, signal_out_p, num_out,
converter_config,
signal_in_p,
num_in,
signal_out_p,
num_out,
)

return target
11 changes: 6 additions & 5 deletions audresample/core/define.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
class ResampleQuality:
r"""Quality levels for resampling."""
QUICK = 'q'
LOW = 'l'
MEDIUM = 'm'
HIGH = 'h'
VERY_HIGH = 'v'

QUICK = "q"
LOW = "l"
MEDIUM = "m"
HIGH = "h"
VERY_HIGH = "v"
35 changes: 18 additions & 17 deletions audresample/core/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,34 @@ def platform_name():
system = platform.system()
machine = platform.machine().lower()

if system == 'Linux': # pragma: no cover
system = 'manylinux_2_17'
elif system == 'Windows': # pragma: no cover
system = 'win'
elif system == 'Darwin': # pragma: no cover
if machine == 'x86_64':
system = 'macosx_10_4'
if system == "Linux": # pragma: no cover
system = "manylinux_2_17"
elif system == "Windows": # pragma: no cover
system = "win"
elif system == "Darwin": # pragma: no cover
if machine == "x86_64":
system = "macosx_10_4"
else:
system = 'macosx_11_0'
system = "macosx_11_0"
else: # pragma: no cover
raise RuntimeError(f'Unsupported platform {system}')
raise RuntimeError(f"Unsupported platform {system}")

return f'{system}_{machine}'
return f"{system}_{machine}"


# load library

root = os.path.dirname(os.path.realpath(__file__))
bin_path = os.path.join(root, 'bin')
bin_path = os.path.join(root, "bin")

plat_name = platform_name()

if 'linux' in plat_name: # pragma: no cover
library = 'libaudresample.so'
elif 'macos' in plat_name: # pragma: no cover
library = 'libaudresample.dylib'
elif 'win' in plat_name: # pragma: no cover
library = 'audresample.dll'
if "linux" in plat_name: # pragma: no cover
library = "libaudresample.so"
elif "macos" in plat_name: # pragma: no cover
library = "libaudresample.dylib"
elif "win" in plat_name: # pragma: no cover
library = "audresample.dll"

lib_path = os.path.join(bin_path, plat_name, library)

Expand All @@ -68,6 +68,7 @@ def platform_name():

# resample


class ConverterConfig(ctypes.Structure): # noqa: D101
_fields_ = [
("srIn", ctypes.c_double),
Expand Down
Loading

0 comments on commit 8e4a242

Please sign in to comment.