I have a class Table with a tuple called header
self.header = ("Column_1", "Column_2", "Column_3", "Column_4", "Column_5")
and a dictionary of tuples called __entries that are added with the function:
"""@brief Creates a new entry"""
def addEntry(self, column_1 : str , column_2 : str, column_3 : bool, column_4 : List[str], column_5 : Callable[[],None]) -> None:
self.__entries[column_1] = (column_1, column_2, column_3, column_4, column_5)
I want to print the header and all entries as a formatted table. My current attempt is:
"""@brief Print the header and all entries as a table"""
def print(self) -> None:
print("|\t" + self.header[0] + "\t|\t" + self.header[1] + "\t|\t" + self.header[2] + "\t|\t" + self.header[3]+ "\t|" + self.header[4]+ "\t|")
for key in self.__entries:
entry = self.__entries[key]
print("|\t" + entry[0] + "\t|\t" + entry[1] + "\t|\t" + str(entry[2]) + "\t|\t" + str(entry[3]) + "\t|\t" + str(entry[4])+ "\t|")
But obviously this doesn't resize the columns, and so the program:
def Foo() -> None:
print(HelloWorld)
def Bar() -> None:
print(HelloWorld)
def Baz() -> None:
print(HelloWorld)
if __name__== "__main__":
table = Table()
table.addEntry("test","Craig",False,[], Foo)
table.addEntry("test2","Bob",False,[], Bar)
table.addEntry("test3","Bill",False,[], Baz)
methodRegistry.print()
Outputs:
| Column_1 | Column_2 | Column_3 | Column_4 | Column_5 |
| test | Craig | False | [] | <function Foo at 0x7f1192b56e18> |
| test2 | Bob | False | [] | <function Bar at 0x7f1191464730> |
| test3 | Bill | False | [] | <function Baz at 0x7f11914647b8> |
I am new to python, but I imagine there is an easy way to do this without manually having to calculate the width of each row and adjust accordingly.
Full source code can be found here: https://gitlab.com/snippets/1936949