mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 01:09:40 +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:
@ -22,6 +22,8 @@ import unittest
|
||||
from pprint import pformat
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
import pytest
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
@ -185,9 +187,9 @@ class TestGraphAdjacencyList(unittest.TestCase):
|
||||
directed_graph: GraphAdjacencyList,
|
||||
edge: list[int],
|
||||
) -> None:
|
||||
self.assertTrue(undirected_graph.contains_edge(edge[0], edge[1]))
|
||||
self.assertTrue(undirected_graph.contains_edge(edge[1], edge[0]))
|
||||
self.assertTrue(directed_graph.contains_edge(edge[0], edge[1]))
|
||||
assert undirected_graph.contains_edge(edge[0], edge[1])
|
||||
assert undirected_graph.contains_edge(edge[1], edge[0])
|
||||
assert directed_graph.contains_edge(edge[0], edge[1])
|
||||
|
||||
def __assert_graph_edge_does_not_exist_check(
|
||||
self,
|
||||
@ -195,9 +197,9 @@ class TestGraphAdjacencyList(unittest.TestCase):
|
||||
directed_graph: GraphAdjacencyList,
|
||||
edge: list[int],
|
||||
) -> None:
|
||||
self.assertFalse(undirected_graph.contains_edge(edge[0], edge[1]))
|
||||
self.assertFalse(undirected_graph.contains_edge(edge[1], edge[0]))
|
||||
self.assertFalse(directed_graph.contains_edge(edge[0], edge[1]))
|
||||
assert not undirected_graph.contains_edge(edge[0], edge[1])
|
||||
assert not undirected_graph.contains_edge(edge[1], edge[0])
|
||||
assert not directed_graph.contains_edge(edge[0], edge[1])
|
||||
|
||||
def __assert_graph_vertex_exists_check(
|
||||
self,
|
||||
@ -205,8 +207,8 @@ class TestGraphAdjacencyList(unittest.TestCase):
|
||||
directed_graph: GraphAdjacencyList,
|
||||
vertex: int,
|
||||
) -> None:
|
||||
self.assertTrue(undirected_graph.contains_vertex(vertex))
|
||||
self.assertTrue(directed_graph.contains_vertex(vertex))
|
||||
assert undirected_graph.contains_vertex(vertex)
|
||||
assert directed_graph.contains_vertex(vertex)
|
||||
|
||||
def __assert_graph_vertex_does_not_exist_check(
|
||||
self,
|
||||
@ -214,13 +216,13 @@ class TestGraphAdjacencyList(unittest.TestCase):
|
||||
directed_graph: GraphAdjacencyList,
|
||||
vertex: int,
|
||||
) -> None:
|
||||
self.assertFalse(undirected_graph.contains_vertex(vertex))
|
||||
self.assertFalse(directed_graph.contains_vertex(vertex))
|
||||
assert not undirected_graph.contains_vertex(vertex)
|
||||
assert not directed_graph.contains_vertex(vertex)
|
||||
|
||||
def __generate_random_edges(
|
||||
self, vertices: list[int], edge_pick_count: int
|
||||
) -> list[list[int]]:
|
||||
self.assertTrue(edge_pick_count <= len(vertices))
|
||||
assert edge_pick_count <= len(vertices)
|
||||
|
||||
random_source_vertices: list[int] = random.sample(
|
||||
vertices[0 : int(len(vertices) / 2)], edge_pick_count
|
||||
@ -281,8 +283,8 @@ class TestGraphAdjacencyList(unittest.TestCase):
|
||||
self.__assert_graph_edge_exists_check(
|
||||
undirected_graph, directed_graph, edge
|
||||
)
|
||||
self.assertFalse(undirected_graph.directed)
|
||||
self.assertTrue(directed_graph.directed)
|
||||
assert not undirected_graph.directed
|
||||
assert directed_graph.directed
|
||||
|
||||
def test_contains_vertex(self) -> None:
|
||||
random_vertices: list[int] = random.sample(range(101), 20)
|
||||
@ -297,12 +299,8 @@ class TestGraphAdjacencyList(unittest.TestCase):
|
||||
|
||||
# Test contains_vertex
|
||||
for num in range(101):
|
||||
self.assertEqual(
|
||||
num in random_vertices, undirected_graph.contains_vertex(num)
|
||||
)
|
||||
self.assertEqual(
|
||||
num in random_vertices, directed_graph.contains_vertex(num)
|
||||
)
|
||||
assert (num in random_vertices) == undirected_graph.contains_vertex(num)
|
||||
assert (num in random_vertices) == directed_graph.contains_vertex(num)
|
||||
|
||||
def test_add_vertices(self) -> None:
|
||||
random_vertices: list[int] = random.sample(range(101), 20)
|
||||
@ -507,9 +505,9 @@ class TestGraphAdjacencyList(unittest.TestCase):
|
||||
) = self.__generate_graphs(20, 0, 100, 4)
|
||||
|
||||
for vertex in random_vertices:
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
undirected_graph.add_vertex(vertex)
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
directed_graph.add_vertex(vertex)
|
||||
|
||||
def test_remove_vertex_exception_check(self) -> None:
|
||||
@ -522,9 +520,9 @@ class TestGraphAdjacencyList(unittest.TestCase):
|
||||
|
||||
for i in range(101):
|
||||
if i not in random_vertices:
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
undirected_graph.remove_vertex(i)
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
directed_graph.remove_vertex(i)
|
||||
|
||||
def test_add_edge_exception_check(self) -> None:
|
||||
@ -536,9 +534,9 @@ class TestGraphAdjacencyList(unittest.TestCase):
|
||||
) = self.__generate_graphs(20, 0, 100, 4)
|
||||
|
||||
for edge in random_edges:
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
undirected_graph.add_edge(edge[0], edge[1])
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
directed_graph.add_edge(edge[0], edge[1])
|
||||
|
||||
def test_remove_edge_exception_check(self) -> None:
|
||||
@ -560,9 +558,9 @@ class TestGraphAdjacencyList(unittest.TestCase):
|
||||
more_random_edges.append(edge)
|
||||
|
||||
for edge in more_random_edges:
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
undirected_graph.remove_edge(edge[0], edge[1])
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
directed_graph.remove_edge(edge[0], edge[1])
|
||||
|
||||
def test_contains_edge_exception_check(self) -> None:
|
||||
@ -574,14 +572,14 @@ class TestGraphAdjacencyList(unittest.TestCase):
|
||||
) = self.__generate_graphs(20, 0, 100, 4)
|
||||
|
||||
for vertex in random_vertices:
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
undirected_graph.contains_edge(vertex, 102)
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
directed_graph.contains_edge(vertex, 102)
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
undirected_graph.contains_edge(103, 102)
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
directed_graph.contains_edge(103, 102)
|
||||
|
||||
|
||||
|
@ -22,6 +22,8 @@ import unittest
|
||||
from pprint import pformat
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
import pytest
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
@ -203,9 +205,9 @@ class TestGraphMatrix(unittest.TestCase):
|
||||
directed_graph: GraphAdjacencyMatrix,
|
||||
edge: list[int],
|
||||
) -> None:
|
||||
self.assertTrue(undirected_graph.contains_edge(edge[0], edge[1]))
|
||||
self.assertTrue(undirected_graph.contains_edge(edge[1], edge[0]))
|
||||
self.assertTrue(directed_graph.contains_edge(edge[0], edge[1]))
|
||||
assert undirected_graph.contains_edge(edge[0], edge[1])
|
||||
assert undirected_graph.contains_edge(edge[1], edge[0])
|
||||
assert directed_graph.contains_edge(edge[0], edge[1])
|
||||
|
||||
def __assert_graph_edge_does_not_exist_check(
|
||||
self,
|
||||
@ -213,9 +215,9 @@ class TestGraphMatrix(unittest.TestCase):
|
||||
directed_graph: GraphAdjacencyMatrix,
|
||||
edge: list[int],
|
||||
) -> None:
|
||||
self.assertFalse(undirected_graph.contains_edge(edge[0], edge[1]))
|
||||
self.assertFalse(undirected_graph.contains_edge(edge[1], edge[0]))
|
||||
self.assertFalse(directed_graph.contains_edge(edge[0], edge[1]))
|
||||
assert not undirected_graph.contains_edge(edge[0], edge[1])
|
||||
assert not undirected_graph.contains_edge(edge[1], edge[0])
|
||||
assert not directed_graph.contains_edge(edge[0], edge[1])
|
||||
|
||||
def __assert_graph_vertex_exists_check(
|
||||
self,
|
||||
@ -223,8 +225,8 @@ class TestGraphMatrix(unittest.TestCase):
|
||||
directed_graph: GraphAdjacencyMatrix,
|
||||
vertex: int,
|
||||
) -> None:
|
||||
self.assertTrue(undirected_graph.contains_vertex(vertex))
|
||||
self.assertTrue(directed_graph.contains_vertex(vertex))
|
||||
assert undirected_graph.contains_vertex(vertex)
|
||||
assert directed_graph.contains_vertex(vertex)
|
||||
|
||||
def __assert_graph_vertex_does_not_exist_check(
|
||||
self,
|
||||
@ -232,13 +234,13 @@ class TestGraphMatrix(unittest.TestCase):
|
||||
directed_graph: GraphAdjacencyMatrix,
|
||||
vertex: int,
|
||||
) -> None:
|
||||
self.assertFalse(undirected_graph.contains_vertex(vertex))
|
||||
self.assertFalse(directed_graph.contains_vertex(vertex))
|
||||
assert not undirected_graph.contains_vertex(vertex)
|
||||
assert not directed_graph.contains_vertex(vertex)
|
||||
|
||||
def __generate_random_edges(
|
||||
self, vertices: list[int], edge_pick_count: int
|
||||
) -> list[list[int]]:
|
||||
self.assertTrue(edge_pick_count <= len(vertices))
|
||||
assert edge_pick_count <= len(vertices)
|
||||
|
||||
random_source_vertices: list[int] = random.sample(
|
||||
vertices[0 : int(len(vertices) / 2)], edge_pick_count
|
||||
@ -300,8 +302,8 @@ class TestGraphMatrix(unittest.TestCase):
|
||||
undirected_graph, directed_graph, edge
|
||||
)
|
||||
|
||||
self.assertFalse(undirected_graph.directed)
|
||||
self.assertTrue(directed_graph.directed)
|
||||
assert not undirected_graph.directed
|
||||
assert directed_graph.directed
|
||||
|
||||
def test_contains_vertex(self) -> None:
|
||||
random_vertices: list[int] = random.sample(range(101), 20)
|
||||
@ -316,12 +318,8 @@ class TestGraphMatrix(unittest.TestCase):
|
||||
|
||||
# Test contains_vertex
|
||||
for num in range(101):
|
||||
self.assertEqual(
|
||||
num in random_vertices, undirected_graph.contains_vertex(num)
|
||||
)
|
||||
self.assertEqual(
|
||||
num in random_vertices, directed_graph.contains_vertex(num)
|
||||
)
|
||||
assert (num in random_vertices) == undirected_graph.contains_vertex(num)
|
||||
assert (num in random_vertices) == directed_graph.contains_vertex(num)
|
||||
|
||||
def test_add_vertices(self) -> None:
|
||||
random_vertices: list[int] = random.sample(range(101), 20)
|
||||
@ -526,9 +524,9 @@ class TestGraphMatrix(unittest.TestCase):
|
||||
) = self.__generate_graphs(20, 0, 100, 4)
|
||||
|
||||
for vertex in random_vertices:
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
undirected_graph.add_vertex(vertex)
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
directed_graph.add_vertex(vertex)
|
||||
|
||||
def test_remove_vertex_exception_check(self) -> None:
|
||||
@ -541,9 +539,9 @@ class TestGraphMatrix(unittest.TestCase):
|
||||
|
||||
for i in range(101):
|
||||
if i not in random_vertices:
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
undirected_graph.remove_vertex(i)
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
directed_graph.remove_vertex(i)
|
||||
|
||||
def test_add_edge_exception_check(self) -> None:
|
||||
@ -555,9 +553,9 @@ class TestGraphMatrix(unittest.TestCase):
|
||||
) = self.__generate_graphs(20, 0, 100, 4)
|
||||
|
||||
for edge in random_edges:
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
undirected_graph.add_edge(edge[0], edge[1])
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
directed_graph.add_edge(edge[0], edge[1])
|
||||
|
||||
def test_remove_edge_exception_check(self) -> None:
|
||||
@ -579,9 +577,9 @@ class TestGraphMatrix(unittest.TestCase):
|
||||
more_random_edges.append(edge)
|
||||
|
||||
for edge in more_random_edges:
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
undirected_graph.remove_edge(edge[0], edge[1])
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
directed_graph.remove_edge(edge[0], edge[1])
|
||||
|
||||
def test_contains_edge_exception_check(self) -> None:
|
||||
@ -593,14 +591,14 @@ class TestGraphMatrix(unittest.TestCase):
|
||||
) = self.__generate_graphs(20, 0, 100, 4)
|
||||
|
||||
for vertex in random_vertices:
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
undirected_graph.contains_edge(vertex, 102)
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
directed_graph.contains_edge(vertex, 102)
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
undirected_graph.contains_edge(103, 102)
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
directed_graph.contains_edge(103, 102)
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user