Skip to content

Commit

Permalink
Replace flake8 by ruff
Browse files Browse the repository at this point in the history
Fix lint issues
  • Loading branch information
DarwinsBuddy committed Jan 21, 2024
1 parent bdbe1e9 commit ef74ee7
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 70 deletions.
11 changes: 3 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Tests
on: [pull_request, push]

jobs:
build:
test:
runs-on: ubuntu-latest
strategy:
matrix:
Expand Down Expand Up @@ -36,15 +36,10 @@ jobs:
python -m venv .venv
. .venv/bin/activate
python -m pip install --upgrade pip
pip install flake8
pip install -r requirements_dev.txt
if: steps.cache-venv.outputs.cache-hit != 'true'
- name: Lint with flake8
run: |
. .venv/bin/activate
# stop the build if there are Python syntax errors or undefined names
flake8 foosball --count --select=E9,F63,F7,F82 --show-source --statistics --config tests/setup.cfg
flake8 foosball --count --exit-zero --max-complexity=10 --statistics --config tests/setup.cfg
- name: Lint with ruff
uses: chartboost/ruff-action@v1
- name: Test with pytest
run: |
. .venv/bin/activate
Expand Down
2 changes: 0 additions & 2 deletions const.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from enum import Enum

CALIBRATION_MODE = "calibrationMode"
CALIBRATION_IMG_PATH = "calibrationImagePath"
CALIBRATION_VIDEO = "calibrationVideo"
Expand Down
2 changes: 0 additions & 2 deletions foosball/arUcos/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
from .calibration import Calibration
from .models import Aruco
4 changes: 2 additions & 2 deletions foosball/detectors/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class BallConfig:
invert_mask: bool = False

def store(self):
filename = f"ball.yaml"
filename = "ball.yaml"
print(f"Store config {filename}" + (" " * 50), end="\n\n")
with open(filename, "w") as f:
yaml.dump(self.to_dict(), f)
Expand Down Expand Up @@ -66,7 +66,7 @@ class GoalConfig:
invert_mask: bool = True

def store(self):
filename = f"goal.yaml"
filename = "goal.yaml"
print(f"Store config {filename}" + (" " * 50), end="\n\n")
with open(filename, "w") as f:
yaml.dump(self.to_dict(), f)
Expand Down
3 changes: 0 additions & 3 deletions foosball/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import collections
import logging
import os
from dataclasses import dataclass
from enum import Enum
from typing import Optional, Union

import cv2
import numpy as np
import yaml

HSV = np.ndarray # list[int, int, int]
RGB = np.ndarray # list[int, int, int]
Expand Down
12 changes: 6 additions & 6 deletions foosball/sink/opencv.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ def add_config_input(calibrationMode, config):

def add_ball_config_input(bounds: BallConfig):
[lower_hsv, upper_hsv] = bounds.bounds
cv2.createTrackbar(f'invert_frame', BALL, 1 if bounds.invert_frame else 0, 1, lambda v: None)
cv2.createTrackbar(f'invert_mask', BALL, 1 if bounds.invert_mask else 0, 1, lambda v: None)
cv2.createTrackbar('invert_frame', BALL, 1 if bounds.invert_frame else 0, 1, lambda v: None)
cv2.createTrackbar('invert_mask', BALL, 1 if bounds.invert_mask else 0, 1, lambda v: None)
# create trackbars for color change
cv2.createTrackbar('Hue', BALL, avg(lower_hsv[0], upper_hsv[0]), 179, lambda v: None)
cv2.createTrackbar(slider_label('S', 'low'), BALL, lower_hsv[1], 255, lambda v: None)
Expand All @@ -94,8 +94,8 @@ def add_ball_config_input(bounds: BallConfig):

def add_goals_config_input(config: GoalConfig):
[lower, upper] = config.bounds
cv2.createTrackbar(f'invert_frame', GOAL, 1 if config.invert_frame else 0, 1, lambda v: None)
cv2.createTrackbar(f'invert_mask', GOAL, 1 if config.invert_mask else 0, 1, lambda v: None)
cv2.createTrackbar('invert_frame', GOAL, 1 if config.invert_frame else 0, 1, lambda v: None)
cv2.createTrackbar('invert_mask', GOAL, 1 if config.invert_mask else 0, 1, lambda v: None)
# create trackbars for color change
cv2.createTrackbar("lower", GOAL, lower, 255, lambda v: None)
cv2.createTrackbar("upper", GOAL, upper, 255, lambda v: None)
Expand Down Expand Up @@ -138,7 +138,7 @@ def reset_goal_config(config: GoalConfig):


def store_ball_config(config: BallConfig):
filename = f"ball.yaml"
filename = "ball.yaml"
[lower, upper] = config.bounds
print(f"Store config {filename}" + (" " * 50), end="\n\n")
with open(filename, "w") as f:
Expand All @@ -151,7 +151,7 @@ def store_ball_config(config: BallConfig):


