[Zope] try clause in external method

Jonathan dev101 at magma.ca
Mon Apr 14 15:22:54 EDT 2008


----- Original Message ----- 
From: "Garry Saddington" <garry at schoolteachers.co.uk>
To: <zope at 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 



More information about the Zope mailing list