Skip to content

Commit

Permalink
Linting & formatting with ruff & pre-commit (#35)
Browse files Browse the repository at this point in the history
* Move linting to ruff (from flake8)

* Add pre-commit config + CI workflow for lint

* Move formatting to ruff-format (from black)

* Fix trailing whitespaces & end-of-files

* Sort imports with Ruff

* CI: enable dependency caching

* adding cache dependency path to pytest.yml

---------

Co-authored-by: Nathan Fradet <[email protected]>
  • Loading branch information
akx and Natooz authored Nov 21, 2023
1 parent c4b9d2e commit 7c8d7cc
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 40 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Lint

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
- uses: pre-commit/[email protected]
env:
RUFF_OUTPUT_FORMAT: github
8 changes: 2 additions & 6 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,13 @@ jobs:
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: pip
cache-dependency-path: setup.py
- name: Install dependencies
run: |
# Install local package with tests dependencies extras
python -m pip install --upgrade pip
pip install -e ".[tests]"
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest --cov=./ --cov-report=xml -n auto
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,4 @@ Thumbs.db
*.gz

# PyCharm config files
.idea/
.idea/
13 changes: 13 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.6
hooks:
- id: ruff
args:
- --fix
- id: ruff-format
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# miditoolkit

A python package for working with MIDI data.
A python package for working with MIDI data.

* Version: 0.1.17
* Latest update date: 2023/10/31
Expand All @@ -23,15 +23,15 @@ The usage is similar to [pretty_midi](https://github.com/craffel/pretty-midi), w
* chunk/cropping
* IO
* BytesIO
* Piano-rolls
* Piano-rolls
* Tools
* notes to piano-rolls
* piano-rolls to notes
* chromagram
* Visualization
* External Library
* [structure analysis](https://github.com/wayne391/sf_segmenter)

## TODO
* better documentation
* absolute timing
Expand All @@ -42,7 +42,7 @@ The usage is similar to [pretty_midi](https://github.com/craffel/pretty-midi), w

## Installation
* current version: 0.1.15
* **python 2 is not supported**
* **python 2 is not supported**
* Install the miditoolkit via [PYPI](https://pypi.org/project/miditoolkit/):
```bash
pip install miditoolkit
Expand Down Expand Up @@ -71,17 +71,15 @@ markers: 71
lyrics: False
instruments: 2
```
A. [Parse and create MIDI files](examples/parse_and_create_MIDI_files.ipynb)
A. [Parse and create MIDI files](examples/parse_and_create_MIDI_files.ipynb)
B. [Piano-roll Manipulation](examples/pinoroll_manipulation.ipynb)


## Philosophy
* [pretty_midi](https://github.com/craffel/pretty-midi) can parse MIDI files and generate pianorolls in absolute timing (seconds).
* [pretty_midi](https://github.com/craffel/pretty-midi) can parse MIDI files and generate pianorolls in absolute timing (seconds).
* [pypianoroll](https://github.com/salu133445/pypianoroll) can parse MIDI files into pianorolls in symbolic timing (through beat resolution).
* [mido](https://github.com/mido/mido) processes MIDI files in the lower level such as messages and ports.

**Miditoolkit** is designed for handling MIDI in **symbolic timing** (ticks), which is the native format of MIDI timing. We keep the midi parser as simple as possible, and offer several important functions to complete the versatility. For example, piano-rolls, tick-to-second, chromagram, and etc.

To customize settings and maximum the degree of freedom, users can use additional libraries like visualization, which are excluded in the toolkit.


To customize settings and maximum the degree of freedom, users can use additional libraries like visualization, which are excluded in the toolkit.
2 changes: 1 addition & 1 deletion miditoolkit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

# Convenience exports for commonly used classes.

from miditoolkit.midi.parser import MidiFile
from miditoolkit.midi.containers import (
ControlChange,
Instrument,
Expand All @@ -20,6 +19,7 @@
TempoChange,
TimeSignature,
)
from miditoolkit.midi.parser import MidiFile

__all__ = [
"ControlChange",
Expand Down
5 changes: 2 additions & 3 deletions miditoolkit/midi/containers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re
from typing import List, Union
from dataclasses import dataclass
from typing import List, Union


@dataclass
Expand Down Expand Up @@ -349,8 +349,7 @@ def _key_name_to_key_number(key_string: str):
# Allow for a space between key and mode
" ?"
# Next, look for any of the mode strings
"(?P<mode>(?:(?:"
+
"(?P<mode>(?:(?:" +
# Next, look for any of the major or minor mode strings
")|(?:".join(major_strs + minor_strs)
+ "))?)$"
Expand Down
18 changes: 9 additions & 9 deletions miditoolkit/midi/parser.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import mido
import functools
import collections
import functools
from pathlib import Path
from typing import Tuple, Union, Sequence
from typing import Sequence, Tuple, Union

import mido
import numpy as np

from .containers import (
ControlChange,
Instrument,
KeySignature,
TimeSignature,
Lyric,
Marker,
Note,
Pedal,
PitchBend,
ControlChange,
Instrument,
TempoChange,
Marker,
Pedal,
TimeSignature,
)

DEFAULT_BPM = int(120)
Expand Down Expand Up @@ -610,7 +610,7 @@ def event_compare(event1, event2):
# Add all note events
for note in instrument.notes:
if segment and not _is_note_within_tick_range(
note, start_tick, end_tick, shift, True
note, start_tick, end_tick, shift, True
):
continue
track.append(
Expand Down
2 changes: 1 addition & 1 deletion miditoolkit/pianoroll/parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from copy import deepcopy
from typing import List, Tuple, Callable, Union
from typing import Callable, List, Tuple, Union

import numpy as np

Expand Down
2 changes: 1 addition & 1 deletion miditoolkit/pianoroll/vis.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def plot_xticks(ax, xtick, xtick_interval, max_tick, beat_resolution, downbeats)
if downbeats.dtype == int:
xticks_downbeats = downbeats
elif downbeats.dtype == bool:
xticks_downbeats = np.where(downbeats == True)
xticks_downbeats = np.where(downbeats == True) # noqa: E712
else:
raise ValueError("Unkown downbeats type: %s" % downbeats)
ax.set_xticks(xticks_downbeats)
Expand Down
4 changes: 4 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target-version = "py37"
extend-select = [
"I",
]
6 changes: 2 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from setuptools import setup, find_packages

from setuptools import find_packages, setup

extras = {
"tests": [
"setuptools",
"flake8",
"pytest-cov",
"pytest-xdist[psutil]",
"setuptools",
"tqdm",
]
}
Expand Down
2 changes: 1 addition & 1 deletion tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# temporary test directory
tmp/
tmp/
4 changes: 2 additions & 2 deletions tests/test_pianoroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

from pathlib import Path

from miditoolkit import MidiFile
from miditoolkit.constants import PITCH_RANGE
from tqdm import tqdm

from miditoolkit import MidiFile
from miditoolkit.constants import PITCH_RANGE
from miditoolkit.pianoroll import notes2pianoroll, pianoroll2notes


Expand Down
5 changes: 3 additions & 2 deletions tests/test_read_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
"""

from pathlib import Path
import shutil
from pathlib import Path

from miditoolkit import MidiFile
from tqdm import tqdm

from miditoolkit import MidiFile


def test_load_dump():
midi_paths = list(Path("tests", "testcases").glob("**/*.mid"))
Expand Down

0 comments on commit 7c8d7cc

Please sign in to comment.