I have the following external method which takes an input .csv file and enters the data into a database by calling a ZSQL method. There may be errors in the input file and so I am trying to catch them and insert each bad row in a different table. The errors are detected by table constraints on the table definition and should be reported by the Psycopg connector. However no correct rows are entered and only the last error row is entered. Can anyone see what I am doing wrong in this script? def timetabler(self,REQUEST): import csv,string f=REQUEST.form["TTEXPORT.CSV"] from cStringIO import StringIO filebody=StringIO(f.read()) r = csv.DictReader(filebody("day","periodstart","periodend" ,"year","teachgroup","teacher" ,"subject" ,"set" ,"room")) for row in r: try: self.insertintoiclasses(**row) except: self.insertimporterrors(**row) dtmlMethodName="manage_import_from_timetabler" return self[dtmlMethodName](self,self.REQUEST) regards garry
----- Original Message ----- From: "Garry Saddington" <garry@schoolteachers.co.uk> To: <zope@zope.org> Sent: Monday, April 14, 2008 3:19 PM Subject: [Zope] try clause in external method
I have the following external method which takes an input .csv file and enters the data into a database by calling a ZSQL method. There may be errors in the input file and so I am trying to catch them and insert each bad row in a different table. The errors are detected by table constraints on the table definition and should be reported by the Psycopg connector. However no correct rows are entered and only the last error row is entered. Can anyone see what I am doing wrong in this script?
def timetabler(self,REQUEST): import csv,string f=REQUEST.form["TTEXPORT.CSV"] from cStringIO import StringIO filebody=StringIO(f.read()) r = csv.DictReader(filebody("day","periodstart","periodend" ,"year","teachgroup","teacher" ,"subject" ,"set" ,"room")) for row in r: try: self.insertintoiclasses(**row) except: self.insertimporterrors(**row) dtmlMethodName="manage_import_from_timetabler" return self[dtmlMethodName](self,self.REQUEST)
A couple of things... 1) Have bare try/except clauses may cause you grief ;-) 2) When an error is trapped by the 'try' statement the transaction is not committed (ie. nothing is written to your db), only the 'except' statement is executed. Jonathan
Jonathan wrote:
A couple of things...
1) Have bare try/except clauses may cause you grief ;-)19
Specifically, in this case, I'll bet an "unexpected exception" is being raised and swallowed...
2) When an error is trapped by the 'try' statement the transaction is not committed (ie. nothing is written to your db), only the 'except' statement is executed.
Actually, I don't think that's true. The transaction will be committed by the publisher machiner that calls the external method... Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk
participants (3)
-
Chris Withers -
Garry Saddington -
Jonathan