Skip to content

Commit d8a634c

Browse files
cleaned up pytest errors
1 parent 7c5bffc commit d8a634c

6 files changed

Lines changed: 59 additions & 40 deletions

File tree

conftest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Pytest configuration
2+
3+
import pytest
4+
import numpy as np
5+
6+
@pytest.fixture(scope="session", autouse=True)
7+
def setup_environment():
8+
"""Use Legacy Numpy Printing Options for consistent test outputs."""
9+
np.set_printoptions(legacy='1.25')

electricpy/geometry/__init__.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616

1717
import math
1818
from dataclasses import dataclass
19-
from typing import Iterable, Iterator, Optional, Tuple, Union
19+
from typing import Iterator, Tuple, Union
2020

2121

2222
Number = Union[int, float]
2323

2424

2525
def _as_float(x) -> float:
2626
"""
27+
Cast as a float.
28+
2729
Coerce numeric-like values (including complex with ~0 imag) to float.
2830
This defends against accidental cmath usage elsewhere in the library.
2931
"""
@@ -40,7 +42,8 @@ def _is_close(a: float, b: float, *, rel_tol: float = 1e-9, abs_tol: float = 1e-
4042

4143
@dataclass(frozen=False)
4244
class Point:
43-
"""A point in 2D space.
45+
"""
46+
A point in 2D space.
4447
4548
Parameters
4649
----------
@@ -49,6 +52,7 @@ class Point:
4952
y : float
5053
The y coordinate of the point
5154
"""
55+
5256
x: float
5357
y: float
5458

@@ -78,18 +82,18 @@ def is_close(self, other: "Point", *, tol: float = 1e-9) -> bool:
7882
return _is_close(self.x, other.x, rel_tol=tol, abs_tol=tol) and _is_close(self.y, other.y, rel_tol=tol, abs_tol=tol)
7983

8084
def __repr__(self) -> str:
85+
"""Developer representation of the Point."""
8186
return f"Point({self.x}, {self.y})"
8287

8388
def __str__(self) -> str:
89+
"""Representation of the Point."""
8490
return f"({self.x}, {self.y})"
8591

8692

8793
@dataclass(frozen=False)
8894
class Line:
89-
"""A line in 2D space in the form:
95+
"""A line in 2D space in the form (`ax + by + c = 0`)."""
9096

91-
ax + by + c = 0
92-
"""
9397
a: float
9498
b: float
9599
c: float
@@ -149,6 +153,8 @@ def intersection(self, other: object) -> Point:
149153

150154
def normalized(self) -> Tuple[float, float, float]:
151155
"""
156+
Normalize the geometry coefficients.
157+
152158
Return a normalized (a,b,c) such that sqrt(a^2+b^2)=1 and sign is stable.
153159
This helps with comparisons and distances.
154160
"""
@@ -178,6 +184,8 @@ def is_close(self, other: "Line", *, tol: float = 1e-9) -> bool:
178184

179185
def __eq__(self, other: object) -> bool:
180186
"""
187+
Evaluate the equality of geometric object.
188+
181189
Exact-ish equality (proportional coefficients), but using normalization for
182190
better behavior than raw ratio checks. This is safer than the original.
183191
"""
@@ -186,10 +194,11 @@ def __eq__(self, other: object) -> bool:
186194
return self.is_close(other, tol=1e-9)
187195

188196
def __repr__(self) -> str:
197+
"""Developer representation of the Line."""
189198
return f"Line({self.a}, {self.b}, {self.c})"
190199

191200
def __str__(self) -> str:
192-
# Keep a readable form; avoid division by zero when possible.
201+
"""Keep a readable form; avoid division by zero when possible."""
193202
if _is_close(self.a, 0.0):
194203
# by + c = 0 => y = -c/b
195204
return f"y = {-self.c / self.b}"
@@ -220,8 +229,8 @@ def angle_btw_lines(l1: Line, l2: Line) -> float:
220229
raise ValueError("Cannot compute angle for degenerate line.")
221230

222231
# Clamp to [-1,1] to protect against tiny numeric drift
223-
cosang = max(-1.0, min(1.0, dot / (n1 * n2)))
224-
ang = math.acos(cosang)
232+
cosine_angle = max(-1.0, min(1.0, dot / (n1 * n2)))
233+
ang = math.acos(cosine_angle)
225234

226235
# Return acute angle
227236
if ang > math.pi / 2:

electricpy/geometry/circle.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
################################################################################
22
"""
3-
electricpy.geometry.circle - Collection of methods which operate on Cartesian
4-
circles.
3+
electricpy.geometry.circle - Collection of methods for Cartesian circles.
54
65
>>> import electricpy.geometry.circle as circle
76
@@ -13,7 +12,7 @@
1312
from __future__ import annotations
1413

1514
from dataclasses import dataclass
16-
from typing import Generator, Iterable, Optional, Tuple, Union, overload
15+
from typing import Generator, Tuple, Union
1716
import math
1817

1918
from electricpy import geometry
@@ -52,6 +51,7 @@ class Circle:
5251
radius : float
5352
The radius of the circle (must be >= 0)
5453
"""
54+
5555
center: Point
5656
radius: float
5757

@@ -139,9 +139,9 @@ def is_tangent(self, l: Line, *, tol: float = 1e-9) -> bool:
139139

140140
def is_normal(self, l: Line, *, tol: float = 1e-9) -> bool:
141141
"""
142-
Return True if the line passes through the circle's center (within tolerance).
142+
Line passes through the circle's center (within tolerance).
143143
144-
IMPORTANT
144+
Important
145145
---------
146146
A line being a "normal to the circle" is only well-defined at a specific
147147
point of contact. This method keeps backward compatibility with the
@@ -175,11 +175,11 @@ def equation(self) -> str:
175175
B = -2.0 * k
176176
C = const
177177

178-
def _term(coeff: float, var: str) -> str:
179-
if _is_close(coeff, 0.0):
178+
def _term(coef: float, var: str) -> str:
179+
if _is_close(coef, 0.0):
180180
return ""
181-
sign = " + " if coeff > 0 else " - "
182-
mag = abs(coeff)
181+
sign = " + " if coef > 0 else " - "
182+
mag = abs(coef)
183183
# Prefer integer-like display when possible
184184
if _is_close(mag, round(mag)):
185185
mag_str = str(int(round(mag)))
@@ -343,20 +343,25 @@ def intersetion(self, other) -> Union[None, Point, Tuple[Point, Point], str]:
343343
# Dunder methods
344344
# -------------------------------------------------------------------------
345345
def __repr__(self) -> str:
346+
"""Representation of the Circle."""
346347
return f"Circle(center={self.center}, radius={self.radius})"
347348

348349
def __str__(self) -> str:
350+
"""Form of the Circle."""
349351
return f"Circle(center={self.center}, radius={self.radius})"
350352

351353
def __eq__(self, other: object) -> bool:
354+
"""Equality comparison for Circle."""
352355
if isinstance(other, Circle):
353356
return self.center == other.center and self.radius == other.radius
354357
return False
355358

356359
def __ne__(self, other: object) -> bool:
360+
"""Inequality comparison for Circle."""
357361
return not self == other
358362

359363
def __hash__(self) -> int:
364+
"""Hash for Circle."""
360365
return hash((self.center, self.radius))
361366

362367

electricpy/geometry/triangle.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
################################################################################
22
"""
3-
electricpy.geometry.triangle - Collection of methods which operate on Cartesian
4-
triangles.
3+
electricpy.geometry.triangle - Collection of methods for Cartesian triangles.
54
65
>>> import electricpy.geometry.triangle as triangle
76
@@ -12,7 +11,7 @@
1211

1312
from __future__ import annotations
1413

15-
from typing import Iterable, Optional, Sequence, Tuple, Union
14+
from typing import Tuple, Union
1615
import math
1716

1817
from electricpy.geometry import Point, Line
@@ -42,9 +41,7 @@ def _is_close(a: float, b: float, *, rel_tol: float = 1e-9, abs_tol: float = 1e-
4241

4342

4443
def _triangle_twice_area(p0: Point, p1: Point, p2: Point) -> float:
45-
"""
46-
Return twice the signed area (cross product magnitude).
47-
"""
44+
"""Return twice the signed area (cross product magnitude)."""
4845
return (p1.x - p0.x) * (p2.y - p0.y) - (p1.y - p0.y) * (p2.x - p0.x)
4946

5047

@@ -115,9 +112,7 @@ def perimeter(self) -> float:
115112
return self.a + self.b + self.c
116113

117114
def perimeters(self) -> float:
118-
"""
119-
Backward-compatible alias for perimeter().
120-
"""
115+
"""Backward-compatible alias for perimeter()."""
121116
return self.perimeter()
122117

123118
def area(self) -> float:
@@ -151,9 +146,9 @@ def centroid(self) -> Point:
151146

152147
def in_center(self) -> Point:
153148
"""
154-
Return the incenter of the triangle.
149+
Return the in_center of the triangle.
155150
156-
The incenter is the weighted average of vertices by the lengths of
151+
The in_center is the weighted average of vertices by the lengths of
157152
the opposite sides. With our naming:
158153
a = |p0-p1| opposite vertex p2
159154
b = |p1-p2| opposite vertex p0
@@ -172,7 +167,7 @@ def in_center(self) -> Point:
172167

173168
def in_radius(self) -> float:
174169
"""
175-
Return the inradius of the triangle.
170+
Return the in_radius of the triangle.
176171
177172
r = A / s where s is semiperimeter.
178173
"""
@@ -184,12 +179,12 @@ def in_radius(self) -> float:
184179

185180
def ortho_center(self) -> Point:
186181
"""
187-
Return the orthocenter of the triangle.
182+
Return the ortho_center of the triangle.
188183
189184
Construct two altitudes:
190185
- altitude from p0 to line through p1-p2
191186
- altitude from p1 to line through p0-p2
192-
Their intersection is the orthocenter.
187+
Their intersection is the ortho_center.
193188
194189
Requires Line objects returned by geometry.line_equation to support:
195190
- foot_perpendicular(Point) -> Point
@@ -207,7 +202,7 @@ def ortho_center(self) -> Point:
207202

208203
def circum_center(self) -> Point:
209204
"""
210-
Return the circumcenter of the triangle.
205+
Return the circum_center of the triangle.
211206
212207
Intersection of perpendicular bisectors of two sides.
213208
"""
@@ -217,21 +212,23 @@ def circum_center(self) -> Point:
217212

218213
def circum_radius(self) -> float:
219214
"""
220-
Return the circumradius of the triangle.
215+
Return the circum_radius of the triangle.
221216
222217
R = abc / (4A)
223218
"""
224219
A = self.area()
225220
if _is_close(A, 0.0, abs_tol=self._tol):
226-
raise ValueError("Degenerate triangle: area is zero, circumradius undefined")
221+
raise ValueError("Degenerate triangle: area is zero, circum_radius undefined")
227222
return (self.a * self.b * self.c) / (4.0 * A)
228223

229224
# -------------------------------------------------------------------------
230225
# Validation
231226
# -------------------------------------------------------------------------
232227
def __is_valid(self) -> bool:
233228
"""
234-
Validate triangle:
229+
Validate triangle.
230+
231+
Checks:
235232
- triangle inequality with tolerance
236233
- non-collinear (non-degenerate) using cross-product area test
237234
"""

electricpy/passive.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def capbacktoback(C1, C2, Lm, VLN=None, VLL=None):
126126
Function to calculate the maximum current and the
127127
frequency of the inrush current of two capacitors
128128
connected in parallel when one (energized) capacitor
129-
is switched into another (non-engergized) capacitor.
129+
is switched into another (non-energized) capacitor.
130130
131131
.. note:: This formula is only valid for three-phase systems.
132132
@@ -443,7 +443,7 @@ def air_core_inductance(d: float, coil_l: float, n: int):
443443

444444
def air_core_required_length(d: float, L: float, n: int):
445445
r"""
446-
Compute Required Length of Air Core Inductor
446+
Compute Required Length of Air Core Inductor.
447447
448448
.. math:: l = \frac{1000 d^2 n^2 - 457418 d L}{1016127 L}
449449
@@ -467,7 +467,7 @@ def air_core_required_length(d: float, L: float, n: int):
467467

468468
def air_core_required_diameter(coil_l: float, L: float, n: int):
469469
r"""
470-
Compute Diameter of Air Core Inductor
470+
Compute Diameter of Air Core Inductor.
471471
472472
.. math:: 1000 n^2 d^2 - 457418 L d - 1016127 L l = 0
473473
@@ -496,7 +496,7 @@ def air_core_required_diameter(coil_l: float, L: float, n: int):
496496

497497
def air_core_required_num_turns(d: float, coil_l: float, L: float):
498498
r"""
499-
Compute Required Number of Turns of Air Core Inductor
499+
Compute Required Number of Turns of Air Core Inductor.
500500
501501
.. math:: n = \sqrt{\frac{L(1016127 l + 457418 d)}{1000 d^2}}
502502

test/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from electricpy.geometry import Point
22
from electricpy.geometry import Line
3-
import numpy as np
43
from numpy.testing import assert_almost_equal
54

65
def compare_points(p1: Point, p2: Point) -> bool:

0 commit comments

Comments
 (0)