2.8 KiB
2.8 KiB
Exercise 9.3 - Solution
(a) Controlling Exported Symbols
# structure.py
= ['Structure']
__all__ ...
# reader.py
= [ 'read_csv_as_dicts',
__all__ 'read_csv_as_instances' ]
...
# tableformat.py
= ['create_formatter', 'print_table']
__all__ ...
(b) Exporting Everything
# structly/__init__.py
from .structure import *
from .reader import *
from .tableformat import *
= [ *structure.__all__,
__all__ *reader.__all__,
*tableformat.__all__ ]
(c) Module Splitting
There are a few parts to this. First, the individual files in
tableformat/formats
are going to look like this:
# tableformat/formats/text.py
from ..formatter import TableFormatter
class TextTableFormatter(TableFormatter):
...
# tableformat/formats/csv.py
from ..formatter import TableFormatter
class CSVTableFormatter(TableFormatter):
...
# tableformat/formats/html.py
from ..formatter import TableFormatter
class HTMLTableFormatter(TableFormatter):
...
The formatter.py
file will look like this:
# tableformat/formatter.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):
@abstractmethod
def headings(self, headers):
pass
@abstractmethod
def row(self, rowdata):
pass
from .formats.text import TextTableFormatter
from .formats.csv import CSVTableFormatter
from .formats.html import HTMLTableFormatter
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 == 'text':
= TextTableFormatter
formatter_cls elif name == 'csv':
= CSVTableFormatter
formatter_cls elif name == 'html':
= HTMLTableFormatter
formatter_cls else:
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()
Finally, you need to have a tableformat/__init__.py
file
like this:
# tableformat/__init__.py
from .formatter import print_table, create_formatter
= [ 'print_table', 'create_formatter' ] __all__