mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Ruff pandas vet (#10281)
* 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>
This commit is contained in:
@@ -12,6 +12,8 @@ from __future__ import annotations
|
||||
import unittest
|
||||
from collections.abc import Iterator
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
class Node:
|
||||
def __init__(self, label: int, parent: Node | None) -> None:
|
||||
@@ -78,7 +80,7 @@ class BinarySearchTree:
|
||||
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
|
||||
|
||||
@@ -95,14 +97,14 @@ class BinarySearchTree:
|
||||
>>> 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)
|
||||
@@ -124,7 +126,7 @@ class BinarySearchTree:
|
||||
>>> 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:
|
||||
@@ -179,7 +181,7 @@ class BinarySearchTree:
|
||||
try:
|
||||
self.search(label)
|
||||
return True
|
||||
except Exception:
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
def get_max_label(self) -> int:
|
||||
@@ -190,7 +192,7 @@ class BinarySearchTree:
|
||||
>>> 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)
|
||||
@@ -198,7 +200,7 @@ class BinarySearchTree:
|
||||
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:
|
||||
@@ -214,7 +216,7 @@ class BinarySearchTree:
|
||||
>>> 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)
|
||||
@@ -222,7 +224,7 @@ class BinarySearchTree:
|
||||
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:
|
||||
@@ -359,7 +361,7 @@ class BinarySearchTreeTest(unittest.TestCase):
|
||||
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:
|
||||
@@ -371,7 +373,7 @@ class BinarySearchTreeTest(unittest.TestCase):
|
||||
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:
|
||||
@@ -517,7 +519,7 @@ class BinarySearchTreeTest(unittest.TestCase):
|
||||
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:
|
||||
@@ -526,7 +528,7 @@ class BinarySearchTreeTest(unittest.TestCase):
|
||||
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:
|
||||
|
||||
@@ -65,14 +65,14 @@ _add_with_resize_down = [
|
||||
|
||||
@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)
|
||||
|
||||
@@ -124,7 +124,8 @@ class CircularLinkedList:
|
||||
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
|
||||
@@ -137,7 +138,8 @@ class CircularLinkedList:
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user