forked from thedanfields/python-mastery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtableformat.py
97 lines (76 loc) · 2.72 KB
/
tableformat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from abc import ABC, abstractmethod
class TableFormatter(ABC):
@abstractmethod
def headings(self, headers):
pass
@abstractmethod
def row(self, rowdata):
pass
class TextTableFormatter(TableFormatter):
def headings(self, headers):
print(' '.join('%10s' % h for h in headers))
print(('-'*10 + ' ')*len(headers))
def row(self, rowdata):
print(' '.join('%10s' % d for d in rowdata))
class CSVFormatter(TableFormatter):
def headings(self, headers):
print(','.join(headers))
def row(self, rowdata):
print(','.join(str(d) for d in rowdata))
class HTMLTableFormatter(TableFormatter):
def headings(self, headers):
print(f"<tr> {' '.join('<th>' + header + '</th>' for header in headers)} </tr>")
def row(self, rowdata):
print(f"<tr> {' '.join('<td>' + str(data) + '</td>' for data in rowdata)} </tr>")
def print_table(records, fields, formatter):
if not isinstance(formatter, TableFormatter):
raise TypeError('Expected a TableFormatter')
formatter.headings(fields)
for record in records:
rowdata = [getattr(record, field) for field in fields]
formatter.row(rowdata)
def create_formatter(format, upper_headers=False, column_formats=[]):
formatters = {
'text': TextTableFormatter,
'csv': CSVFormatter,
'html': HTMLTableFormatter
}
if format not in formatters:
raise ValueError('Unknown format %s' % format)
main_formatter = formatters[format]()
if not upper_headers and not column_formats:
return main_formatter
parent_classes = [formatters[format]]
if upper_headers:
parent_classes.insert(0, UpperHeadersMixin)
if column_formats:
parent_classes.insert(0, ColumnFormatMixin)
class SpecialFormatter(*parent_classes):
formats = column_formats
return SpecialFormatter()
class ColumnFormatMixin:
formats = []
def row(self, rowdata):
rowdata = [(fmt % d) for fmt, d in zip(self.formats, rowdata)]
super().row(rowdata)
class UpperHeadersMixin:
def headings(self, headers):
super().headings([h.upper() for h in headers])
def print_table_orig(data, fields):
for field in fields:
print('%10s ' % (field), end="")
print()
print("---------- " * len(fields))
for entry in data:
for field in fields:
print('%10s ' % (getattr(entry, field)), end="")
print()
def create_formatter_orig(format):
if format == 'text':
return TextTableFormatter()
elif format == 'csv':
return CSVFormatter()
elif format == 'html':
return HTMLTableFormatter()
else:
raise ValueError('Unknown format %s' % format)