Skip to content

Replace bandit, flake8, isort, and pyupgrade with ruff #8178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions .flake8

This file was deleted.

16 changes: 16 additions & 0 deletions .github/workflows/ruff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# https://beta.ruff.rs
name: ruff
on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: pip install --user ruff
- run: ruff --format=github .
78 changes: 19 additions & 59 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ repos:
rev: v4.4.0
hooks:
- id: check-executables-have-shebangs
- id: check-toml
- id: check-yaml
- id: end-of-file-fixer
types: [python]
Expand All @@ -14,60 +15,41 @@ repos:
hooks:
- id: auto-walrus

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.255
hooks:
- id: ruff

- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black

- repo: https://github.com/PyCQA/isort
rev: 5.12.0
- repo: https://github.com/codespell-project/codespell
rev: v2.2.4
hooks:
- id: isort
args:
- --profile=black
- id: codespell
additional_dependencies:
- tomli

- repo: https://github.com/tox-dev/pyproject-fmt
rev: "0.9.2"
hooks:
- id: pyproject-fmt

- repo: local
hooks:
- id: validate-filenames
name: Validate filenames
entry: ./scripts/validate_filenames.py
language: script
pass_filenames: false

- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.12.1
hooks:
- id: validate-pyproject

- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
hooks:
- id: pyupgrade
args:
- --py311-plus

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.255
hooks:
- id: ruff
args:
- --ignore=E741

- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
- id: flake8 # See .flake8 for args
additional_dependencies: &flake8-plugins
- flake8-bugbear
- flake8-builtins
# - flake8-broken-line
- flake8-comprehensions
- pep8-naming

- repo: https://github.com/asottile/yesqa
rev: v1.4.0
hooks:
- id: yesqa
additional_dependencies:
*flake8-plugins

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.1.1
hooks:
Expand All @@ -77,25 +59,3 @@ repos:
- --install-types # See mirrors-mypy README.md
- --non-interactive
additional_dependencies: [types-requests]

- repo: https://github.com/codespell-project/codespell
rev: v2.2.4
hooks:
- id: codespell
args:
- --ignore-words-list=3rt,ans,crate,damon,fo,followings,hist,iff,kwanza,mater,secant,som,sur,tim,zar
exclude: |
(?x)^(
ciphers/prehistoric_men.txt |
strings/dictionary.txt |
strings/words.txt |
project_euler/problem_022/p022_names.txt
)$
- repo: local
hooks:
- id: validate-filenames
name: Validate filenames
entry: ./scripts/validate_filenames.py
language: script
pass_filenames: false
3 changes: 3 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,14 @@
* [Disjoint Set](data_structures/disjoint_set/disjoint_set.py)
* Hashing
* [Double Hash](data_structures/hashing/double_hash.py)
* [Hash Map](data_structures/hashing/hash_map.py)
* [Hash Table](data_structures/hashing/hash_table.py)
* [Hash Table With Linked List](data_structures/hashing/hash_table_with_linked_list.py)
* Number Theory
* [Prime Numbers](data_structures/hashing/number_theory/prime_numbers.py)
* [Quadratic Probing](data_structures/hashing/quadratic_probing.py)
* Tests
* [Test Hash Map](data_structures/hashing/tests/test_hash_map.py)
* Heap
* [Binomial Heap](data_structures/heap/binomial_heap.py)
* [Heap](data_structures/heap/heap.py)
Expand Down
2 changes: 1 addition & 1 deletion arithmetic_analysis/newton_raphson.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from __future__ import annotations

from decimal import Decimal
from math import * # noqa: F401, F403
from math import * # noqa: F403

from sympy import diff

Expand Down
2 changes: 1 addition & 1 deletion arithmetic_analysis/newton_raphson_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# Newton's Method - https://en.wikipedia.org/wiki/Newton's_method

from sympy import diff, lambdify, symbols
from sympy.functions import * # noqa: F401, F403
from sympy.functions import * # noqa: F403


