Skip to content

Commit

Permalink
Improved Vector.__str__/__repr__
Browse files Browse the repository at this point in the history
  • Loading branch information
gumyr committed Jul 14, 2024
1 parent 6014507 commit 55c2231
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
20 changes: 14 additions & 6 deletions src/build123d/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,13 +435,18 @@ def __and__(self: Plane, other: Union[Axis, Location, Plane, VectorLike, "Shape"
"""intersect vector with other &"""
return self.intersect(other)

# def __repr__(self) -> str:
# """Display vector"""
# return "Vector: " + str((self.X, self.Y, self.Z))

def __repr__(self) -> str:
"""Display vector"""
return "Vector: " + str((self.X, self.Y, self.Z))
x = round(self.X, 13) if abs(self.X) > TOLERANCE else 0.0
y = round(self.Y, 13) if abs(self.Y) > TOLERANCE else 0.0
z = round(self.Z, 13) if abs(self.Z) > TOLERANCE else 0.0
return f"Vector({x:.14g}, {y:.14g}, {z:.14g})"

def __str__(self) -> str:
"""Display vector"""
return "Vector: " + str((self.X, self.Y, self.Z))
__str__ = __repr__

def __eq__(self, other: Vector) -> bool: # type: ignore[override]
"""Vectors equal operator =="""
Expand Down Expand Up @@ -476,7 +481,7 @@ def transform(self, affine_transform: Matrix, is_direction: bool = False) -> Vec
Defaults to False (vector)
Returns:
Vector: transformed vector
Vector: transformed vector
"""
if not is_direction:
# to gp_Pnt to obey build123d transformation convention (in OCP.vectors do not translate)
Expand Down Expand Up @@ -1923,9 +1928,12 @@ def bottom(cls) -> Plane:
def isometric(cls) -> Plane:
"""Isometric Plane"""
return Plane(
(0, 0, 0), (1 / 2**0.5, 1 / 2**0.5, 0), (1 / 3**0.5, -1 / 3**0.5, 1 / 3**0.5)
(0, 0, 0),
(1 / 2**0.5, 1 / 2**0.5, 0),
(1 / 3**0.5, -1 / 3**0.5, 1 / 3**0.5),
)


class Plane(metaclass=PlaneMeta):
"""Plane
Expand Down
9 changes: 6 additions & 3 deletions tests/test_direct_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3724,9 +3724,12 @@ def test_vector_not_implemented(self):
pass

def test_vector_special_methods(self):
v = Vector(1, 2, 3)
self.assertEqual(repr(v), "Vector: (1.0, 2.0, 3.0)")
self.assertEqual(str(v), "Vector: (1.0, 2.0, 3.0)")
self.assertEqual(repr(Vector(1, 2, 3)), "Vector(1, 2, 3)")
self.assertEqual(str(Vector(1, 2, 3)), "Vector(1, 2, 3)")
self.assertEqual(
str(Vector(9.99999999999999, -23.649999999999995, -7.37188088351e-15)),
"Vector(10, -23.65, 0)",
)

def test_vector_iter(self):
self.assertEqual(sum([v for v in Vector(1, 2, 3)]), 6)
Expand Down

0 comments on commit 55c2231

Please sign in to comment.