From e5023fc4f04815b42088391f4e907db6ef7c139c Mon Sep 17 00:00:00 2001 From: iopapamanoglou Date: Sun, 3 Nov 2024 16:57:05 +0100 Subject: [PATCH] Core: Range unfold inner const ranges on query --- src/faebryk/core/parameter.py | 7 +++++++ src/faebryk/library/Constant.py | 5 ++++- src/faebryk/library/Range.py | 12 ++++++++---- src/faebryk/library/Set.py | 3 +++ 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/faebryk/core/parameter.py b/src/faebryk/core/parameter.py index 607075de..8ad0319d 100644 --- a/src/faebryk/core/parameter.py +++ b/src/faebryk/core/parameter.py @@ -478,6 +478,13 @@ def get_max(self: "Parameter[PV]") -> PV: def _max(self): raise ValueError(f"Can't get max for {self}") + @_resolved_self + def get_min(self: "Parameter[PV]") -> PV: + return self._min() + + def _min(self): + raise ValueError(f"Can't get min for {self}") + def with_same_unit( self: "Quantity | float | int | LIT_OR_PARAM", to_convert: float | int, diff --git a/src/faebryk/library/Constant.py b/src/faebryk/library/Constant.py index f949a316..803c9e40 100644 --- a/src/faebryk/library/Constant.py +++ b/src/faebryk/library/Constant.py @@ -101,7 +101,10 @@ def try_compress(self) -> Parameter[PV]: return super().try_compress() def _max(self): - return self.value + return self + + def _min(self): + return self def _as_unit(self, unit: UnitsContainer, base: int, required: bool) -> str: return to_si_str(self.value, unit) diff --git a/src/faebryk/library/Range.py b/src/faebryk/library/Range.py index c217873b..0545d679 100644 --- a/src/faebryk/library/Range.py +++ b/src/faebryk/library/Range.py @@ -36,14 +36,14 @@ def _get_narrowed_bounds(self) -> list[Parameter[PV]]: @property def min(self) -> Parameter[PV]: try: - return min(self._get_narrowed_bounds()) + return min(p._min() for p in self._get_narrowed_bounds()) except (TypeError, ValueError): raise self.MinMaxError() @property def max(self) -> Parameter[PV]: try: - return max(self._get_narrowed_bounds()) + return max(p._max() for p in self._get_narrowed_bounds()) except (TypeError, ValueError): raise self.MinMaxError() @@ -126,16 +126,20 @@ def __copy__(self) -> Self: return type(self)(*self._bounds) def try_compress(self) -> Parameter[PV]: + bounds = self.bounds # compress into constant if possible if len(set(map(id, self.bounds))) == 1: - return Parameter.from_literal(self.bounds[0]) + return Parameter.from_literal(bounds[0]) return super().try_compress() def __contains__(self, other: LIT_OR_PARAM) -> bool: return self.min <= other and self.max >= other def _max(self): - return max(p.get_max() for p in self._get_narrowed_bounds()) + return self.max + + def _min(self): + return self.min def _as_unit(self, unit: UnitsContainer, base: int, required: bool) -> str: return ( diff --git a/src/faebryk/library/Set.py b/src/faebryk/library/Set.py index 112b5ece..6fd13427 100644 --- a/src/faebryk/library/Set.py +++ b/src/faebryk/library/Set.py @@ -86,6 +86,9 @@ def try_compress(self) -> Parameter[PV]: def _max(self): return max(p.get_max() for p in self.params) + def _min(self): + return min(p.get_min() for p in self.params) + def _as_unit(self, unit: UnitsContainer, base: int, required: bool) -> str: return ( "Set("