Skip to content

Commit 100c0e6

Browse files
committed
Replace bandit, flake8, isort, and pyupgrade with ruff
1 parent 8959211 commit 100c0e6

File tree

17 files changed

+109
-97
lines changed

17 files changed

+109
-97
lines changed

Diff for: .github/workflows/ruff.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# https://beta.ruff.rs
2+
name: ruff
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
jobs:
11+
ruff:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
- run: pip install --user ruff
16+
- run: ruff --format=github .

Diff for: .pre-commit-config.yaml

+19-59
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ repos:
33
rev: v4.4.0
44
hooks:
55
- id: check-executables-have-shebangs
6+
- id: check-toml
67
- id: check-yaml
78
- id: end-of-file-fixer
89
types: [python]
@@ -14,60 +15,41 @@ repos:
1415
hooks:
1516
- id: auto-walrus
1617

18+
- repo: https://github.com/charliermarsh/ruff-pre-commit
19+
rev: v0.0.255
20+
hooks:
21+
- id: ruff
22+
1723
- repo: https://github.com/psf/black
1824
rev: 23.1.0
1925
hooks:
2026
- id: black
2127

22-
- repo: https://github.com/PyCQA/isort
23-
rev: 5.12.0
28+
- repo: https://github.com/codespell-project/codespell
29+
rev: v2.2.4
2430
hooks:
25-
- id: isort
26-
args:
27-
- --profile=black
31+
- id: codespell
32+
additional_dependencies:
33+
- tomli
2834

2935
- repo: https://github.com/tox-dev/pyproject-fmt
3036
rev: "0.9.2"
3137
hooks:
3238
- id: pyproject-fmt
3339

40+
- repo: local
41+
hooks:
42+
- id: validate-filenames
43+
name: Validate filenames
44+
entry: ./scripts/validate_filenames.py
45+
language: script
46+
pass_filenames: false
47+
3448
- repo: https://github.com/abravalheri/validate-pyproject
3549
rev: v0.12.1
3650
hooks:
3751
- id: validate-pyproject
3852

39-
- repo: https://github.com/asottile/pyupgrade
40-
rev: v3.3.1
41-
hooks:
42-
- id: pyupgrade
43-
args:
44-
- --py311-plus
45-
46-
- repo: https://github.com/charliermarsh/ruff-pre-commit
47-
rev: v0.0.255
48-
hooks:
49-
- id: ruff
50-
args:
51-
- --ignore=E741
52-
53-
- repo: https://github.com/PyCQA/flake8
54-
rev: 6.0.0
55-
hooks:
56-
- id: flake8 # See .flake8 for args
57-
additional_dependencies: &flake8-plugins
58-
- flake8-bugbear
59-
- flake8-builtins
60-
# - flake8-broken-line
61-
- flake8-comprehensions
62-
- pep8-naming
63-
64-
- repo: https://github.com/asottile/yesqa
65-
rev: v1.4.0
66-
hooks:
67-
- id: yesqa
68-
additional_dependencies:
69-
*flake8-plugins
70-
7153
- repo: https://github.com/pre-commit/mirrors-mypy
7254
rev: v1.1.1
7355
hooks:
@@ -77,25 +59,3 @@ repos:
7759
- --install-types # See mirrors-mypy README.md
7860
- --non-interactive
7961
additional_dependencies: [types-requests]
80-
81-
- repo: https://github.com/codespell-project/codespell
82-
rev: v2.2.4
83-
hooks:
84-
- id: codespell
85-
args:
86-
- --ignore-words-list=3rt,ans,crate,damon,fo,followings,hist,iff,kwanza,mater,secant,som,sur,tim,zar
87-
exclude: |
88-
(?x)^(
89-
ciphers/prehistoric_men.txt |
90-
strings/dictionary.txt |
91-
strings/words.txt |
92-
project_euler/problem_022/p022_names.txt
93-
)$
94-
95-
- repo: local
96-
hooks:
97-
- id: validate-filenames
98-
name: Validate filenames
99-
entry: ./scripts/validate_filenames.py
100-
language: script
101-
pass_filenames: false

Diff for: arithmetic_analysis/newton_raphson.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from __future__ import annotations
66

77
from decimal import Decimal
8-
from math import * # noqa: F401, F403
8+
from math import * # noqa: F403
99

