Skip to main content

Render objects or dictionaries in 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,
) -> 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).
    :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.0.tar.gz (21.4 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.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for strarr-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cf66df49b759cc2a606d9abcf0dbcd4c29fc8ace5c48804a106b10a44f31a0c4
MD5 4abdac8428353df4a4645d42cd7839f6
BLAKE2b-256 b4e779c29c3c81d0f5db1522d0eb16294a1cea68d4c912f7210d2fd9415efb2c

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for strarr-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3b72445ceb7e4f77031e2c424db6abd06dc4784bf6a7c9a663e32730f61f9897
MD5 0a9a1623164038370c9b446d072be532
BLAKE2b-256 53d74e20ebfe9e036cf7dc530f02362516d3a5cefc1da16948f38c3b049abfa0

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