def __rmatmul__(cls, setting: Any): from concrete_settings import Setting if not isinstance(setting, Setting): setting = Setting(setting) bhv = cls() return bhv(setting)
def _make_setting_from_attribute( mcs, name, attr, annotations) -> Union[PropertySetting, Setting]: # is it a class method? if isinstance(attr, types.FunctionType): return PropertySetting(attr) type_hint = annotations.get(name, GuessSettingType) return Setting(attr, doc="", type_hint=type_hint)
def test_setting_ctor(v_int): validators = (lambda x: x, ) s = Setting(v_int, type_hint=int, validators=validators, doc="docstring") assert s.value == v_int assert s.__doc__ == "docstring" assert s.validators is validators assert s.type_hint is int
def S(name: str, type_hint=str) -> Setting: s = Setting(type_hint=type_hint) s.__set_name__(s, name) return s
class TestSettings(Settings): MAX_SPEED = Setting(23) @ plus(2) @ div(5)
class TestSettings(Settings): MAX_SPEED = Setting(10) @ div MIN_SPEED = Setting(9) @ div(3)
class TestSettings(Settings): MAX_SPEED = Setting(10) @ bhv_mock
def S(name: str, type_hint=str) -> Setting: """A helper function which creates a setting and assigns it a name.""" s = Setting(type_hint=type_hint) s.__set_name__(s, name) return s
class AppSettings(Settings): AGE: int = Setting(10)
def attach_to(self, setting: Setting): setting.attach_behavior(self)
def __rmatmul__(self, setting: Any): if not isinstance(setting, Setting): setting = Setting(setting) self.attach_to(setting) return setting
class TestSettings(Settings): mandatory_validators = (is_positive,) MIN_SPEED: int = Setting(0, validators=(is_less_that_10,)) MAX_SPEED: int = Setting(11, validators=(is_less_that_10,))
class TestSettigs(Settings): MAX_SPEED = Setting(10, validators=(validator,))
class TestSettings(Settings): MAX_SPEED = Setting(10, validators=(validator_with_exception, ))