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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file kuromon-0.2.0.tar.gz.

File metadata

  • Download URL: kuromon-0.2.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.7.6 Darwin/21.5.0

File hashes

Hashes for kuromon-0.2.0.tar.gz
Algorithm Hash digest
SHA256 502f63042acb51e004f06abc07c46b6bd5ee2c113940a4d77eb95b7d467c23a2
MD5 c314c101ab77c480796f4e65ee4ccef6
BLAKE2b-256 3b68a40e9dbeee3ab0b28ec5e24c9b2179224d7aabdf21636f33cd619a33b8d7

See more details on using hashes here.

File details

Details for the file kuromon-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: kuromon-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.7.6 Darwin/21.5.0

File hashes

Hashes for kuromon-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4956c7dc85d665021f531dc70b35f90ebed61f0b8f123308943c9f1447e2650b
MD5 15dc6c37e3ebf3561c1427a4767abbcf
BLAKE2b-256 f2eab8f495a3e238b4569f6fd75bcb58afb02650954147757930b3aff8aabd24

See more details on using hashes here.

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