Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# --------------------------------------------------------------------------
 
 
class Row:
    """A row in a PTable"""
 
    def __init__(self, columns: list[str]) -> None:
        self._columns: list[str] = columns  # List of strings
        self._annotation_table = None  # Optional PTable used for displaying detailed information about the feature row.
 
    def append_annotation_table(self, ptable) -> None:
        self._annotation_table = ptable
 
 
class PTable:
    """A table that can be printed to the console."""
 
    def __init__(self, sortable=False) -> None:
        self._rows: list[Row] = []
        self._column_count = None
        self._sortable = sortable  # allow the rows to be sorted by the first column
 
    def add_row(self, columns: list[str]) -> Row:
        """Add a row to the table. The number of columns must match the number of columns in the table."""
        if self._column_count is None:
            self._column_count = len(columns)
        assert self._column_count == len(columns)
        row = Row(columns)
        self._rows.append(row)
        return row
 
    def get_string(self, first_column_width=None, second_column_width=None) -> str:
        """Serialize the table to a string."""
        if len(self._rows) == 0:
            return ""
 
        # Collect the max width of each column
        column_widths = []
        for row in self._rows:
            if column_widths:
                assert len(column_widths) == len(row._columns)
            else:
                column_widths = [0] * len(row._columns)
            for i, column in enumerate(row._columns):
                column_widths[i] = max(column_widths[i], len(str(column)))
 
        if first_column_width:
            column_widths[0] = max(first_column_width, column_widths[0])
 
        if second_column_width:
            column_widths[2] = max(second_column_width, column_widths[2])
 
        serialized_table = ""
        if self._sortable:
            sorted_rows = sorted(self._rows, key=lambda row: row._columns[0])
        else:
            sorted_rows = self._rows
 
        for row in sorted_rows:
            for i, column in enumerate(row._columns):
                serialized_table += f"{str(column).ljust(column_widths[i] + 2)}"
            serialized_table += "\n"
            if row._annotation_table:
                serialized_table += row._annotation_table.get_string(
                    first_column_width=column_widths[0], second_column_width=column_widths[2]
                )
 
        return serialized_table