Merge pull request #1277 from snoyer/group_by_rounding
Some checks failed
benchmarks / benchmarks (macos-14, 3.12) (push) Has been cancelled
benchmarks / benchmarks (macos-15-intel, 3.12) (push) Has been cancelled
benchmarks / benchmarks (ubuntu-latest, 3.12) (push) Has been cancelled
benchmarks / benchmarks (windows-latest, 3.12) (push) Has been cancelled
Upload coverage reports to Codecov / run (push) Has been cancelled
pylint / lint (3.10) (push) Has been cancelled
Wheel building and publishing / Build wheel on ubuntu-latest (push) Has been cancelled
tests / tests (macos-14, 3.10) (push) Has been cancelled
tests / tests (macos-14, 3.14) (push) Has been cancelled
tests / tests (macos-15-intel, 3.10) (push) Has been cancelled
tests / tests (macos-15-intel, 3.14) (push) Has been cancelled
tests / tests (ubuntu-latest, 3.10) (push) Has been cancelled
tests / tests (ubuntu-latest, 3.14) (push) Has been cancelled
tests / tests (windows-latest, 3.10) (push) Has been cancelled
tests / tests (windows-latest, 3.14) (push) Has been cancelled
Run type checking / typecheck (3.10) (push) Has been cancelled
Run type checking / typecheck (3.14) (push) Has been cancelled
Wheel building and publishing / upload_pypi (push) Has been cancelled

future-proof rounding in `group_by`
This commit is contained in:
Roger Maitland 2026-04-17 10:52:42 -04:00 committed by GitHub
commit f5347f131f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 5 deletions

View file

@ -3043,17 +3043,22 @@ class ShapeList(list[T]):
return round(obj.volume, tol_digits)
elif callable(group_by):
raw_key_f = group_by
def key_f(obj):
val = raw_key_f(obj)
return round(val, tol_digits) if isinstance(val, (int, float)) else val
val = group_by(obj)
try:
return round(val, tol_digits)
except TypeError:
return val
elif isinstance(group_by, property):
def key_f(obj):
val = group_by.__get__(obj)
return round(val, tol_digits) if isinstance(val, (int, float)) else val
try:
return round(val, tol_digits)
except TypeError:
return val
else:
raise ValueError(f"Unsupported group_by function: {group_by}")

View file

@ -29,8 +29,9 @@ license:
import pprint
import unittest
from build123d import Cylinder
from build123d.geometry import Axis
from build123d.topology import Edge, Solid
from build123d.topology import Edge, Shape, Solid
from build123d.objects_curve import CenterArc
@ -83,6 +84,11 @@ class TestGroupBy(unittest.TestCase):
longest_lines = lines.group_by(edge_length)[-1]
self.assertEqual(len(longest_lines), 2)
def test_non_roundable_property(self):
cyl = Cylinder(1, 2)
grouped = cyl.faces().group_by(Shape.matrix_of_inertia)
self.assertEqual(len(grouped.groups), 2)
if __name__ == "__main__":
unittest.main()