Skip to content

Commit

Permalink
added function to print dict as ascii art table in the shell
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchiLaser committed Sep 6, 2023
1 parent 8c6cf3b commit f1882eb
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/timeforge/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from contextlib import contextmanager
from datetime import datetime, date, timedelta
import feiertage
import itertools
import os
from pypdf import PdfReader, PdfWriter
import requests
Expand All @@ -13,6 +14,35 @@
from . import config


def PrintDictAsTable(dataset: dict, title_keys: str, title_values: str):
"""
This function prints a dictionary as a table.
This is really useful for debugging purposes and will be called multiple times when the verbose flag is set.
Parameters
----------
dataset : dict
The dictionary which should be printed
title_keys : str
A title for the dictionary keys column
title_values : str
A title for the dictionary values column
"""
# get the max length of a string in the key and in the value section
max_key_len, max_value_len = len(title_keys), len(title_values)
for (i, j) in zip([*dataset.keys()], [*dataset.values()]):
max_key_len = max(len(str(i)), max_key_len)
max_value_len = max(len(str(j)), max_value_len)

print("┌─" + "─" * max_key_len + "─┬─" + "─" * max_value_len + "─┐")
print("│ " + title_keys + " " * (max_key_len - len(title_keys)) + " │ " + title_values + " " * (max_value_len - len(title_values)) + " │")
print("├─" + "─" * max_key_len + "─┼─" + "─" * max_value_len + "─┤")
for (i, j) in zip([*dataset.keys()], [*dataset.values()]):
print("│ " + str(i) + " " * (max_key_len - len(str(i))) + " │ " + str(j) + " " * (max_value_len - len(str(j))) + " │")
print("└─" + "─" * max_key_len + "─┴─" + "─" * max_value_len + "─┘")


class APP_Data:

def __init__(self):
Expand Down

0 comments on commit f1882eb

Please sign in to comment.