def __init__(self, family_members: list = None, income: Union[int, float, str] = 0, cars: Union[int, str] = 0): """ Constructor / Instantiate the class Parameters ---------- family_members : list list of Person (Male or Female) instances income : int, float, str gross yearly income cars : int, str number of cars in the family """ super().__init__() try: self._assert_family_members(family_members) Assertor.assert_data_types([income, cars], [(int, float, str), (int, str)]) Assertor.assert_non_negative([income, cars]) self._family_members = family_members self._inntekt = str(income) self._antall_biler = str(cars) LOGGER.success( "created '{}', with id: [{}]".format(self.__class__.__name__, self.id_str)) except Exception as family_exception: LOGGER.exception(family_exception) raise family_exception
def test_non_negative(negative_values): """ Test that assert_non_negative() method raises ValueError if value passed to method negative, i.e. less than zero. """ with pt.raises(ValueError): Assertor.assert_non_negative(negative_values)
def antall_biler(self, cars: (int, str)): """ cars setter Parameters ---------- cars : int, str new number of cars to set in family """ Assertor.assert_data_types([cars], [(int, str)]) Assertor.assert_non_negative(cars) self._antall_biler = str(cars)
def inntekt(self, income: (int, float, str)): """ income setter Parameters ---------- income : int, float, str new gross yearly income """ Assertor.assert_data_types([income], [(int, float, str)]) Assertor.assert_non_negative(income) self._inntekt = str(income)
def validating_cars(self, cars: Union[int, str]): """ method for validating that number of cars is non-negative Parameters ---------- cars : int, str number of cars to be validated """ Assertor.assert_data_types([cars], [(int, str)]) Assertor.assert_non_negative([cars], msg="Only non-negative 'numbers of cars'")
def validate_income(self, income: Union[int, float, str]): """ method for validating that income is non-negative Parameters ---------- income : int, float, str income to be validated """ Assertor.assert_data_types([income], [(int, float, str)]) Assertor.assert_non_negative([income], msg="Only non-negative 'income' accepted")
def sifo_age(self, age: Union[int, float, str]): """ Converts age into SIFO compatible str Parameters ---------- age : int, float, str age to be converted Returns ------- Out : str SIFO compatible age str """ try: age = float(age) except Exception as exp: raise TypeError("invalid numeric str, got '{}'".format(exp)) Assertor.assert_non_negative([age]) sifo_yrs = [0.41, 0.91, 1, 2, 3, 5, 9, 13, 17, 19, 30, 50, 60, 66, 74, 999] return str(sifo_yrs[bisect_left(sifo_yrs, age)]) if age <= 999 else str(999)