def store_goals_config(config: GoalConfig):
filename = f"goal.yaml"
filename = "goal.yaml"
[lower, upper] = config.bounds
print(f"Store config {filename}" + (" " * 50), end="\n\n")
with open(filename, "w") as f:
Expand Down
2 changes: 1 addition & 1 deletion foosball/source/gear.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self, video=None, resolution=(640, 480), framerate=60, **kwargs):
self.eos = False
# if a video path was not supplied, grab the reference
# to the webcam
if video is None or type(video) == int:
if video is None or isinstance(video, int):
options = {
"CAP_PROP_FRAME_WIDTH": resolution[0],
"CAP_PROP_FRAME_HEIGHT": resolution[1],
Expand Down
3 changes: 2 additions & 1 deletion foosball/tracking/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import numpy as np

from const import CalibrationMode, OFF
from ..arUcos import calibration, Aruco
from ..arUcos import calibration
from ..arUcos.models import Aruco
from ..detectors.color import GoalDetector, GoalConfig
from ..models import Frame, PreprocessResult, Point, Rect, Blob, Goals, FrameDimensions, ScaleDirection, \
InfoLog, Info
Expand Down
8 changes: 4 additions & 4 deletions foosball/tracking/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ def get_info(self, ball_track: Track) -> InfoLog:
])
if self.ball_calibration:
[lower, upper] = self.ball_detector.config.bounds
info.append(Info(verbosity=0, title=f"lower", value=f'({",".join(map(str,lower))})'))
info.append(Info(verbosity=0, title=f"upper", value=f'({",".join(map(str,upper))})'))
info.append(Info(verbosity=0, title=f"invert frame", value=f'{self.ball_detector.config.invert_frame}'))
info.append(Info(verbosity=0, title=f"invert mask", value=f'{self.ball_detector.config.invert_mask}'))
info.append(Info(verbosity=0, title="lower", value=f'({",".join(map(str,lower))})'))
info.append(Info(verbosity=0, title="upper", value=f'({",".join(map(str,upper))})'))
info.append(Info(verbosity=0, title="invert frame", value=f'{self.ball_detector.config.invert_frame}'))
info.append(Info(verbosity=0, title="invert mask", value=f'{self.ball_detector.config.invert_mask}'))
return info

@property
Expand Down
116 changes: 75 additions & 41 deletions tests/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,81 @@ addopts =
--cov=foosball
--cov-branch
--cov-report=html
[tool.ruff]
# Exclude a variety of commonly ignored directories.
exclude = [
".bzr",
".direnv",
".eggs",
".git",
".git-rewrite",
".hg",
".ipynb_checkpoints",
".mypy_cache",
".nox",
".pants.d",
".pyenv",
".pytest_cache",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
".vscode",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"site-packages",
"venv",
]

[flake8]
# https://github.com/ambv/black#line-length
max-line-length = 120
# E501: line too long
# W503: Line break occurred before a binary operator
# E203: Whitespace before ':'
# D202 No blank lines allowed after function docstring
# W504 line break after binary operator
ignore =
E501,
W503,
E203,
D202,
W504
# Same as Black.
line-length = 120
indent-width = 4

[isort]
# https://github.com/timothycrosley/isort
# https://github.com/timothycrosley/isort/wiki/isort-Settings
# splits long import on multiple lines indented by 4 spaces
multi_line_output = 3
include_trailing_comma=True
force_grid_wrap=0
use_parentheses=True
line_length=120
indent = " "
# by default isort don't check module indexes
not_skip = __init__.py
# will group `import x` and `from x import` of the same module.
force_sort_within_sections = true
sections = FUTURE,STDLIB,INBETWEENS,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
default_section = THIRDPARTY
known_first_party = foosball,tests
forced_separate = tests
combine_as_imports = true
# Assume Python 3.11
target-version = "py311"

[mypy]
python_version = 3.10
ignore_errors = true
follow_imports = silent
ignore_missing_imports = true
warn_incomplete_stub = true
warn_redundant_casts = true
warn_unused_configs = true
[tool.ruff.lint]
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default.
# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or
# McCabe complexity (`C901`) by default.
select = ["E4", "E7", "E9", "F"]
ignore = E501,W503,E203,D202,W504

# Allow fix for all enabled rules (when `--fix`) is provided.
fixable = ["ALL"]
unfixable = []

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

[tool.ruff.format]
# Like Black, use double quotes for strings.
quote-style = "double"

# Like Black, indent with spaces, rather than tabs.
indent-style = "space"

# Like Black, respect magic trailing commas.
skip-magic-trailing-comma = false

# Like Black, automatically detect the appropriate line ending.
line-ending = "auto"

# Enable auto-formatting of code examples in docstrings. Markdown,
# reStructuredText code/literal blocks and doctests are all supported.
#
# This is currently disabled by default, but it is planned for this
# to be opt-out in the future.
docstring-code-format = false

# Set the line length limit used when formatting code snippets in
# docstrings.
#
# This only has an effect when the `docstring-code-format` setting is
# enabled.
docstring-code-line-length = "dynamic"

0 comments on commit ef74ee7

Please sign in to comment.