Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Core: Range unfold inner const ranges on query
Browse files Browse the repository at this point in the history
  • Loading branch information
iopapamanoglou committed Nov 3, 2024
1 parent df1e405 commit e5023fc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/faebryk/core/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion src/faebryk/library/Constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions src/faebryk/library/Range.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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 (
Expand Down
3 changes: 3 additions & 0 deletions src/faebryk/library/Set.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("
Expand Down

0 comments on commit e5023fc

Please sign in to comment.