1010
from sympy import diff
1111

Diff for: arithmetic_analysis/newton_raphson_new.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# Newton's Method - https://en.wikipedia.org/wiki/Newton's_method
99

1010
from sympy import diff, lambdify, symbols
11-
from sympy.functions import * # noqa: F401, F403
11+
from sympy.functions import * # noqa: F403
1212

1313

1414
def newton_raphson(

Diff for: dynamic_programming/min_distance_up_bottom.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
The aim is to demonstate up bottom approach for solving the task.
77
The implementation was tested on the
88
leetcode: https://leetcode.com/problems/edit-distance/
9-
"""
109
11-
"""
1210
Levinstein distance
1311
Dynamic Programming: up -> down.
1412
"""
1513

14+
import functools
15+
1616

1717
def min_distance_up_bottom(word1: str, word2: str) -> int:
1818
"""
@@ -25,13 +25,10 @@ def min_distance_up_bottom(word1: str, word2: str) -> int:
2525
>>> min_distance_up_bottom("zooicoarchaeologist", "zoologist")
2626
10
2727
"""
28-
29-
from functools import lru_cache
30-
3128
len_word1 = len(word1)
3229
len_word2 = len(word2)
3330

34-
@lru_cache(maxsize=None)
31+
@functools.cache
3532
def min_distance(index1: int, index2: int) -> int:
3633
# if first word index is overflow - delete all from the second word
3734
if index1 >= len_word1:

Diff for: dynamic_programming/minimum_tickets_cost.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
Dynamic Programming: up -> down.
2323
"""
2424

25-
from functools import lru_cache
25+
import functools
2626

2727

2828
def mincost_tickets(days: list[int], costs: list[int]) -> int:
@@ -106,7 +106,7 @@ def mincost_tickets(days: list[int], costs: list[int]) -> int:
106106

107107
days_set = set(days)
108108

109-
@lru_cache(maxsize=None)
109+
@functools.cache
110110
def dynamic_programming(index: int) -> int:
111111
if index > 365:
112112
return 0

Diff for: dynamic_programming/word_break.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
Space: O(n)
2121
"""
2222

23-
from functools import lru_cache
23+
import functools
2424
from typing import Any
2525

2626

@@ -80,7 +80,7 @@ def word_break(string: str, words: list[str]) -> bool:
8080
len_string = len(string)
8181

8282
# Dynamic programming method
83-
@lru_cache(maxsize=None)
83+
@functools.cache
8484
def is_breakable(index: int) -> bool:
8585
"""
8686
>>> string = 'a'

Diff for: hashes/sha1.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import argparse
2727
import hashlib # hashlib is only used inside the Test class
2828
import struct
29-
import unittest
3029

3130

3231
class SHA1Hash:
@@ -128,14 +127,9 @@ def final_hash(self):
128127
return "%08x%08x%08x%08x%08x" % tuple(self.h)
129128

130129

131-
class SHA1HashTest(unittest.TestCase):
132-
"""
133-
Test class for the SHA1Hash class. Inherits the TestCase class from unittest
134-
"""
135-
136-
def testMatchHashes(self): # noqa: N802
137-
msg = bytes("Test String", "utf-8")
138-
self.assertEqual(SHA1Hash(msg).final_hash(), hashlib.sha1(msg).hexdigest())
130+
def test_sha1_hash():
131+
msg = b"Test String"
132+
assert SHA1Hash(msg).final_hash() == hashlib.sha1(msg).hexdigest() # noqa: S324
139133

140134

141135
def main():

Diff for: machine_learning/support_vector_machines.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def __init__(
5656
*,
5757
regularization: float = np.inf,
5858
kernel: str = "linear",
59-
gamma: float = 0,
59+
gamma: float = 0.0,
6060
) -> None:
6161
self.regularization = regularization
6262
self.gamma = gamma
@@ -65,7 +65,7 @@ def __init__(
6565
elif kernel == "rbf":
6666
if self.gamma == 0:
6767
raise ValueError("rbf kernel requires gamma")
68-
if not (isinstance(self.gamma, float) or isinstance(self.gamma, int)):
68+
if not isinstance(self.gamma, (float, int)):
6969
raise ValueError("gamma must be float or int")
7070
if not self.gamma > 0:
7171
raise ValueError("gamma must be > 0")

Diff for: maths/fibonacci.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
fib_binet runtime: 0.0174 ms
1717
"""
1818

19-
from functools import lru_cache
19+
import functools
2020
from math import sqrt
2121
from time import time
2222

@@ -110,7 +110,7 @@ def fib_recursive_cached(n: int) -> list[int]:
110110
Exception: n is negative
111111
"""
112112

113-
@lru_cache(maxsize=None)
113+
@functools.cache
114114
def fib_recursive_term(i: int) -> int:
115115
"""
116116
Calculates the i-th (0-indexed) Fibonacci number using recursion

Diff for: other/quine.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/python3
2+
# ruff: noqa
23
"""
34
Quine:
45

Diff for: project_euler/problem_075/sol1.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
from collections import defaultdict
3131
from math import gcd
32-
from typing import DefaultDict
3332

3433

3534
def solution(limit: int = 1500000) -> int:
@@ -43,7 +42,7 @@ def solution(limit: int = 1500000) -> int:
4342
>>> solution(50000)
4443
5502
4544
"""
46-
frequencies: DefaultDict = defaultdict(int)
45+
frequencies: defaultdict = defaultdict(int)
4746
euclid_m = 2
4847
while 2 * euclid_m * (euclid_m + 1) <= limit:
4948
for euclid_n in range((euclid_m % 2) + 1, euclid_m, 2):

Diff for: pyproject.toml

+52-5
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,55 @@ addopts = [
1212
omit = [".env/*"]
1313
sort = "Cover"
1414

15-
#[report]
16-
#sort = Cover
17-
#omit =
18-
# .env/*
19-
# backtracking/*
15+
[tool.codespell]
16+
ignore-words-list = "3rt,ans,crate,damon,fo,followings,hist,iff,kwanza,mater,secant,som,sur,tim,zar"
17+
skip = "./.*,*.json,ciphers/prehistoric_men.txt,project_euler/problem_022/p022_names.txt,pyproject.toml,strings/dictionary.txt,strings/words.txt"
18+
19+
[tool.ruff]
20+
ignore = [
21+
"B904",
22+
"B905",
23+
"E741",
24+
"N999",
25+
"PLC1901",
26+
"PLR2004",
27+
"PLR5501",
28+
"PLW0120",
29+
"PLW060",
30+
"PLW2901",
31+
"RUF00",
32+
"RUF100",
33+
"S101",
34+
"S105",
35+
"S113",
36+
"UP038",
37+
]
38+
select = [
39+
"A",
40+
"B",
41+
"C40",
42+
"C9",
43+
"E",
44+
"F",
45+
"I",
46+
"N",
47+
"PLC",
48+
"PLE",
49+
"PLR",
50+
"PLW",
51+
"RUF",
52+
"S",
53+
"UP",
54+
"W",
55+
"YTT",
56+
]
57+
target-version = "py311"
58+
59+
[tool.ruff.mccabe]
60+
max-complexity = 20 # default: 10
61+
62+
[tool.ruff.pylint]
63+
max-args = 10 # default: 5
64+
max-branches = 20 # default: 12
65+
max-returns = 8 # default: 6
66+
max-statements = 88 # default: 50

Diff for: sorts/external_sort.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def get_file_handles(self, filenames, buffer_size):
104104
files = {}
105105

106106
for i in range(len(filenames)):
107-
files[i] = open(filenames[i], "r", buffer_size)
107+
files[i] = open(filenames[i], buffer_size)
108108

109109
return files
110110

Diff for: strings/check_anagrams.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
wiki: https://en.wikipedia.org/wiki/Anagram
33
"""
44
from collections import defaultdict
5-
from typing import DefaultDict
65

76

87
def check_anagrams(first_str: str, second_str: str) -> bool:
@@ -30,7 +29,7 @@ def check_anagrams(first_str: str, second_str: str) -> bool:
3029
return False
3130

3231
# Default values for count should be 0
33-
count: DefaultDict[str, int] = defaultdict(int)
32+
count: defaultdict[str, int] = defaultdict(int)
3433

3534
# For each character in input strings,
3635
# increment count in the corresponding

0 commit comments

Comments
 (0)