Skip to content

Commit e1320c2

Browse files
authored
Use ruff as linter and formatter in github action (#17)
* Use ruff as linter and formatter in github action * Implemented Ruff format suggestions * Run ruff on whole directory
1 parent 22fc390 commit e1320c2

10 files changed

+121
-121
lines changed

.github/workflows/python_package.yml

+15-11
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,28 @@ jobs:
1313
strategy:
1414
matrix:
1515
python-version: ["3.10", "3.11", "3.12"]
16-
1716
steps:
18-
- uses: actions/checkout@v3
17+
- uses: actions/checkout@v4
1918
- name: Set up Python ${{ matrix.python-version }}
2019
uses: actions/setup-python@v3
2120
with:
2221
python-version: ${{ matrix.python-version }}
2322
- name: Install dependencies
2423
run: |
25-
python -m pip install --upgrade pip
26-
pip install flake8
2724
pip install .[test]
28-
- name: Lint with flake8
29-
run: |
30-
# stop the build if there are Python syntax errors or undefined names
31-
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
32-
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
33-
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
3425
- name: Test with pytest
3526
run: |
36-
pytest
27+
pytest
28+
29+
ruff:
30+
runs-on: ubuntu-latest
31+
steps:
32+
- uses: actions/checkout@v4
33+
- name: Ruff Lint
34+
uses: astral-sh/ruff-action@v1
35+
with:
36+
args: "check --config pyproject.toml"
37+
- name: Ruff Format
38+
uses: astral-sh/ruff-action@v1
39+
with:
40+
args: "format --diff"

.github/workflows/ruff.yml

-8
This file was deleted.

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
You should also add project tags for each release in Github, see [Managing releases in a repository](https://docs.github.com/en/repositories/releasing-projects-on-github/managing-releases-in-a-repository).
88

9+
## [Unreleased]
10+
### Changed
11+
- GitHub workflow for linting and formatting uses ruff as a separate job
12+
13+
### Removed
14+
- GitHub action to run flake8 for linting in build
15+
16+
917
## [2.0.0] - 2024-05-29
1018
### Added
1119
- Added example auto-built Sphinx documentation in the `docs` folder

docs/conf.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,44 @@
1212
#
1313
import os
1414
import sys
15-
sys.path.insert(0, os.path.abspath('..'))
15+
16+
sys.path.insert(0, os.path.abspath(".."))
1617

1718

1819
# -- Project information -----------------------------------------------------
1920

20-
project = 'testdoc'
21-
copyright = '2023, Luke Ruud'
22-
author = 'Luke Ruud'
21+
project = "testdoc"
22+
copyright = "2023, Luke Ruud"
23+
author = "Luke Ruud"
2324

2425
# The full version, including alpha/beta/rc tags
25-
release = '0.1'
26+
release = "0.1"
2627

2728

2829
# -- General configuration ---------------------------------------------------
2930

3031
# Add any Sphinx extension module names here, as strings. They can be
3132
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
3233
# ones.
33-
extensions = ["sphinx.ext.autodoc", "sphinx.ext.napoleon"
34-
]
34+
extensions = ["sphinx.ext.autodoc", "sphinx.ext.napoleon"]
3535

3636
# Add any paths that contain templates here, relative to this directory.
37-
templates_path = ['_templates']
37+
templates_path = ["_templates"]
3838

3939
# List of patterns, relative to source directory, that match files and
4040
# directories to ignore when looking for source files.
4141
# This pattern also affects html_static_path and html_extra_path.
42-
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
42+
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
4343

4444

4545
# -- Options for HTML output -------------------------------------------------
4646

4747
# The theme to use for HTML and HTML Help pages. See the documentation for
4848
# a list of builtin themes.
4949
#
50-
html_theme = 'alabaster'
50+
html_theme = "alabaster"
5151

5252
# Add any paths that contain custom static files (such as style sheets) here,
5353
# relative to this directory. They are copied after the builtin static files,
5454
# so a file named "default.css" will overwrite the builtin "default.css".
55-
html_static_path = ['_static']
55+
html_static_path = ["_static"]

notebooks/word_count_prototype.ipynb

+61-66
Large diffs are not rendered by default.

pyproject.toml

+7-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ dependencies = [
3232
# Extra dependencies only needed for running tests go here
3333
test = ["pytest"]
3434

35-
# Dependencies that are useful only to developers, like an autoformatter and support for visualizations in jupyter notebooks go here
35+
# Dependencies that are useful only to developers, like an autoformatter and
36+
# support for visualizations in jupyter notebooks go here
3637
dev = [
3738
"ruff",
3839
"jupyter",
@@ -45,3 +46,8 @@ dev = [
4546
# The value must be of the form "<package_name>:<module_name>.<function>"
4647
[project.scripts]
4748
corpus-counter = "cdstemplate:corpus_counter_script.main_cli"
49+
50+
# Add customizations to the Ruff linter as described on https://docs.astral.sh/ruff/configuration/
51+
[tool.ruff]
52+
# Override ruff's default so that lines aren't more than 120 characters
53+
line-length = 119

src/cdstemplate/corpus_counter_script.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""An example of a script you can run. It tokenizes an folder of input documents and
22
writes the corpus counts to a user-specified CSV file
33
"""
4+
45
# Import modules, functions and classes from external libraries
56
import argparse
67
import logging
@@ -11,19 +12,16 @@
1112

1213
logger = logging.getLogger(__name__)
1314

15+
1416
def main_cli():
15-
"""A wrapper function that defines command line arguments and help messages for
16-
when the user wants run this module's code as a script.
17+
"""A wrapper function that defines command line arguments and help messages for
18+
when the user wants run this module's code as a script.
1719
"""
1820
# The argument parser gives nice ways to include help message and specify which arguments
1921
# are required or optional, see https://docs.python.org/3/library/argparse.html#prog for usage instructions
20-
parser = argparse.ArgumentParser(
21-
description="A script to generate counts of tokens in a corpus"
22-
)
22+
parser = argparse.ArgumentParser(description="A script to generate counts of tokens in a corpus")
2323

24-
parser.add_argument(
25-
"csv", help="Path to the output CSV storing token counts. Required."
26-
)
24+
parser.add_argument("csv", help="Path to the output CSV storing token counts. Required.")
2725

2826
parser.add_argument(
2927
"documents",
@@ -64,5 +62,5 @@ def main(csv_out, documents, case_insensitive=False):
6462

6563
# The entry point of your script - if a user runs it from the command line, for example using `python -m <package>.<module>`
6664
# or `python <script_path>.py`, this is what will be run.
67-
if __name__ == "__main__":
65+
if __name__ == "__main__":
6866
main_cli()

src/cdstemplate/utils.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
"""A module for important set-up and configuration functionality, but doesn't implement the library's key features.
2-
"""
1+
"""A module for important set-up and configuration functionality, but doesn't implement the library's key features."""
2+
33
import logging
44

55

66
def configure_logging():
7-
"""A helper method that configures logging, usable by any script in this library.
8-
"""
7+
"""A helper method that configures logging, usable by any script in this library."""
98
logging.basicConfig(
109
level=logging.DEBUG,
1110
format="%(levelname)s : %(asctime)s : %(name)s : %(message)s",

src/cdstemplate/word_count.py

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""An example of an module with functions and a class that can be imported once the package is installed.
22
This module provides operations for tokenization and tracking cumulative word counts in a set of documents.
33
"""
4+
45
from collections import Counter
56
import logging
67
import re
@@ -29,8 +30,7 @@ def tokenize(text, pattern=r"\s"):
2930

3031

3132
class CorpusCounter:
32-
"""A simple class object that tracks document and token counts in a corpus.
33-
"""
33+
"""A simple class object that tracks document and token counts in a corpus."""
3434

3535
def __init__(self, tokenization_pattern=r"\s", case_insensitive=False):
3636
"""Constructor instantiates with empty counters
@@ -89,16 +89,12 @@ def get_token_count(self, token):
8989
return self.token_counter[token]
9090

9191
def get_vocab_size(self):
92-
"""Returns vocabulary size (number of unique tokens)
93-
"""
92+
"""Returns vocabulary size (number of unique tokens)"""
9493
return len(self.token_counter)
9594

9695
def get_token_counts_as_dataframe(self):
97-
"""Returns the token counts of the corpus as a Pandas DataFrame with columns 'token', 'count'
98-
"""
99-
dataframe = pd.DataFrame.from_records(
100-
list(self.token_counter.items()), columns=["token", "count"]
101-
)
96+
"""Returns the token counts of the corpus as a Pandas DataFrame with columns 'token', 'count'"""
97+
dataframe = pd.DataFrame.from_records(list(self.token_counter.items()), columns=["token", "count"])
10298
dataframe = dataframe.sort_values("token")
10399
return dataframe
104100

@@ -111,4 +107,3 @@ def save_token_counts(self, csv_file):
111107
"""
112108
logger.info("Saving token counts to %s", csv_file)
113109
self.get_token_counts_as_dataframe().to_csv(csv_file, index=False, header=True)
114-

tests/test_word_count.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
33
In pytest, each individual test is a python function that starts with `test`.
44
"""
5+
56
# Import your library for testing
67
from cdstemplate import word_count
78

89

910
def test_tokenize_document():
10-
my_document = "It was all very well to say `Drink me,' but the wise little Alice was not going to do that in a hurry."
11+
my_document = (
12+
"It was all very well to say `Drink me,' but the wise little Alice was not going to do that in a hurry."
13+
)
1114

1215
expected_tokens = [
1316
"It",

0 commit comments

Comments
 (0)