Skip to content

Commit f24ddba

Browse files
IsxImattIpre-commit-ci[bot]tianyizheng02
authored
Implemented doctests for geometry-related classes (#12368)
* Implemented doctests for geometry-related classes * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Removed unused noqa directive * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * refactored sudoku_solver.py * refactored sudoku_solver.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * context manager for file handling changed too in from_file function --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
1 parent a2be5ad commit f24ddba

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

data_structures/arrays/sudoku_solver.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def cross(items_a, items_b):
2323
+ [cross(rs, cs) for rs in ("ABC", "DEF", "GHI") for cs in ("123", "456", "789")]
2424
)
2525
units = {s: [u for u in unitlist if s in u] for s in squares}
26-
peers = {s: set(sum(units[s], [])) - {s} for s in squares} # noqa: RUF017
26+
peers = {s: {x for u in units[s] for x in u} - {s} for s in squares}
2727

2828

2929
def test():
@@ -172,7 +172,8 @@ def unitsolved(unit):
172172

173173
def from_file(filename, sep="\n"):
174174
"Parse a file into a list of strings, separated by sep."
175-
return open(filename).read().strip().split(sep)
175+
with open(filename) as file:
176+
return file.read().strip().split(sep)
176177

177178

178179
def random_puzzle(assignments=17):

geometry/geometry.py

+29
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ class Side:
4848
Side(length=5, angle=Angle(degrees=45.6), next_side=None)
4949
>>> Side(5, Angle(45.6), Side(1, Angle(2))) # doctest: +ELLIPSIS
5050
Side(length=5, angle=Angle(degrees=45.6), next_side=Side(length=1, angle=Angle(d...
51+
>>> Side(-1)
52+
Traceback (most recent call last):
53+
...
54+
TypeError: length must be a positive numeric value.
55+
>>> Side(5, None)
56+
Traceback (most recent call last):
57+
...
58+
TypeError: angle must be an Angle object.
59+
>>> Side(5, Angle(90), "Invalid next_side")
60+
Traceback (most recent call last):
61+
...
62+
TypeError: next_side must be a Side or None.
5163
"""
5264

5365
length: float
@@ -162,6 +174,19 @@ class Polygon:
162174
163175
>>> Polygon()
164176
Polygon(sides=[])
177+
>>> polygon = Polygon()
178+
>>> polygon.add_side(Side(5)).get_side(0)
179+
Side(length=5, angle=Angle(degrees=90), next_side=None)
180+
>>> polygon.get_side(1)
181+
Traceback (most recent call last):
182+
...
183+
IndexError: list index out of range
184+
>>> polygon.set_side(0, Side(10)).get_side(0)
185+
Side(length=10, angle=Angle(degrees=90), next_side=None)
186+
>>> polygon.set_side(1, Side(10))
187+
Traceback (most recent call last):
188+
...
189+
IndexError: list assignment index out of range
165190
"""
166191

167192
sides: list[Side] = field(default_factory=list)
@@ -207,6 +232,10 @@ class Rectangle(Polygon):
207232
30
208233
>>> rectangle_one.area()
209234
50
235+
>>> Rectangle(-5, 10)
236+
Traceback (most recent call last):
237+
...
238+
TypeError: length must be a positive numeric value.
210239
"""
211240

212241
def __init__(self, short_side_length: float, long_side_length: float) -> None:

0 commit comments

Comments
 (0)