1.6 KiB
1.6 KiB
Exercise 9.4 - Solution
# tableformat.py
from abc import ABC, abstractmethod
def print_table(records, fields, formatter):
if not isinstance(formatter, TableFormatter):
raise RuntimeError('Expected a TableFormatter')
formatter.headings(fields)for r in records:
= [getattr(r, fieldname) for fieldname in fields]
rowdata
formatter.row(rowdata)
class TableFormatter(ABC):
= { }
_formats
@classmethod
def __init_subclass__(cls):
= cls.__module__.split('.')[-1]
name = cls
TableFormatter._formats[name]
@abstractmethod
def headings(self, headers):
pass
@abstractmethod
def row(self, rowdata):
pass
class ColumnFormatMixin:
= []
formats def row(self, rowdata):
= [ (fmt % item) for fmt, item in zip(self.formats, rowdata)]
rowdata super().row(rowdata)
class UpperHeadersMixin:
def headings(self, headers):
super().headings([h.upper() for h in headers])
def create_formatter(name, column_formats=None, upper_headers=False):
if name not in TableFormatter._formats:
__import__(f'{__package__}.formats.{name}')
= TableFormatter._formats.get(name)
formatter_cls if not formatter_cls:
raise RuntimeError('Unknown format %s' % name)
if column_formats:
class formatter_cls(ColumnFormatMixin, formatter_cls):
= column_formats
formats
if upper_headers:
class formatter_cls(UpperHeadersMixin, formatter_cls):
pass
return formatter_cls()