How to use the beautifultable.compat.to_unicode function in beautifultable

To help you get started, we’ve selected a few beautifultable examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pri22296 / beautifultable / beautifultable / helpers.py View on Github external
for i in range(len(row)):
                row[i] = pre_process(
                    row[i], table.detect_numerics, table.precision, sign.value,
                )
            for row_ in self._clamp_row(row):
                for i in range(len(table.columns)):
                    # str.format method doesn't work for multibyte strings
                    # hence, we need to manually align the texts instead
                    # of using the align property of the str.format method
                    pad_len = width[i] - termwidth(row_[i])
                    if align[i].value == "<":
                        right_pad = " " * pad_len
                        row_[i] = to_unicode(row_[i]) + right_pad
                    elif align[i].value == ">":
                        left_pad = " " * pad_len
                        row_[i] = left_pad + to_unicode(row_[i])
                    else:
                        left_pad = " " * (pad_len // 2)
                        right_pad = " " * (pad_len - pad_len // 2)
                        row_[i] = left_pad + to_unicode(row_[i]) + right_pad
                content = []
                for j, item in enumerate(row_):
                    if j > 0:
                        content.append(
                            table.columns.separator
                            if (mask[j - 1] or mask[j])
                            else " " * termwidth(table.columns.separator)
                        )
                    content.append(item)
                content = "".join(content)
                content = (
                    table.border.left
github pri22296 / beautifultable / beautifultable / rows.py View on Github external
row[i] = get_output_str(
                    row[i],
                    table.detect_numerics,
                    table.numeric_precision,
                    sign.value,
                )
            list_of_rows = self._get_row_within_width(row)
            for row_ in list_of_rows:
                for i in range(table.column_count):
                    # str.format method doesn't work for multibyte strings
                    # hence, we need to manually align the texts instead
                    # of using the align property of the str.format method
                    pad_len = width[i] - termwidth(row_[i])
                    if align[i].value == "<":
                        right_pad = " " * pad_len
                        row_[i] = to_unicode(row_[i]) + right_pad
                    elif align[i].value == ">":
                        left_pad = " " * pad_len
                        row_[i] = left_pad + to_unicode(row_[i])
                    else:
                        left_pad = " " * (pad_len // 2)
                        right_pad = " " * (pad_len - pad_len // 2)
                        row_[i] = left_pad + to_unicode(row_[i]) + right_pad
                content = table.column_separator_char.join(row_)
                content = table.left_border_char + content
                content += table.right_border_char
                string.append(content)
        return "\n".join(string)
github pri22296 / beautifultable / beautifultable / utils.py View on Github external
def textwrap(item, width):
    obj = ANSIMultiByteString(to_unicode(item))
    return obj.wrap(width)
github pri22296 / beautifultable / beautifultable / utils.py View on Github external
If the conversion is not possible, it simply returns the string.
    """
    num_types = (int, float)
    # We don't wan't to perform any conversions if item is already a number
    if isinstance(item, num_types):
        return item

    # First try for an int conversion so that strings like "5" are converted
    # to 5 instead of 5.0 . This is safe as a direct int cast for a non integer
    # string raises a ValueError.
    try:
        num = int(to_unicode(item))
    except ValueError:
        try:
            num = float(to_unicode(item))
        except ValueError:
            return item
        else:
            return num
    except TypeError:
        return item
    else:
        return num
github pri22296 / beautifultable / beautifultable / beautifultable.py View on Github external
# Print column headers if not empty or only spaces
            row_iterator = iter(self.rows)
            if column_header_visible:
                yield next(row_iterator)._get_string(
                    align=self.columns.header.alignment
                )
                if self.columns.header.separator:
                    yield self._get_header_separator()

            # Printing rows
            first_row_encountered = False
            for i, row in enumerate(row_iterator):
                if first_row_encountered and self.rows.separator:
                    yield self._get_row_separator()
                first_row_encountered = True
                content = to_unicode(row)
                yield content

            if rows is not None:
                # Printing additional rows
                prev_length = len(self.rows)
                for i, row in enumerate(rows, start=1):
                    if first_row_encountered and self.rows.separator:
                        yield self._get_row_separator()
                    first_row_encountered = True
                    if self._serialno:
                        row.insert(0, prev_length + i)
                    if row_header_visible:
                        self.rows.append([None] + list(row))
                    else:
                        self.rows.append(row)
                    content = to_unicode(self.rows[-1])
github pri22296 / beautifultable / beautifultable / utils.py View on Github external
def termwidth(item):
    """Returns the visible width of the string as shown on the terminal"""
    obj = ANSIMultiByteString(to_unicode(item))
    return obj.termwidth()
github pri22296 / beautifultable / beautifultable / utils.py View on Github external
def to_numeric(item):
    """
    Helper method to convert a string to float or int if possible.

    If the conversion is not possible, it simply returns the string.
    """
    num_types = (int, float)
    # We don't wan't to perform any conversions if item is already a number
    if isinstance(item, num_types):
        return item

    # First try for an int conversion so that strings like "5" are converted
    # to 5 instead of 5.0 . This is safe as a direct int cast for a non integer
    # string raises a ValueError.
    try:
        num = int(to_unicode(item))
    except ValueError:
        try:
            num = float(to_unicode(item))
        except ValueError:
            return item
        else:
            return num
    except TypeError:
        return item
    else:
        return num