From e449199a25fed6594248b549e4ba7234afa103e1 Mon Sep 17 00:00:00 2001 From: Sven Augustin Date: Thu, 28 Apr 2022 11:53:36 +0200 Subject: [PATCH] added conv(adj) to map to conv(adj.get_current_value()) for conv in (int, float, round, trunc, floor, ceil) --- slic/core/adjustable/adjustable.py | 4 ++-- slic/core/adjustable/convenience.py | 31 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/slic/core/adjustable/adjustable.py b/slic/core/adjustable/adjustable.py index 702c5606..aec0dce1 100644 --- a/slic/core/adjustable/adjustable.py +++ b/slic/core/adjustable/adjustable.py @@ -2,11 +2,11 @@ from slic.utils import typename from slic.core.task import TaskProducer from .baseadjustable import BaseAdjustable from .error import AdjustableError -from .convenience import SpecConvenience +from .convenience import SpecConvenience, NumericConvenience from .limited import Limited -class Adjustable(BaseAdjustable, TaskProducer, SpecConvenience, Limited): +class Adjustable(BaseAdjustable, TaskProducer, SpecConvenience, NumericConvenience, Limited): stop = None #TODO: might be better to make this callable diff --git a/slic/core/adjustable/convenience.py b/slic/core/adjustable/convenience.py index 8cb519c9..5629a58e 100644 --- a/slic/core/adjustable/convenience.py +++ b/slic/core/adjustable/convenience.py @@ -1,3 +1,34 @@ +from math import trunc, floor, ceil + + +class NumericConvenience: + + def __int__(self): + v = self.get_current_value() + return int(v) + + def __float__(self): + v = self.get_current_value() + return float(v) + + + def __round__(self, *args, **kwargs): + v = self.get_current_value() + return round(v, *args, **kwargs) + + def __trunc__(self): + v = self.get_current_value() + return trunc(v) + + def __floor__(self): + v = self.get_current_value() + return floor(v) + + def __ceil__(self): + v = self.get_current_value() + return ceil(v) + + class SpecConvenience: