Ejemplo n.º 1
0
    def __split_unit(self, readable_value):
        if RealNumber(readable_value).is_type():
            if self._default_unit is None:
                raise UnitNotFoundError(
                    "unit not found",
                    value=readable_value,
                    available_units=_get_unit_msg(self._text_units),
                )

            return (readable_value, self._default_unit)

        if not String(readable_value).is_type():
            raise TypeError("readable_value must be a string")

        for unit in self._units:
            try:
                if unit.regexp.search(readable_value):
                    number = unit.regexp.split(readable_value)[0]
                    if not RealNumber(number).is_type():
                        continue

                    return (number, unit)
            except TypeError:
                continue

        raise UnitNotFoundError(
            "unit not found",
            value=readable_value,
            available_units=_get_unit_msg(self._text_units),
        )
Ejemplo n.º 2
0
    def _to_header_item(self, col_dp, value_dp):
        from typepy import String

        format_string = self._get_header_format_string(col_dp, value_dp)
        header = String(value_dp.data).force_convert().strip()

        return format_string.format(self.__remove_line_break(header))
Ejemplo n.º 3
0
    def _to_header_item(self, col_dp: ColumnDataProperty, value_dp: DataProperty) -> str:
        format_string = self._get_header_format_string(col_dp, value_dp)
        header = String(value_dp.data).force_convert().strip()
        default_style = self._get_col_style(col_dp.column_index)
        style = self._fetch_style_from_filter(-1, col_dp, value_dp, default_style)

        return self._styler.apply_terminal_style(format_string.format(header), style=style)
Ejemplo n.º 4
0
    def __exeute_method(self, method, value):
        try:
            result = getattr(self.typeclass(value, self.strict_level),
                             method)()
            if method == "validate":
                result = "-"
        except (TypeError, typepy.TypeConversionError):
            return '"E"'

        # for string tests
        if NullString(result, StrictLevel.MAX).is_type():
            return '""'

        strict_level = StrictLevel.MAX

        typeobj = Integer(result, strict_level)
        if typeobj.is_type():
            return typeobj.convert()

        typeobj = RealNumber(result, strict_level)
        if typeobj.is_type():
            return typeobj.convert()

        if String(result, strict_level).is_type():
            return '"{}"'.format(result)

        if Infinity(result, strict_level).is_type():
            return '"inf"'

        if Nan(result, strict_level).is_type():
            return '"nan"'

        return result
Ejemplo n.º 5
0
def is_multibyte_str(text):
    from typepy import StrictLevel, String

    if not String(text, strict_level=StrictLevel.MIN).is_type():
        return False

    try:
        unicode_text = MultiByteStrDecoder(text).unicode_str
    except ValueError:
        return False

    try:
        unicode_text.encode("ascii")
    except UnicodeEncodeError:
        return True

    return False
Ejemplo n.º 6
0
    def create(
        self, table_data: TableData, index_list: Sequence[str], source_info: "SourceInfo"
    ) -> None:
        con_mem = simplesqlite.connect_memdb()

        con_mem.create_table_from_tabledata(
            table_data,
            primary_key=self.__add_pri_key_name,
            add_primary_key_column=String(self.__add_pri_key_name).is_type(),
        )

        src_table_name = con_mem.fetch_table_names()[0]
        dst_table_name = src_table_name

        if self.__require_rename_table(con_mem, src_table_name):
            dst_table_name = self.__make_unique_table_name(src_table_name)

            self.__logger.debug(
                "rename table from '{}' to '{}'".format(src_table_name, dst_table_name)
            )

            is_create_table = True
            simplesqlite.copy_table(
                src_con=con_mem,
                dst_con=self.__dst_con,
                src_table_name=src_table_name,
                dst_table_name=dst_table_name,
            )
        else:
            is_create_table = not self.__dst_con.has_table(dst_table_name)
            simplesqlite.append_table(
                src_con=con_mem, dst_con=self.__dst_con, table_name=dst_table_name
            )

        self.__dst_con.create_index_list(dst_table_name, index_list)

        self.__result_logger.logging_success(
            source_info.get_name(self.__verbosity_level), dst_table_name, is_create_table
        )
Ejemplo n.º 7
0
    def destination(self, value: str) -> None:
        if not String(value, strict_level=StrictLevel.MAX).is_type():
            raise ValueError("empty destination")

        self.__destination = value
Ejemplo n.º 8
0
    def _to_header_item(self, col_dp, value_dp):
        format_string = self._get_header_format_string(col_dp, value_dp)
        header = String(value_dp.data).force_convert().strip()

        return format_string.format(header)
Ejemplo n.º 9
0
    def test_normal(self, value, strict_level, expected):
        type_checker = String(value, strict_level=strict_level)

        assert type_checker.is_type() == expected
        assert type_checker.typecode == Typecode.STRING