Skip to content

Commit

Permalink
Ruff pandas vet (TheAlgorithms#10281)
Browse files Browse the repository at this point in the history
* Python linting: Add ruff rules for Pandas-vet and Pytest-style

* updating DIRECTORY.md

---------

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
  • Loading branch information
cclauss and github-actions authored Oct 11, 2023
1 parent d5323db commit 3f094fe
Show file tree
Hide file tree
Showing 28 changed files with 260 additions and 241 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ repos:
- id: validate-pyproject

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.5.1
rev: v1.6.0
hooks:
- id: mypy
args:
Expand Down
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@
* [Logistic Regression](machine_learning/logistic_regression.py)
* Loss Functions
* [Binary Cross Entropy](machine_learning/loss_functions/binary_cross_entropy.py)
* [Categorical Cross Entropy](machine_learning/loss_functions/categorical_cross_entropy.py)
* [Huber Loss](machine_learning/loss_functions/huber_loss.py)
* [Mean Squared Error](machine_learning/loss_functions/mean_squared_error.py)
* [Mfcc](machine_learning/mfcc.py)
Expand Down
6 changes: 4 additions & 2 deletions blockchain/diophantine_equation.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def extended_gcd(a: int, b: int) -> tuple[int, int, int]:
(1, -2, 3)
"""
assert a >= 0 and b >= 0
assert a >= 0
assert b >= 0

if b == 0:
d, x, y = a, 1, 0
Expand All @@ -92,7 +93,8 @@ def extended_gcd(a: int, b: int) -> tuple[int, int, int]:
x = q
y = p - q * (a // b)

assert a % d == 0 and b % d == 0
assert a % d == 0
assert b % d == 0
assert d == a * x + b * y

return (d, x, y)
Expand Down
18 changes: 12 additions & 6 deletions ciphers/xor_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def encrypt(self, content: str, key: int) -> list[str]:
"""

# precondition
assert isinstance(key, int) and isinstance(content, str)
assert isinstance(key, int)
assert isinstance(content, str)

key = key or self.__key or 1

Expand All @@ -56,7 +57,8 @@ def decrypt(self, content: str, key: int) -> list[str]:
"""

# precondition
assert isinstance(key, int) and isinstance(content, list)
assert isinstance(key, int)
assert isinstance(content, list)

key = key or self.__key or 1

Expand All @@ -74,7 +76,8 @@ def encrypt_string(self, content: str, key: int = 0) -> str:
"""

# precondition
assert isinstance(key, int) and isinstance(content, str)
assert isinstance(key, int)
assert isinstance(content, str)

key = key or self.__key or 1

Expand All @@ -99,7 +102,8 @@ def decrypt_string(self, content: str, key: int = 0) -> str:
"""

# precondition
assert isinstance(key, int) and isinstance(content, str)
assert isinstance(key, int)
assert isinstance(content, str)

key = key or self.__key or 1

Expand All @@ -125,7 +129,8 @@ def encrypt_file(self, file: str, key: int = 0) -> bool:
"""

# precondition
assert isinstance(file, str) and isinstance(key, int)
assert isinstance(file, str)
assert isinstance(key, int)

try:
with open(file) as fin, open("encrypt.out", "w+") as fout:
Expand All @@ -148,7 +153,8 @@ def decrypt_file(self, file: str, key: int) -> bool:
"""

# precondition
assert isinstance(file, str) and isinstance(key, int)
assert isinstance(file, str)
assert isinstance(key, int)

try:
with open(file) as fin, open("decrypt.out", "w+") as fout:
Expand Down
3 changes: 2 additions & 1 deletion conversions/decimal_to_hexadecimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ def decimal_to_hexadecimal(decimal: float) -> str:
>>> decimal_to_hexadecimal(-256) == hex(-256)
True
"""
assert type(decimal) in (int, float) and decimal == int(decimal)
assert isinstance(decimal, (int, float))
assert decimal == int(decimal)
decimal = int(decimal)
hexadecimal = ""
negative = False
Expand Down
28 changes: 15 additions & 13 deletions data_structures/binary_tree/binary_search_tree_recursive.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import unittest
from collections.abc import Iterator

import pytest


class Node:
def __init__(self, label: int, parent: Node | None) -> None:
Expand Down Expand Up @@ -78,7 +80,7 @@ def _put(self, node: Node | None, label: int, parent: Node | None = None) -> Nod
node.right = self._put(node.right, label, node)
else:
msg = f"Node with label {label} already exists"
raise Exception(msg)
raise ValueError(msg)

return node

Expand All @@ -95,14 +97,14 @@ def search(self, label: int) -> Node:
>>> node = t.search(3)
Traceback (most recent call last):
...
Exception: Node with label 3 does not exist
ValueError: Node with label 3 does not exist
"""
return self._search(self.root, label)

def _search(self, node: Node | None, label: int) -> Node:
if node is None:
msg = f"Node with label {label} does not exist"
raise Exception(msg)
raise ValueError(msg)
else:
if label < node.label:
node = self._search(node.left, label)
Expand All @@ -124,7 +126,7 @@ def remove(self, label: int) -> None:
>>> t.remove(3)
Traceback (most recent call last):
...
Exception: Node with label 3 does not exist
ValueError: Node with label 3 does not exist
"""
node = self.search(label)
if node.right and node.left:
Expand Down Expand Up @@ -179,7 +181,7 @@ def exists(self, label: int) -> bool:
try:
self.search(label)
return True
except Exception:
except ValueError:
return False

def get_max_label(self) -> int:
Expand All @@ -190,15 +192,15 @@ def get_max_label(self) -> int:
>>> t.get_max_label()
Traceback (most recent call last):
...
Exception: Binary search tree is empty
ValueError: Binary search tree is empty
>>> t.put(8)
>>> t.put(10)
>>> t.get_max_label()
10
"""
if self.root is None:
raise Exception("Binary search tree is empty")
raise ValueError("Binary search tree is empty")

node = self.root
while node.right is not None:
Expand All @@ -214,15 +216,15 @@ def get_min_label(self) -> int:
>>> t.get_min_label()
Traceback (most recent call last):
...
Exception: Binary search tree is empty
ValueError: Binary search tree is empty
>>> t.put(8)
>>> t.put(10)
>>> t.get_min_label()
8
"""
if self.root is None:
raise Exception("Binary search tree is empty")
raise ValueError("Binary search tree is empty")

node = self.root
while node.left is not None:
Expand Down Expand Up @@ -359,7 +361,7 @@ def test_put(self) -> None:
assert t.root.left.left.parent == t.root.left
assert t.root.left.left.label == 1

with self.assertRaises(Exception): # noqa: B017
with pytest.raises(ValueError):
t.put(1)

def test_search(self) -> None:
Expand All @@ -371,7 +373,7 @@ def test_search(self) -> None:
node = t.search(13)
assert node.label == 13

with self.assertRaises(Exception): # noqa: B017
with pytest.raises(ValueError):
t.search(2)

def test_remove(self) -> None:
Expand Down Expand Up @@ -517,7 +519,7 @@ def test_get_max_label(self) -> None:
assert t.get_max_label() == 14

t.empty()
with self.assertRaises(Exception): # noqa: B017
with pytest.raises(ValueError):
t.get_max_label()

def test_get_min_label(self) -> None:
Expand All @@ -526,7 +528,7 @@ def test_get_min_label(self) -> None:
assert t.get_min_label() == 1

t.empty()
with self.assertRaises(Exception): # noqa: B017
with pytest.raises(ValueError):
t.get_min_label()

def test_inorder_traversal(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions data_structures/hashing/tests/test_hash_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ def _run_operation(obj, fun, *args):

@pytest.mark.parametrize(
"operations",
(
[
pytest.param(_add_items, id="add items"),
pytest.param(_overwrite_items, id="overwrite items"),
pytest.param(_delete_items, id="delete items"),
pytest.param(_access_absent_items, id="access absent items"),
pytest.param(_add_with_resize_up, id="add with resize up"),
pytest.param(_add_with_resize_down, id="add with resize down"),
),
],
)
def test_hash_map_is_the_same_as_dict(operations):
my = HashMap(initial_block_size=4)
Expand Down
6 changes: 4 additions & 2 deletions data_structures/linked_list/circular_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ def delete_nth(self, index: int = 0) -> Any:
if not 0 <= index < len(self):
raise IndexError("list index out of range.")

assert self.head is not None and self.tail is not None
assert self.head is not None
assert self.tail is not None
delete_node: Node = self.head
if self.head == self.tail: # Just one node
self.head = self.tail = None
Expand All @@ -137,7 +138,8 @@ def delete_nth(self, index: int = 0) -> Any:
for _ in range(index - 1):
assert temp is not None
temp = temp.next
assert temp is not None and temp.next is not None
assert temp is not None
assert temp.next is not None
delete_node = temp.next
temp.next = temp.next.next
if index == len(self) - 1: # Delete at tail
Expand Down
3 changes: 2 additions & 1 deletion digital_image_processing/test_digital_image_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ def test_median_filter():

def test_sobel_filter():
grad, theta = sob.sobel_filter(gray)
assert grad.any() and theta.any()
assert grad.any()
assert theta.any()


def test_sepia():
Expand Down
Loading

0 comments on commit 3f094fe

Please sign in to comment.