parser API style - methods take path or file vs sep methods for path and file

Jeff Kowalczyk jtk at yahoo.com
Thu May 8 14:50:33 EDT 2003


I wrote a small application-specific data-munging CSV parser/transformer, and want to make
a suitable API around the fundamental parseLine(string) function, to support different
canned ways of using the parser in throwaway scripts.

def parseLine(s):
    return s  # transform, return row-string of csv values
def parseMultilineString(s):
    return s  # multiline string of row-strings of csv values
def parseFileToStringList(f):
    return parsedLines  # list of row-strings of csv values
def parseFileToMultilineString(f):
    return s  # multiline string of row-strings of csv values
def parseFilePathToStringList(p):
    return parsedLines  # list of row-strings of csv values
def parseFilePathToMultilineString(p):
    return s  # multiline string of row-strings of csv values

Each method after parseline was trivial due to reuse, but would I be better off (more
pythonic) to incorporate the duplicate 'Path' methods into smarter 'File' methods? It
wouldn't be hard to treat the single paramater like a file, raise an exception if
readlines() fails, try the parameter as a string filespec, and finally raise another
exception if that fails too. But *should* I do this, or are files and paths different
enough that you should duplicate methods for each to protect the API user?

Thanks.









More information about the Python-list mailing list