Skip to main content

strarr

strarr is a Python library designed to convert any iterable of objects or dictionaries into a formatted table string with minimum effort.

It is intended to be used to quickly print data in a tabular format, for better understanding or debugging purpose.

Features

  • Iterables support: Works with lists, tuples, and any other iterables.
  • Object and Dictionary Support: Works with both objects and dictionaries.
    • Use attributes for objects and keys for dictionaries.
    • Automatically infer type of data (either dict or object) based on the first element in the iterable.
    • Assumes same type for all elements in the iterable.
  • Automatic Field Detection: Automatically detects fields from objects or dictionaries.
  • Customizable Field Inclusion/Exclusion: Easily include or exclude specific fields.
  • Index Column: Optionally add an index column to the table.
  • Customizable output:
    • Add optional indentation at the start of rows.
    • Control spaces between columns.

Installation

Install the library using pip:

pip install strarr

Usage

Basic example:

from strarr import strarr


class Example:
    def __init__(self, an_id, name):
        self.id = an_id
        self.name = name


data = [Example(1, "Alice"), Example(2, "Bob")]

print(strarr(data))
"""Output:
#  [id]  [name]
1     1   Alice
2     2     Bob
"""

For more examples, see tests directory in the GitHub repository.

Documentation

from collections.abc import Iterable, Collection
from typing import Any

def strarr(
    iterable: Iterable[Any],
    /,
    include: Collection[str] = (),
    *,
    exclude: Collection[str] = (),
    index: int | None = 1,
    indent: str = "",
    space: int = 2,
    align_left: bool = False,
) -> str:
    """
    Convert an iterable of objects or dictionaries into a formatted table
    rendered in a string.

    :param iterable: Iterable of objects or dictionaries.
    :param include: List of fields to include in the table.
        If not specified or empty, all fields will be included.
        Mutually exclusive with `exclude`.
    :param exclude: List of fields to exclude from the table.
        Mutually exclusive with `include`.
    :param index: If not None, adds an index column starting from this value.
    :param indent: String to prepend to each line of the table (default "").
    :param space: Number of spaces to add between columns (default 2).
    :param align_left: If True, aligns columns to the left,
        otherwise to the right (default False).
    :return: Formatted string table.
    :raises IncludeExcludeError: If both include and exclude are specified.
    :raises NoFieldError:
        If `include` is not specified and no fields
        can be inferred from the first element.
    :raises NoFieldAfterExcludingError:
        If `exclude` is specified and no fields are left after excluding.
    """

License

This project is licensed under the terms of the MIT License.

Metadata

Release files for strarr 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for strarr 0.1.1
File Size Uploaded
strarr-0.1.1.tar.gz 21.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for strarr 0.1.1
File Interpreter ABI Platform
strarr-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 26.5 kB

Release files / strarr-0.1.1.tar.gz

Download URL strarr-0.1.1.tar.gz
Size 21.7 kB
Tags Source
SHA-256 checksum
How to use checksums
415e7823aa6d8cbcfb47d193925c1e2bc9878f6ef9190f19a3c9fb0676ed0058
BLAKE2b-256 checksum
How to use checksums
d1638a7fc722e547054593ad512a66571df31b1d9dab383674647395060ca663
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.4

Release files / strarr-0.1.1-py3-none-any.whl

Download URL strarr-0.1.1-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f59abcf212b8a18665c8412acfbd1f12438affdc6de60bfd415aa6ff441c615e
BLAKE2b-256 checksum
How to use checksums
06a2168b4656b939cbca2e75ee866a620112e23d904f2a6109f9143528f486b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.4

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page