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,
) -> 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.1.tar.gz (21.5 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.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for strarr-0.1.0.1.tar.gz
Algorithm Hash digest
SHA256 54c6f92035a3d2c762d4945a1a00cc6b8d88cce3b7ab370cbfe0ccf0ec47f5c2
MD5 1d6d51836d7e8890f201d0015dd7a1b9
BLAKE2b-256 4e4e97e765a3bd5480766abcd6ab59212ad1016ab6aca8f1f5c3a2216ca4d089

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for strarr-0.1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dcd1b2bfc11b5474495e8ab5d41557ba6cbe964290c35d621b5c0125d85ed461
MD5 d047e4f7f5e800a792311dc87dc9c5ed
BLAKE2b-256 81c4bedfda10371f79045776cc5b0648aacdf3cac9da2408ac377295bbb79038

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