Esempio n. 1
0
    def greater_than_or_equals(self, one: Any, another: Any) -> bool:
        if one is None:
            if another is None:
                return True
            elif is_numeric_instance(another) or is_date_or_time_instance(
                    another):
                return False
        elif another is None:
            if is_numeric_instance(one) or is_date_or_time_instance(one):
                return True
        elif isinstance(one, int) or isinstance(one, float) or isinstance(
                one, Decimal):
            return self.try_compare(
                lambda: greater_or_equals_decimal(one, another, True),
                lambda: self.raise_cannot_compare(one, another))
        elif isinstance(another, int) or isinstance(
                another, float) or isinstance(another, Decimal):
            return self.try_compare(
                lambda: less_or_equals_decimal(one, another, False),
                lambda: self.raise_cannot_compare(one, another))
        elif isinstance(one, time):
            # compare time
            return self.try_compare(
                lambda: greater_or_equals_time(one, another, ask_time_formats(
                ), True), lambda: self.raise_cannot_compare(one, another))
        elif isinstance(another, time):
            # compare time
            return self.try_compare(
                lambda: less_or_equals_time(another, one, ask_time_formats(
                ), False), lambda: self.raise_cannot_compare(one, another))
        elif isinstance(one, datetime) or isinstance(one, date):
            # compare datetime or date
            return self.try_compare(
                lambda: greater_or_equals_date(another, one,
                                               ask_all_date_formats(), True),
                lambda: self.raise_cannot_compare(one, another))
        elif isinstance(another, datetime) or isinstance(another, date):
            # compare datetime or date
            return self.try_compare(
                lambda: less_or_equals_date(another, one, ask_all_date_formats(
                ), False), lambda: self.raise_cannot_compare(one, another))

        self.raise_cannot_compare(one, another)
	def handle_comparison_possible_types(
			self, a_value: Any, another_possible_types: List[PossibleParameterType]) -> Any:
		if isinstance(a_value, str):
			if PossibleParameterType.NUMBER in another_possible_types:
				parsed, decimal_value = is_decimal(a_value)
				if parsed:
					return decimal_value
			if PossibleParameterType.DATE in another_possible_types or PossibleParameterType.DATETIME in another_possible_types:
				parsed, date_value = is_date(a_value, ask_all_date_formats())
				if parsed:
					return date_value
			if PossibleParameterType.TIME in another_possible_types:
				parsed, time_value = is_time(a_value, ask_time_formats())
				if parsed:
					return time_value
		return a_value
	def translate(self, value: Any) -> Optional[Union[date, time, Any]]:
		factor_type = self.factor.type
		if factor_type == FactorType.FULL_DATETIME:
			parsed_value = try_to_date(value, ask_all_date_formats())
		elif factor_type == FactorType.DATETIME:
			parsed_value = try_to_date(value, ask_all_date_formats())
		elif factor_type == FactorType.DATE or factor_type == FactorType.DATE_OF_BIRTH:
			parsed_value = try_to_date(value, ask_all_date_formats())
		elif factor_type == FactorType.TIME:
			parsed_value = try_to_time(value, ask_time_formats())
		else:
			parsed_value = value
		if parsed_value is not None:
			return parsed_value
		elif ask_abandon_date_time_on_parse_fail():
			return None
		else:
			return value
