Skip to main content

Render iterable of objects or dictionaries into a formatted table string.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

strarr-0.1.1.tar.gz (21.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

strarr-0.1.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file strarr-0.1.1.tar.gz.

File metadata

  • Download URL: strarr-0.1.1.tar.gz
  • Upload date:
  • Size: 21.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.4

File hashes

Hashes for strarr-0.1.1.tar.gz
Algorithm Hash digest
SHA256 415e7823aa6d8cbcfb47d193925c1e2bc9878f6ef9190f19a3c9fb0676ed0058
MD5 3f7b6949d9a87fceade73dbdc761127b
BLAKE2b-256 d1638a7fc722e547054593ad512a66571df31b1d9dab383674647395060ca663

See more details on using hashes here.

File details

Details for the file strarr-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: strarr-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.4

File hashes

Hashes for strarr-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f59abcf212b8a18665c8412acfbd1f12438affdc6de60bfd415aa6ff441c615e
MD5 01e7a4d7c6304f85d1d91e9c7abb73d4
BLAKE2b-256 06a2168b4656b939cbca2e75ee866a620112e23d904f2a6109f9143528f486b0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page