Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Template class and class design on concrete example xl2csv writer

Reply
Thread Tools

Template class and class design on concrete example xl2csv writer

 
 
Karim
Guest
Posts: n/a
 
      11-23-2011
Hello All,

I have the following code and I am quite satisfied with its design BUT I
have the feeling I can do better.
Basically the, main() execute the program (I did not put the parsing of
arguments function). I am opening
an Excel document and writing content in a CSV one w/ different format.
The design is an abstract template
class XLWriter, and derived 'Xl2Csv', 'Xl2Scsv', 'Xl2text' classes to
write the correct ASCII DOCUMENT to correct format.
The property hook method file_format is implement in these classes and
return an object of type 'XlCsvFileFormat' or 'XlTabFileFormat'.
It allows to put the right file extension and delimiter. These class are
derived from standard csv.excel and csv.excel_tab.
At last a Factory class MakeXlWriter has the job to create the correct
writer.

For now I did not add a strategy pattern which usually goes with the
Template pattern.

Except from that all better design or others critics will be welcome.


Regards
karim

__________________________________________________ __________________________________________________ ___________________________________

from __future__ import print_function

import sys, os, argparse, csv, xlrd

__all__ = ['main', 'Xl2CsvError', 'XLWriter', 'XlCsvFileFormat',
'XlTabFileFormat', 'Xl2Csv', 'Xl2Scsv', 'Xl2text', 'MakeXlWriter']


class Xl2CsvError(Exception):
"""The exception class to manage the internal program errors."""
pass

class XlWriter(object):
"""Abstract template class."""
def __init__(self, xlfilename=None, sheets=None):
"""Initializer."""
if self.__class__.__name__ == 'XlWriter':
raise TypeError('Abstract template Class XlWriter could not
be instanciated directly!')

if not xlfilename:
raise Xl2CsvError('Please provide a non empty file name!')
else:
self._source_name = xlfilename

self._book = xlrd.open_workbook(xlfilename)

if sheets is not None:
if isinstance(sheets[0], int):
self._selected_sheets =
[self._book.sheet_by_index(sheetnum-1) for sheetnum in sheets]
elif isinstance(sheets[0], str):
try:
self._selected_sheets =
[self._book.sheet_by_name(sheetname) for sheetname in sheets]
except xlrd.biffh.XLRDError, e:
print('{0} in file document {1}'.format(e, xlfilename))
sys.exit(1)
else:
raise Xl2CsvError('Sheets element type not recognized!')
else:
self._selected_sheets = self._book.sheets()

def write(self):
"""The file extraction public method."""

for sheet in self._selected_sheets:
xlfilename = '{sheet}{ext}'.format(sheet=sheet.name,
ext='.'+self.file_format.extension.lower())
try:
writer = csv.writer(open(xlfilename, 'wb'),
delimiter=self.file_format.delimiter)
print("Creating csv file '{file}' for sheet '{sheet}'
contained in document {src} ...".format(
sheet=sheet.name, file=xlfilename,
src=self._source_name), end=' ')
for row in xrange(sheet.nrows):
writer.writerow(sheet.row_values(row))
print('Done.')
except csv.Error, e:
print(e)
return 1

return 0

@property
def file_format(self):
"""Hook method. Need to implement in derived classes.

Should return an XLAsciiFileFormat object to get file extension
and inner delimiter.
"""
pass

class XlCsvFileFormat(csv.excel):
"""Add file extension to the usual properties of Excel-generated
CSV files."""
extension = 'CSV'

class XlTabFileFormat(csv.excel_tab):
"""Add file extension to the usual properties of Excel-generated
<TAB> delimited files."""
extension = 'TXT'

class Xl2Csv(XlWriter):
@property
def file_format(self):
"""Hook factory method"""
return XlCsvFileFormat()

class Xl2Scsv(XlWriter):
@property
def file_format(self):
"""Hook factory method"""
_format = XlCsvFileFormat()
_format.extension = 'SCSV'
_format.delimiter = ';'
return _format

class Xl2Text(XlWriter):
@property
def file_format(self):
"""Hook factory method"""
return XlTabFileFormat()

class MakeXlWriter(object):
"""Factory class for XLWriter objects.
"""
@staticmethod
def make(xlfilename=None, sheets=None, extension='CSV'):
if extension == "TXT":
return Xl2Text(xlfilename=xlfilename, sheets=sheets)
elif extension == "SCSV":
return Xl2Scsv(xlfilename=xlfilename, sheets=sheets)
elif extension == "CSV":
return Xl2Csv(xlfilename=xlfilename, sheets=sheets)

def main():
"""Main of this application"""
args = _process_command_line()
args.xl.close()
writer = MakeXlWriter.make(xlfilename=args.xl.name,
sheets=args.sheets, extension=args.ext)
return writer.write()

__________________________________________________ __________________________________________________ _______________________

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Concrete example of data interchange using xml document and schema WideBoy XML 0 01-04-2006 10:59 AM
[Reflection] Possible to query instance of abstract top level classwithout having the .class file of the concrete class? Stefan Siegl Java 1 08-19-2004 02:26 AM
Isn't DataGrid.Render(writer) supposed to automatically call RenderBeginTag(writer) and RenderEndTag(writer)? Henri ASP .Net Building Controls 1 07-13-2004 10:27 PM
JDOM simple concrete example Peter the Swede Java 1 11-12-2003 09:35 AM



Advertisments