Esempio n. 4
0
def cast_value_for_factor(value: Any, factor: Factor) -> Any:
    if value is None:
        return None

    factor_type = factor.type
    if factor_type in [
            FactorType.SEQUENCE, FactorType.NUMBER, FactorType.UNSIGNED,
            FactorType.FLOOR, FactorType.RESIDENTIAL_AREA, FactorType.AGE,
            FactorType.BIZ_SCALE
    ]:
        parsed, decimal_value = is_decimal(value)
        if parsed:
            return decimal_value
        else:
            raise DataKernelException(
                f'Value[{value}] is incompatible with factor[name={factor.name}, type={factor_type}].'
            )
    elif factor_type == FactorType.TEXT:
        if isinstance(value, str):
            return value
        elif isinstance(value, (int, float, Decimal, bool, date, time)):
            return str(value)
        else:
            raise DataKernelException(
                f'Value[{value}, type={type(value)}] is incompatible with '
                f'factor[name={factor.name}, type={factor_type}].')
    elif factor_type in [
            FactorType.ADDRESS, FactorType.ROAD, FactorType.COMMUNITY,
            FactorType.EMAIL, FactorType.PHONE, FactorType.MOBILE,
            FactorType.FAX, FactorType.OCCUPATION, FactorType.ID_NO
    ]:
        if isinstance(value, str):
            return value
        elif isinstance(value, (int, float, Decimal)):
            return str(value)
        else:
            raise DataKernelException(
                f'Value[{value}, type={type(value)}] is incompatible with '
                f'factor[name={factor.name}, type={factor_type}].')
    # noinspection PyPep8
    elif factor_type in [
            FactorType.CONTINENT, FactorType.REGION, FactorType.COUNTRY,
            FactorType.PROVINCE, FactorType.CITY, FactorType.DISTRICT,
            FactorType.RESIDENCE_TYPE, FactorType.GENDER, FactorType.RELIGION,
            FactorType.NATIONALITY, FactorType.BIZ_TRADE, FactorType.ENUM
    ]:
        if isinstance(value, str):
            return value
        elif isinstance(value, (int, Decimal)):
            return str(value)
        else:
            raise DataKernelException(
                f'Value[{value}, type={type(value)}] is incompatible with '
                f'factor[name={factor.name}, type={factor_type}].')
    elif factor_type == FactorType.FULL_DATETIME:
        # noinspection DuplicatedCode
        if isinstance(value, datetime):
            return value
        if isinstance(value, date):
            return datetime(year=value.year, month=value.month, day=value.day)
        if isinstance(value, time):
            raise DataKernelException(
                f'Value[{value}, type={type(value)}] is incompatible with '
                f'factor[name={factor.name}, type={factor_type}].')

        parsed, date_value = is_date(str(value), ask_full_datetime_formats())
        if parsed:
            return date_value
        else:
            raise DataKernelException(
                f'Value[{value}, type={type(value)}] is incompatible with '
                f'factor[name={factor.name}, type={factor_type}].')
    elif factor_type == FactorType.DATETIME:
        # noinspection DuplicatedCode
        if isinstance(value, datetime):
            return value
        if isinstance(value, date):
            return datetime(year=value.year, month=value.month, day=value.day)
        if isinstance(value, time):
            raise DataKernelException(
                f'Value[{value}, type={type(value)}] is incompatible with '
                f'factor[name={factor.name}, type={factor_type}].')

        parsed, date_value = is_date(str(value), ask_all_date_formats())
        if parsed:
            return date_value
        else:
            raise DataKernelException(
                f'Value[{value}, type={type(value)}] is incompatible with '
                f'factor[name={factor.name}, type={factor_type}].')
    elif factor_type in [FactorType.DATE, FactorType.DATE_OF_BIRTH]:
        if isinstance(value, datetime):
            return value.date()
        if isinstance(value, date):
            return value
        if isinstance(value, time):
            raise DataKernelException(
                f'Value[{value}, type={type(value)}] is incompatible with '
                f'factor[name={factor.name}, type={factor_type}].')

        parsed, date_value = is_date(value, ask_all_date_formats())
        if parsed:
            if isinstance(date_value, datetime):
                return date_value.replace(hour=0,
                                          minute=0,
                                          second=0,
                                          microsecond=0,
                                          tzinfo=None)
            else:
                return date_value
        else:
            raise DataKernelException(
                f'Value[{value}, type={type(value)}] is incompatible with '
                f'factor[name={factor.name}, type={factor_type}].')
    elif factor_type == FactorType.TIME:
        if isinstance(value, datetime):
            return value.time()
        if isinstance(value, date):
            raise DataKernelException(
                f'Value[{value}, type={type(value)}] is incompatible with '
                f'factor[name={factor.name}, type={factor_type}].')
        if isinstance(value, time):
            return value

        parsed, time_value = is_time(value, ask_time_formats())
        if parsed:
            return time_value
        else:
            raise DataKernelException(
                f'Value[{value}, type={type(value)}] is incompatible with '
                f'factor[name={factor.name}, type={factor_type}].')
    elif factor_type in [
            FactorType.YEAR, FactorType.HALF_YEAR, FactorType.QUARTER,
            FactorType.MONTH, FactorType.HALF_MONTH, FactorType.TEN_DAYS,
            FactorType.WEEK_OF_YEAR, FactorType.WEEK_OF_MONTH,
            FactorType.HALF_WEEK, FactorType.DAY_OF_MONTH,
            FactorType.DAY_OF_WEEK, FactorType.DAY_KIND, FactorType.HOUR,
            FactorType.HOUR_KIND, FactorType.MINUTE, FactorType.SECOND,
            FactorType.MILLISECOND, FactorType.AM_PM
    ]:
        # TODO strictly validation is needed or not?
        parsed, decimal_value = is_decimal(value)
        if parsed:
            return decimal_value
        else:
            raise DataKernelException(
                f'Value[{value}] is incompatible with factor[name={factor.name}, type={factor_type}].'
            )
    elif factor_type == FactorType.BOOLEAN:
        if isinstance(value, bool):
            return value
        elif isinstance(value, (int, float, Decimal)):
            return value != 0
        elif isinstance(value, str):
            v = value.strip().lower()
            if v == 't' or v == 'y' or v == 'yes' or v == 'true':
                return True
            elif v == 'f' or v == 'n' or v == 'no' or v == 'false':
                return False
        raise DataKernelException(
            f'Value[{value}, type={type(value)}] is incompatible with '
            f'factor[name={factor.name}, type={factor_type}].')
    else:
        raise DataKernelException(
            f'Factor type[{factor_type}] is not supported.')
Esempio n. 5
0
 def not_equals(self, one: Any, another: Any) -> bool:
     return value_not_equals(one, another, ask_time_formats(),
                             ask_all_date_formats())