Skip to content

format

Formatter dataclass

Formatter(name, desc, func)

class for storing an object formatter function

__call__

__call__(*args, **kwargs)

format object

Source code in arrakis/format.py
50
51
52
def __call__(self, *args, **kwargs):
    """format object"""
    return self.func(*args, **kwargs)

format_block_rich

format_block_rich(block)

format a SeriesBlock into a rich Table

Source code in arrakis/format.py
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
def format_block_rich(block: SeriesBlock) -> Table:
    """format a SeriesBlock into a rich Table"""
    fields = [
        "channel",
        "dtype",
        "sample_rate",
        "samples",
        "has gap",
        "data (abrv)",
    ]
    header = f"time: {block.time_ns:_} ns, duration: {block.duration_ns:_} ns"
    table = Table(
        *fields,
        title=header,
        title_justify="left",
        title_style="bold",
        box=None,
    )
    for name, series in sorted(block.items()):
        cells = [
            name,
            str(series.data_type),
            str(series.sample_rate),
            str(len(series.data)),
            str(series.has_nulls),
            numpy.array2string(
                series.data,
                max_line_width=10000,
                separator=", ",
                threshold=5,
                edgeitems=1,
                formatter={
                    "all": str,
                },
            ),
        ]
        table.add_row(*cells)
    return table

format_channels_rich

format_channels_rich(channels)

format a list of channels into a rich Table

Source code in arrakis/format.py
112
113
114
115
116
117
118
119
120
121
122
123
124
def format_channels_rich(channels: list[Channel]) -> Table:
    """format a list of channels into a rich Table"""
    table = None
    for channel in channels:
        cdict = {k: str(v) for k, v in channel.as_dict().items()}
        if not table:
            table = Table(
                *cdict,
                box=None,
            )
        table.add_row(*cdict.values())
    assert table
    return table

formatter

formatter(obj, format='str')

format an object for printing to the screen

See FORMATTERS for available formats. If format is not specified the str representation of the object is returned.

Source code in arrakis/format.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def formatter(obj: Any, format: str = "str") -> str:  # noqa: A002
    """format an object for printing to the screen

    See FORMATTERS for available formats.  If format is not specified
    the str representation of the object is returned.

    """
    for formatter in FORMATTERS:
        if formatter.name == format:
            return formatter(obj)
    else:
        msg = f"Unknown format: {format}"
        raise ValueError(msg)

print_rich

print_rich(obj)

print a rich object to the screen

Source code in arrakis/format.py
167
168
169
def print_rich(obj):
    """print a rich object to the screen"""
    Console().print(obj)