|
1 | 1 | #!/usr/bin/env python
|
2 | 2 | # -*- coding: utf-8 -*-
|
3 | 3 |
|
4 |
| -from __future__ import print_function, unicode_literals |
5 |
| -from .exceptions import SupressedError |
| 4 | +from __future__ import absolute_import, print_function, unicode_literals |
6 | 5 |
|
7 |
| -class Table(): |
| 6 | +import io |
| 7 | + |
| 8 | +from .exceptions import (SuppressedError, |
| 9 | + TooManyValues, |
| 10 | + DoNotOverwrite) |
| 11 | + |
| 12 | + |
| 13 | +class Table: |
8 | 14 | """docstring for Table.
|
9 | 15 | This is the main class. It adds rows and columns, with data
|
10 | 16 | """
|
11 |
| - def __init__(self, name, debug = True): |
| 17 | + |
| 18 | + def __init__(self, name, debug=True): |
12 | 19 | super(Table, self).__init__()
|
13 |
| - self.debug = debug |
| 20 | + self.todebug = debug |
14 | 21 | self.rows = 0
|
15 | 22 | self.columns = 1
|
16 | 23 | self.table = '''|{}|'''.format(str(name))
|
17 | 24 | self.finalized = False
|
18 |
| - if self.debug: |
| 25 | + if self.todebug: |
19 | 26 | self.functions = []
|
| 27 | + self.finalized_run = False |
20 | 28 |
|
21 |
| - def debug(self): |
| 29 | + def debug(self, print_d=True): |
| 30 | + """ |
| 31 | + :rtype: str |
| 32 | + """ |
| 33 | + global dmessage |
22 | 34 | try:
|
23 |
| - print('''Printing debug information: |
| 35 | + dmessage = '''Printing debug information: |
24 | 36 | Rows: {rows}
|
25 | 37 | Columns: {cols}
|
26 | 38 | Finalized?: {fin}
|
27 | 39 | Table Content: {table}
|
28 |
| - Functions: {funcs}'''.format(rows = str(self.rows), |
29 |
| - cols = str(self.columns), |
30 |
| - fin = str(self.finalized), |
31 |
| - table = self.table, |
32 |
| - funcs = self.functions)) |
33 |
| - except(NameError): |
34 |
| - pass |
35 |
| - except Exception as e: |
| 40 | + Functions: {funcs}'''.format(rows=str(self.rows), |
| 41 | + cols=str(self.columns), |
| 42 | + fin=str(self.finalized), |
| 43 | + table=self.table, |
| 44 | + funcs=self.functions) |
| 45 | + if print_d: |
| 46 | + print(dmessage) |
| 47 | + else: |
| 48 | + return dmessage |
| 49 | + except NameError: |
| 50 | + pass |
| 51 | + except Exception as e: |
| 52 | + raise SuppressedError(type(e).__name__, str(e), dmessage) |
36 | 53 |
|
37 |
| - def add_column(self, name, all_cols = False): |
| 54 | + def add_column(self, name, all_cols=False): |
38 | 55 | self.columns += 1
|
39 | 56 | self.table += '{}|'.format(str(name))
|
40 | 57 | try:
|
41 | 58 | if all_cols:
|
42 |
| - return {'function':'add_column', 'data': [str(name)]} |
| 59 | + return {'function': 'add_column', 'data': [str(name)]} |
43 | 60 | else:
|
44 |
| - self.functions.append({'function':'add_column', 'data': [str(name)]}) |
45 |
| - except(NameError): |
| 61 | + self.functions.append({'function': 'add_column', 'data': [str(name)]}) |
| 62 | + except NameError: |
46 | 63 | pass
|
| 64 | + except Exception as e: |
| 65 | + raise SuppressedError(type(e).__name__, str(e), self.debug(print_d=False)) |
47 | 66 |
|
48 | 67 | def all_columns(self, *args):
|
49 |
| - all_col_data = {'function':'all_columns', 'data': []} |
50 |
| - for value in args: |
51 |
| - all_col_data['data'].append(self.add_column(str(value), all_cols = True)) |
| 68 | + try: |
| 69 | + all_col_data = {'function': 'all_columns', 'data': []} |
| 70 | + for value in args: |
| 71 | + all_col_data['data'].append(self.add_column(str(value), all_cols=True)) |
| 72 | + self.functions.append(all_col_data) |
| 73 | + except Exception as e: |
| 74 | + raise SuppressedError(type(e).__name__, str(e), self.debug(print_d=False)) |
52 | 75 |
|
53 | 76 | def finalize_cols(self):
|
54 |
| - finalizer = '\n|' |
55 |
| - for i in range(self.columns): |
56 |
| - finalizer += '---|' |
57 |
| - self.table += finalizer |
58 |
| - |
59 |
| - def add_row(self,show_warning_message = True, *args): |
60 |
| - if not self.finalized: |
61 |
| - self.finalize_cols() |
62 |
| - self.finalized = True |
63 |
| - self.rows += 1 |
64 |
| - row = '|' |
65 |
| - rows_made = 0 |
66 |
| - for i in range(int(len(args))): |
67 |
| - row += '{}|'.format(str(args[i])) |
68 |
| - rows_made += 1 |
69 |
| - if self.columns > rows_made: |
70 |
| - if show_warning_message: |
71 |
| - 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))) |
72 |
| - for i in range(int(self.columns-rows_made)): |
73 |
| - row += ' |' |
74 |
| - elif self.columns < rows_made: |
75 |
| - raise AssertionError('You entered in too many row values. Please only enter {} row names.'.format(str(self.columns))) |
76 |
| - self.table += '\n{}'.format(row) |
| 77 | + try: |
| 78 | + finalizer = '\n|' |
| 79 | + for i in range(self.columns): |
| 80 | + finalizer += '---|' |
| 81 | + self.table += finalizer |
| 82 | + self.functions.append({'function': 'finalize_cols', 'data': finalizer}) |
| 83 | + except Exception as e: |
| 84 | + raise SuppressedError(type(e).__name__, str(e), self.debug(print_d=False)) |
77 | 85 |
|
| 86 | + def add_row(self, show_warning_message=True, *args): |
| 87 | + try: |
| 88 | + if self.finalized_run: |
| 89 | + self.finalized_run = False |
| 90 | + if not self.finalized: |
| 91 | + self.finalize_cols() |
| 92 | + self.finalized_run = True |
| 93 | + self.finalized = True |
| 94 | + add_row_data = {'function': 'add_row', |
| 95 | + 'data': {'finalized_run': self.finalized_run, |
| 96 | + 'show_warning_message': show_warning_message, |
| 97 | + 'values': []}} |
| 98 | + self.rows += 1 |
| 99 | + row = '|' |
| 100 | + rows_made = 0 |
| 101 | + for i in range(int(len(args))): |
| 102 | + row += '{}|'.format(str(args[i])) |
| 103 | + rows_made += 1 |
| 104 | + add_row_data['data']['values'].append(args[i]) |
| 105 | + if self.columns > rows_made: |
| 106 | + if show_warning_message: |
| 107 | + print( |
| 108 | + "You did not enter in enough values. You entered in {} values out of {} values. The values " |
| 109 | + "that you did not enter in will be filled in with a blank space. You can stop this message " |
| 110 | + "from appearing by adding the argument show_warning_message = False".format( |
| 111 | + str(rows_made), str(self.columns))) |
| 112 | + add_row_data['data']['message_shown'] = True |
| 113 | + for i in range(int(self.columns - rows_made)): |
| 114 | + row += ' |' |
| 115 | + add_row_data['data']['values'].append('{} blank values added'.format(str(int(self.columns) - int( |
| 116 | + rows_made)))) |
| 117 | + elif self.columns < rows_made: |
| 118 | + raise TooManyValues(self.columns) |
| 119 | + self.table += '\n{}'.format(row) |
| 120 | + self.functions.append(add_row_data) |
| 121 | + except TooManyValues: |
| 122 | + raise |
| 123 | + except Exception as e: |
| 124 | + raise SuppressedError(type(e).__name__, str(e), self.debug(print_d=False)) |
78 | 125 |
|
79 |
| - def export_table_to_file(self, filename = 'markdowntable', extension='txt', mode = 'w+', overwrite_warning = True): |
80 |
| - with open('{fname}.{ext}'.format(fname = str(filename), ext = str(extension)),str(mode)) as file: |
81 |
| - try: |
82 |
| - contents = file.read() |
83 |
| - if mode == 'w' or mode == 'w+': |
84 |
| - mode_check = True |
85 |
| - if len(contents) > 0 and overwrite_warning and mode_check: |
86 |
| - 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.') |
87 |
| - except(io.UnsupportedOperation): |
88 |
| - pass |
89 |
| - file.write(table) |
90 |
| - return True |
| 126 | + def export_table_to_file(self, filename='markdowntable', extension='txt', mode='w+', overwrite_warning=True): |
| 127 | + global mode_check, message_displayed, fileread |
| 128 | + try: |
| 129 | + with open('{fname}.{ext}'.format(fname=str(filename), ext=str(extension)), str(mode)) as file: |
| 130 | + try: |
| 131 | + contents = file.read() |
| 132 | + print(contents) |
| 133 | + if mode == 'w' or mode == 'w+': |
| 134 | + mode_check = True |
| 135 | + if len(contents) > 0 and overwrite_warning and mode_check: |
| 136 | + print( |
| 137 | + 'This file already contains content. Do you want to overwrite the contents of the file? ' |
| 138 | + 'You can add the argument mode = a[+] to not overwrite.') |
| 139 | + true = True |
| 140 | + false = False |
| 141 | + overwrite = input('Write True or False:') |
| 142 | + if overwrite: |
| 143 | + raise DoNotOverwrite |
| 144 | + message_displayed = True |
| 145 | + fileread = True |
| 146 | + except io.UnsupportedOperation: |
| 147 | + pass |
| 148 | + except DoNotOverwrite: |
| 149 | + pass |
| 150 | + else: |
| 151 | + file.write(self.table) |
| 152 | + self.functions.append({'function': 'export_table_to_file', |
| 153 | + 'data': {'filename': filename, 'extension': extension, 'writemode': mode, |
| 154 | + 'overwrite_warning': overwrite_warning, 'file read': fileread}}) |
| 155 | + except Exception as e: |
| 156 | + raise SuppressedError(type(e).__name__, str(e), self.debug(print_d=False)) |
0 commit comments