Skip to main content

Convert a list of dict, dataclass, Pydantic or POPO objects into a string represented table

Project description

kuromon

PyPI version Python CI

Convert a list of dict, dataclass, Pydantic or POPO objects into a string represented table.

Requirements

  • Python 3.7+

Installation

pip install kuromon
# or if you want to use Pydantic along with kuromon
pip install kuromon[pydantic]

Usage

from kuromon import to_table

dict_data = [
    {"id": 1, "name": "foo", "tags": None},
    {"id": 2, "name": "bar", "tags": ["a"]},
]
print(to_table(dict_data))
# |    |   id | name   | tags   |
# |----|------|--------|--------|
# |  0 |    1 | foo    |        |
# |  1 |    2 | bar    | ['a']  |

# Disable indexing by setting index=False
print(to_table(dict_data, index=False))
# |   id | name   | tags   |
# |------|--------|--------|
# |    1 | foo    |        |
# |    2 | bar    | ['a']  |

# Change the table format via tablefmt
# NOTE: You can use the following tabulate formats
#       https://github.com/astanin/python-tabulate#table-format
print(to_table(dict_data, tablefmt="plain"))
# 0     1  foo
# 1     2  bar     ['a']

The above example uses a list of dict objects. You can also use a list of dataclass or Pydantic objects.

from dataclasses import dataclass
from typing import List, Optional

from dacite import from_dict
from pydantic import BaseModel

from kuromon import to_table


class TestModel(BaseModel):
    id: int
    name: str
    tags: Optional[List[str]]


@dataclass
class TestDataClass:
    id: int
    name: str
    tags: Optional[List[str]]


dict_data = [
    {"id": 1, "name": "foo", "tags": None},
    {"id": 2, "name": "bar", "tags": ["a"]},
]
pydantic_data = [TestModel.parse_obj(obj) for obj in dict_data]
dataclass_data = [from_dict(data_class=TestDataClass, data=obj) for obj in dict_data]

print(to_table(pydantic_data))
print(to_table(dataclass_data))

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

kuromon-0.2.0.tar.gz (3.8 kB view hashes)

Uploaded Source

Built Distribution

kuromon-0.2.0-py3-none-any.whl (3.9 kB view hashes)

Uploaded Python 3

Supported by

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