Created
February 17, 2024 08:05
-
-
Save TotallyNotAHaxxer/a33196a42ffb9cf26c0f5cc831caad4d to your computer and use it in GitHub Desktop.
Table class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
| This class is super easy to use. We can use it as a much more safer, faster | |
| and lightweight class for organizing information in a standard ASCII table | |
| format. The reason I did this is to increase speed, versatility, and save | |
| Python a few sweats in memory when we try to print other sets of info on | |
| a table. Also this prevents formatting mistakes and is also really fun | |
| and cool to tune and use for development purposes. | |
| | |
| | |
| Made by : Totally_Not_A_Haxxer | |
| Made for: BH :) | |
| GitHub : https://github.com/TotallyNotAHaxxer | |
""" | |
# for performance reasons | do not do this in production, importing line by line is better and faster on Pythons compiler rather than all in one line. | |
import argparse, textwrap | |
class ASCII_Table_Generator: | |
def __init__(self, ROWS, COLS, COLWIDTH): | |
self.rows = ROWS | |
self.columns = COLS | |
self.columnwidths = COLWIDTH | |
def GenerateTable(self): | |
TB = "" | |
HEAD = "|" | |
for i, column in enumerate(self.columns): | |
HEAD += " {:<{}} |".format(column, self.columnwidths[i]) | |
TB += "-" * len(HEAD) + "\n" | |
TB += HEAD + "\n" | |
TB += "-" * len(HEAD) + "\n" | |
for RowInsert in self.rows: | |
ROW = "|" | |
for i, v in enumerate(RowInsert): | |
wrapped_value = textwrap.fill(v, width=self.columnwidths[i]) | |
ROW += " {:<{}} |".format(wrapped_value, self.columnwidths[i]) | |
TB += ROW + "\n" | |
return TB | |
# We need a two dimensional array here | |
ROW = [ | |
["Name of audit to run", "-a", "--audit"], | |
["Save parameters as defaults for the next test run", "-s", "--save"] | |
] | |
# columns only span across one dimension- as this is easier for header | |
COL = ['Description', 'Value', 'Long Flag'] | |
print(ASCII_Table_Generator( | |
ROW, # rows to add | |
COL, # columns that we need | |
[50, 10, 20] | |
).GenerateTable()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment