Metadata-Version: 2.1
Name: kuromon
Version: 0.1.0
Summary: Convert a list of dict, dataclass or Pydantic objects into a string represented table
Home-page: https://github.com/ninoseki/kuromon
Author: Manabu Niseki
Author-email: manabu.niseki@gmail.com
Requires-Python: >=3.7,<4.0
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Requires-Dist: importlib-metadata (>=4.12.0,<5.0.0); python_version < "3.8"
Requires-Dist: pydantic (>=1.6.2,!=1.7,!=1.7.1,!=1.7.2,!=1.7.3,!=1.8,!=1.8.1,<2.0.0)
Requires-Dist: tabulate (>=0.8.10,<0.9.0)
Project-URL: Repository, https://github.com/ninoseki/kuromon
Description-Content-Type: text/markdown

# kuromon

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

## Requirements

- Python 3.7+

## Installation

```bash
pip install kuromon
```

## Usage

```py
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.

```py
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))
```
