|
| 1 | +from __future__ import print_function, unicode_literals |
| 2 | + |
| 3 | + |
| 4 | +class Table(): |
| 5 | + """docstring for Table. |
| 6 | + This is the main class. It adds rows and columns, with data |
| 7 | + """ |
| 8 | + def __init__(self, name): |
| 9 | + super(Table, self).__init__() |
| 10 | + self.rows = 0 |
| 11 | + self.columns = 1 |
| 12 | + self.table = '''|{}|'''.format(str(name)) |
| 13 | + self.finalized = False |
| 14 | + |
| 15 | + def debug(self): |
| 16 | + print('''Printing debug information: |
| 17 | + Rows: {rows} |
| 18 | + Columns: {cols} |
| 19 | + Finalized?: {fin} |
| 20 | + Table Content: {table}'''.format(rows = str(self.rows), |
| 21 | + cols = str(self.columns), |
| 22 | + fin = str(self.finalized), |
| 23 | + table = self.table)) |
| 24 | + |
| 25 | + def add_column(self, name): |
| 26 | + self.columns += 1 |
| 27 | + self.table += '''{}|'''.format(name) |
| 28 | + |
| 29 | + def all_columns(self, *args): |
| 30 | + for value in args: |
| 31 | + self.add_column(str(value)) |
| 32 | + |
| 33 | + def finalize_cols(self): |
| 34 | + finalizer = '\n|' |
| 35 | + for i in range(self.columns): |
| 36 | + finalizer += '---|' |
| 37 | + self.table += finalizer |
| 38 | + |
| 39 | + def add_row(self,show_warning_message = True, *args): |
| 40 | + if not self.finalized: |
| 41 | + self.finalize_cols() |
| 42 | + self.finalized = True |
| 43 | + self.rows += 1 |
| 44 | + row = '|' |
| 45 | + rows_made = 0 |
| 46 | + for i in range(int(len(args))): |
| 47 | + row += '{}|'.format(str(args[i])) |
| 48 | + rows_made += 1 |
| 49 | + if self.columns > rows_made: |
| 50 | + if show_warning_message: |
| 51 | + print('You did not enter in enough values. You entered in {} values out of {} values. The values that you did not enter in will be filled in with a blank space. You can stop this message from appearing by adding the argument show_warning_message = False'.format(str(rows_made), str(self.columns))) |
| 52 | + for i in range(int(self.columns-rows_made)): |
| 53 | + row += ' |' |
| 54 | + elif self.columns < rows_made: |
| 55 | + raise AssertionError('You entered in too many row values. Please only enter {} row names.'.format(str(self.columns))) |
| 56 | + self.table += '\n{}'.format(row) |
| 57 | + |
| 58 | + def get_table(self): |
| 59 | + return self.table |
| 60 | + |
| 61 | + |
| 62 | + def export_table_to_file(self, filename = 'markdowntable', extension='txt', mode = 'w+', overwrite_warning = True): |
| 63 | + with open('{fname}.{ext}'.format(fname = str(filename), ext = str(extension)),str(mode)) as file: |
| 64 | + try: |
| 65 | + contents = file.read() |
| 66 | + if mode == 'w' or mode == 'w+': |
| 67 | + mode_check = True |
| 68 | + if len(contents) > 0 and overwrite_warning and mode_check: |
| 69 | + print('This file already contains content. Do you want to overwrite the contents of the file? You can add the argument mode = a[+] to not overwrite.') |
| 70 | + except(io.UnsupportedOperation): |
| 71 | + pass |
| 72 | + file.write(table) |
| 73 | + return True |
0 commit comments