from dataclasses import dataclass, field from datetime import datetime as Date from rich import get_console from rich.table import Table @dataclass class War: name: str entry_date: Date @property def date_sum(self): day = self.entry_date.day month = self.entry_date.month century = self.entry_date.year // 100 decade = self.entry_date.year % 100 return day + month + century + decade @property def row(self): return self.name, self.entry_date.strftime("%Y-%m-%d"), str(self.date_sum) wars = [ War("World War 1", Date(1914, 7, 28)), War("World War 2", Date(1939, 9, 1)), War('"Official" Ukraine War', Date(2022, 2, 24)), ] console = get_console() table = Table() table.add_column("Name") table.add_column("Entry date") table.add_column("Date Sum") for war in wars: table.add_row(*war.row) console.print(table) # output """ ┏━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━┓ ┃ Name ┃ Entry date ┃ Date Sum ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━┩ │ World War 1 │ 1914-07-28 │ 68 │ │ World War 2 │ 1939-09-01 │ 68 │ │ "Official" Ukraine War │ 2022-02-24 │ 68 │ └────────────────────────┴────────────┴──────────┘ """