Skip to content

Commit c96241b

Browse files
cclaussgithub-actions
and
github-actions
authored
Replace bandit, flake8, isort, and pyupgrade with ruff (TheAlgorithms#8178)
* Replace bandit, flake8, isort, and pyupgrade with ruff * Comment on ruff rules * updating DIRECTORY.md --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent adc3ccd commit c96241b

21 files changed

+127
-132
lines changed

.flake8

-10
This file was deleted.

.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 .

.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

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

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(

data_structures/heap/heap_generic.py

-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ def test_heap() -> None:
166166
>>> h.get_top()
167167
[9, -40]
168168
"""
169-
pass
170169

171170

172171
if __name__ == "__main__":

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:

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

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'

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():

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")

maths/eulers_totient.py

+15-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
# Eulers Totient function finds the number of relative primes of a number n from 1 to n
22
def totient(n: int) -> list:
3+
"""
4+
>>> n = 10
5+
>>> totient_calculation = totient(n)
6+
>>> for i in range(1, n):
7+
... print(f"{i} has {totient_calculation[i]} relative primes.")
8+
1 has 0 relative primes.
9+
2 has 1 relative primes.
10+
3 has 2 relative primes.
11+
4 has 2 relative primes.
12+
5 has 4 relative primes.
13+
6 has 2 relative primes.
14+
7 has 6 relative primes.
15+
8 has 4 relative primes.
16+
9 has 6 relative primes.
17+
"""
318
is_prime = [True for i in range(n + 1)]
419
totients = [i - 1 for i in range(n + 1)]
520
primes = []
@@ -20,25 +35,6 @@ def totient(n: int) -> list:
2035
return totients
2136

2237

23-
def test_totient() -> None:
24-
"""
25-
>>> n = 10
26-
>>> totient_calculation = totient(n)
27-
>>> for i in range(1, n):
28-
... print(f"{i} has {totient_calculation[i]} relative primes.")
29-
1 has 0 relative primes.
30-
2 has 1 relative primes.
31-
3 has 2 relative primes.
32-
4 has 2 relative primes.
33-
5 has 4 relative primes.
34-
6 has 2 relative primes.
35-
7 has 6 relative primes.
36-
8 has 4 relative primes.
37-
9 has 6 relative primes.
38-
"""
39-
pass
40-
41-
4238
if __name__ == "__main__":
4339
import doctest
4440

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

maths/pythagoras.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,13 @@ def __repr__(self) -> str:
1414

1515

1616
def distance(a: Point, b: Point) -> float:
17-
return math.sqrt(abs((b.x - a.x) ** 2 + (b.y - a.y) ** 2 + (b.z - a.z) ** 2))
18-
19-
20-
def test_distance() -> None:
2117
"""
2218
>>> point1 = Point(2, -1, 7)
2319
>>> point2 = Point(1, -3, 5)
2420
>>> print(f"Distance from {point1} to {point2} is {distance(point1, point2)}")
2521
Distance from Point(2, -1, 7) to Point(1, -3, 5) is 3.0
2622
"""
27-
pass
23+
return math.sqrt(abs((b.x - a.x) ** 2 + (b.y - a.y) ** 2 + (b.z - a.z) ** 2))
2824

2925

3026
if __name__ == "__main__":

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

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):

0 commit comments

Comments
 (0)