Arthur said:
What is the best way to do data source abtraction? For example have
different classes with the same interface, but different
implementations.
I was thinking of almost having classA as my main class, and have
classA dynamically "absorb" classFood into to based on the extension
of the input file received by classA. But this doesn't seem possible.
Please advise.
Thank you.
The best method I've found is to have a class that abstracts
the data and presents the values from the data source via
class attributes. If you also implement it as an iterator
(e.g. give it __iter__ and __next__ methods), you can easily
iterate over the resultset from each data source. With this
method I've abstracted data in CSV files, fixed ASCII files,
SQL tables, Excel Spreadsheets, tab delimited files that are
members of a .ZIP archive, you name it.
Short example (not tested):
class foo:
'''
Class to abstract tab delimited files
'''
def __init__(self, filepath, columnnames):
self.fp=open(filepath, 'r')
self.columnnames=columnnames
return
def __iter__(self):
return self
def next(self):
#
# Try to get the next line from file
#
try: line=self.fp.next()
except StopIteration:
self.fp.close()
raise
#
# Decode the tab delimited line into its parts
#
line=line.rstrip()
if not line: raise StopIteration
values=line.split('\t')
print "values=", values
l=zip(self.columnnames, values)
print l
for column, value in l:
setattr(self, column, value)
return
if __name__ == "__main__":
obj=foo('abc.txt', ['name', 'address1', 'address2', 'city', 'state', 'zip'])
for entry in obj:
print ""
print "Name........", obj.name
print "Address1....", obj.address1
print "Address2....", obj.address2
print "City........", obj.city
print "State.......", obj.state
print "Zip.........", obj.zip
-Larry Bates