def newton_raphson(
Expand Down
1 change: 0 additions & 1 deletion data_structures/heap/heap_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ def test_heap() -> None:
>>> h.get_top()
[9, -40]
"""
pass


if __name__ == "__main__":
Expand Down
9 changes: 3 additions & 6 deletions dynamic_programming/min_distance_up_bottom.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
The aim is to demonstate up bottom approach for solving the task.
The implementation was tested on the
leetcode: https://leetcode.com/problems/edit-distance/
"""
"""
Levinstein distance
Dynamic Programming: up -> down.
"""

import functools


def min_distance_up_bottom(word1: str, word2: str) -> int:
"""
Expand All @@ -25,13 +25,10 @@ def min_distance_up_bottom(word1: str, word2: str) -> int:
>>> min_distance_up_bottom("zooicoarchaeologist", "zoologist")
10
"""

from functools import lru_cache

len_word1 = len(word1)
len_word2 = len(word2)

@lru_cache(maxsize=None)
@functools.cache
def min_distance(index1: int, index2: int) -> int:
# if first word index is overflow - delete all from the second word
if index1 >= len_word1:
Expand Down
4 changes: 2 additions & 2 deletions dynamic_programming/minimum_tickets_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
Dynamic Programming: up -> down.
"""

from functools import lru_cache
import functools


def mincost_tickets(days: list[int], costs: list[int]) -> int:
Expand Down Expand Up @@ -106,7 +106,7 @@ def mincost_tickets(days: list[int], costs: list[int]) -> int:

days_set = set(days)

@lru_cache(maxsize=None)
@functools.cache
def dynamic_programming(index: int) -> int:
if index > 365:
return 0
Expand Down
4 changes: 2 additions & 2 deletions dynamic_programming/word_break.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Space: O(n)
"""

from functools import lru_cache
import functools
from typing import Any


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

# Dynamic programming method
@lru_cache(maxsize=None)
@functools.cache
def is_breakable(index: int) -> bool:
"""
>>> string = 'a'
Expand Down
12 changes: 3 additions & 9 deletions hashes/sha1.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import argparse
import hashlib # hashlib is only used inside the Test class
import struct
import unittest


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


class SHA1HashTest(unittest.TestCase):
"""
Test class for the SHA1Hash class. Inherits the TestCase class from unittest
"""

def testMatchHashes(self): # noqa: N802
msg = bytes("Test String", "utf-8")
self.assertEqual(SHA1Hash(msg).final_hash(), hashlib.sha1(msg).hexdigest())
def test_sha1_hash():
msg = b"Test String"
assert SHA1Hash(msg).final_hash() == hashlib.sha1(msg).hexdigest() # noqa: S324


def main():
Expand Down
4 changes: 2 additions & 2 deletions machine_learning/support_vector_machines.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def __init__(
*,
regularization: float = np.inf,
kernel: str = "linear",
gamma: float = 0,
gamma: float = 0.0,
) -> None:
self.regularization = regularization
self.gamma = gamma
Expand All @@ -65,7 +65,7 @@ def __init__(
elif kernel == "rbf":
if self.gamma == 0:
raise ValueError("rbf kernel requires gamma")
if not (isinstance(self.gamma, float) or isinstance(self.gamma, int)):
if not isinstance(self.gamma, (float, int)):
raise ValueError("gamma must be float or int")
if not self.gamma > 0:
raise ValueError("gamma must be > 0")
Expand Down
34 changes: 15 additions & 19 deletions maths/eulers_totient.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Eulers Totient function finds the number of relative primes of a number n from 1 to n
def totient(n: int) -> list:
"""
>>> n = 10
>>> totient_calculation = totient(n)
>>> for i in range(1, n):
... print(f"{i} has {totient_calculation[i]} relative primes.")
1 has 0 relative primes.
2 has 1 relative primes.
3 has 2 relative primes.
4 has 2 relative primes.
5 has 4 relative primes.
6 has 2 relative primes.
7 has 6 relative primes.
8 has 4 relative primes.
9 has 6 relative primes.
"""
is_prime = [True for i in range(n + 1)]
totients = [i - 1 for i in range(n + 1)]
primes = []
Expand All @@ -20,25 +35,6 @@ def totient(n: int) -> list:
return totients


def test_totient() -> None:
"""
>>> n = 10
>>> totient_calculation = totient(n)
>>> for i in range(1, n):
... print(f"{i} has {totient_calculation[i]} relative primes.")
1 has 0 relative primes.
2 has 1 relative primes.
3 has 2 relative primes.
4 has 2 relative primes.
5 has 4 relative primes.
6 has 2 relative primes.
7 has 6 relative primes.
8 has 4 relative primes.
9 has 6 relative primes.
"""
pass


if __name__ == "__main__":
import doctest

Expand Down
4 changes: 2 additions & 2 deletions maths/fibonacci.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
fib_binet runtime: 0.0174 ms
"""

from functools import lru_cache
import functools
from math import sqrt
from time import time

Expand Down Expand Up @@ -110,7 +110,7 @@ def fib_recursive_cached(n: int) -> list[int]:
Exception: n is negative
"""

@lru_cache(maxsize=None)
@functools.cache
def fib_recursive_term(i: int) -> int:
"""
Calculates the i-th (0-indexed) Fibonacci number using recursion
Expand Down
6 changes: 1 addition & 5 deletions maths/pythagoras.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,13 @@ def __repr__(self) -> str:


def distance(a: Point, b: Point) -> float:
return math.sqrt(abs((b.x - a.x) ** 2 + (b.y - a.y) ** 2 + (b.z - a.z) ** 2))


def test_distance() -> None:
"""
>>> point1 = Point(2, -1, 7)
>>> point2 = Point(1, -3, 5)
>>> print(f"Distance from {point1} to {point2} is {distance(point1, point2)}")
Distance from Point(2, -1, 7) to Point(1, -3, 5) is 3.0
"""
pass
return math.sqrt(abs((b.x - a.x) ** 2 + (b.y - a.y) ** 2 + (b.z - a.z) ** 2))


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions other/quine.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/python3
# ruff: noqa
"""
Quine:
Expand Down
3 changes: 1 addition & 2 deletions project_euler/problem_075/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

from collections import defaultdict
from math import gcd
from typing import DefaultDict


def solution(limit: int = 1500000) -> int:
Expand All @@ -43,7 +42,7 @@ def solution(limit: int = 1500000) -> int:
>>> solution(50000)
5502
"""
frequencies: DefaultDict = defaultdict(int)
frequencies: defaultdict = defaultdict(int)
euclid_m = 2
while 2 * euclid_m * (euclid_m + 1) <= limit:
for euclid_n in range((euclid_m % 2) + 1, euclid_m, 2):
Expand Down
Loading