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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
def format_block_rich(block: SeriesBlock) -> Table:
    """format a SeriesBlock into a rich Table"""
    title = f"time: {block.time_ns:_} ns, duration: {block.duration_ns:_} ns"
    table = Table(title=title, title_style="bold", title_justify="left", box=None)
    for field in ["channel", "data_type", "sample_rate", "samples", "has_gap", "data"]:
        table.add_column(field, justify=FIELDS.get(field, "left"))
    for name, series in sorted(block.items()):
        dstr = numpy.array2string(
            series.data,
            max_line_width=10000,
            separator=", ",
            threshold=3,
            edgeitems=1,
            formatter={
                "all": str,
            },
        )
        cells = [
            name,
            str(series.data_type),
            str(series.sample_rate),
            str(len(series.data)),
            str(series.has_gaps),
            # NOTE rich uses brackets to indicate markup, so escape
            # brackets with a leading slash
            rf"\{dstr}",
        ]
        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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
def format_channels_rich(channels: list[Channel]) -> Table:
    """format a list of channels into a rich Table"""

    def _init_table(headers):
        table = Table(box=None)
        for header in headers:
            table.add_column(
                header,
                justify=FIELDS.get(header, "left"),
            )
        return table

    table = None
    for channel in channels:
        if not table:
            table = _init_table(channel.as_dict().keys())
        table.add_row(*[str(v) for v in channel.as_dict().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
183
184
185
def print_rich(obj):
    """print a rich object to the screen"""
    Console().print(obj)