A Pythonic way for "resume next" on exceptions?
The problem: I am reading a series of heterogeneous input files. I wrote a
reader class for each of them, which reads the file using init(self,
file_name), and throws an exception in case of malformed input.
The code looks like this:
clients = Clients ('Clients.csv' )
simulation = Simulation ('Simulation.csv' )
indicators = Indicators ('Indicators.csv' )
legalEntity = LegalEntity ('LegalEntity.csv' )
defaultPortfolio = DefaultPortfolio ('DefaultPortfolio.csv' )
excludedProductTypes = ExcludedProductTypes('ExcludedProductTypes.csv')
The problem is that I want not to die at the first malformed file, but
instead read all of them and THEN die if at least one was malformed. The
only way to do it that I could find looks horrible:
my errors = []
try:
clients = Clients ('Clients.csv' )
except Exception, e:
errors.append(e)
try:
simulation = Simulation ('Simulation.csv' )
except Exception, e:
errors.append(e)
try:
indicators = Indicators ('Indicators.csv' )
except Exception, e:
errors.append(e)
try:
legalEntity = LegalEntity ('LegalEntity.csv' )
except Exception, e:
errors.append(e)
try:
defaultPortfolio = DefaultPortfolio ('DefaultPortfolio.csv' )
except Exception, e:
errors.append(e)
try:
excludedProductTypes = ExcludedProductTypes('ExcludedProductTypes.csv')
except Exception, e:
errors.append(e)
if len(errors) > 0:
raise MultipleErrors(errors)
Is there a better looking way to approach the problem?
No comments:
Post a Comment