From sqlite3.user at gmail.com Sun Nov 21 21:02:40 2010 From: sqlite3.user at gmail.com (John Q. Public) Date: Sun, 21 Nov 2010 12:02:40 -0800 (PST) Subject: [DB-SIG] Python db programming conventions In-Reply-To: <4CBF506C.9020100@egenix.com> References: <29977345.post@talk.nabble.com> <30007234.post@talk.nabble.com> <4CBF506C.9020100@egenix.com> Message-ID: <30273508.post@talk.nabble.com> Is this how my createdb() method should look like? How would you write this method so it is both readable and correct? Thank you for your time and patience. def createdb(self): try: con = sqlite3.connect(db) cur = con.cursor() cur.execute(''' CREATE TABLE t1 ( kid INTEGER PRIMARY KEY, c1 TEXT, c2 TEXT ) ''') cur.execute(''' CREATE TABLE t2 ( kid INTEGER PRIMARY KEY, c1 TEXT, c2 TEXT ) ''') cur.execute(''' CREATE TABLE t3 ( kid INTEGER PRIMARY KEY, c1 TEXT, c2 TEXT ) ''') con.commit() except: a = "ERROR: createdb did not commit. \n" b = "tried this sql: \n" raise IOError, "%s%s%s" % ( a, b, sql ) finally: cur.close() con.close() -- View this message in context: http://old.nabble.com/Python-db-programming-conventions-tp29977345p30273508.html Sent from the Python - db-sig mailing list archive at Nabble.com. From vernondcole at gmail.com Sun Nov 21 22:10:38 2010 From: vernondcole at gmail.com (Vernon Cole) Date: Sun, 21 Nov 2010 14:10:38 -0700 Subject: [DB-SIG] Python db programming conventions In-Reply-To: <30273508.post@talk.nabble.com> References: <29977345.post@talk.nabble.com> <30007234.post@talk.nabble.com> <4CBF506C.9020100@egenix.com> <30273508.post@talk.nabble.com> Message-ID: I find this code to be very readable. I, personally, would not use quite so much vertical white space, like whole lines with only a "(", but that is a matter of aesthetics only. My senses were probably permanently marred by learning to program using punched cards and 14 7/8 inch printer paper. Vertical space killed trees back then. Also, for production code, I would specify the exceptions I expect to catch in the try: except, and then re-raise the same exception. Always raising IOERROR may lead one astray from finding the actual cause of the error. These are small things. Your program is not only correct, it's also beautiful. -- Vernon On Sun, Nov 21, 2010 at 1:02 PM, John Q. Public wrote: > > Is this how my createdb() method should look like? > How would you write this method so it is both readable and correct? > Thank you for your time and patience. > > def createdb(self): > try: > con = sqlite3.connect(db) > cur = con.cursor() > > cur.execute(''' > CREATE TABLE t1 > ( > kid INTEGER PRIMARY KEY, > c1 TEXT, > c2 TEXT > ) > ''') > > cur.execute(''' > CREATE TABLE t2 > ( > kid INTEGER PRIMARY KEY, > c1 TEXT, > c2 TEXT > ) > ''') > > cur.execute(''' > CREATE TABLE t3 > ( > kid INTEGER PRIMARY KEY, > c1 TEXT, > c2 TEXT > ) > ''') > > con.commit() > except: > a = "ERROR: createdb did not commit. \n" > b = "tried this sql: \n" > raise IOError, "%s%s%s" % ( a, b, sql ) > finally: > cur.close() > con.close() > > -- > View this message in context: > http://old.nabble.com/Python-db-programming-conventions-tp29977345p30273508.html > Sent from the Python - db-sig mailing list archive at Nabble.com. > > _______________________________________________ > DB-SIG maillist - DB-SIG at python.org > http://mail.python.org/mailman/listinfo/db-sig > -------------- next part -------------- An HTML attachment was scrubbed... URL: From farcepest at gmail.com Mon Nov 22 04:42:46 2010 From: farcepest at gmail.com (Andy Dustman) Date: Sun, 21 Nov 2010 22:42:46 -0500 Subject: [DB-SIG] Python db programming conventions In-Reply-To: <30273508.post@talk.nabble.com> References: <29977345.post@talk.nabble.com> <30007234.post@talk.nabble.com> <4CBF506C.9020100@egenix.com> <30273508.post@talk.nabble.com> Message-ID: On Sun, Nov 21, 2010 at 3:02 PM, John Q. Public wrote: > > Is this how my createdb() method should look like? > How would you write this method so it is both readable and correct? > Thank you for your time and patience. > > def createdb(self): > ? ?try: > ? ? ? ?con = sqlite3.connect(db) > ? ? ? ?cur = con.cursor() > > ? ? ? ?cur.execute(''' > ? ? ? ? ? ?CREATE TABLE t1 > ? ? ? ? ? ?( > ? ? ? ? ? ? ? ?kid INTEGER PRIMARY KEY, > ? ? ? ? ? ? ? ?c1 TEXT, > ? ? ? ? ? ? ? ?c2 TEXT > ? ? ? ? ? ?) > ? ? ? ?''') > > ? ? ? ?cur.execute(''' > ? ? ? ? ? ?CREATE TABLE t2 > ? ? ? ? ? ?( > ? ? ? ? ? ? ? ?kid INTEGER PRIMARY KEY, > ? ? ? ? ? ? ? ?c1 TEXT, > ? ? ? ? ? ? ? ?c2 TEXT > ? ? ? ? ? ?) > ? ? ? ?''') > > ? ? ? ?cur.execute(''' > ? ? ? ? ? ?CREATE TABLE t3 > ? ? ? ? ? ?( > ? ? ? ? ? ? ? ?kid INTEGER PRIMARY KEY, > ? ? ? ? ? ? ? ?c1 TEXT, > ? ? ? ? ? ? ? ?c2 TEXT > ? ? ? ? ? ?) > ? ? ? ?''') > > ? ? ? ?con.commit() > ? ?except: > ? ? ? ?a = "ERROR: createdb did not commit. \n" > ? ? ? ?b = "tried this sql: ? \n" > ? ? ? ?raise IOError, "%s%s%s" % ( a, b, sql ) > ? ?finally: > ? ? ? ?cur.close() > ? ? ? ?con.close() You never actually set sql anywhere, so you'll always get a NameError instead of IOError. It would probably be better to not catch the exception at all in this function. -- Question the answers From Chris.Clark at ingres.com Mon Nov 22 18:19:09 2010 From: Chris.Clark at ingres.com (Chris Clark) Date: Mon, 22 Nov 2010 09:19:09 -0800 Subject: [DB-SIG] Python db programming conventions In-Reply-To: References: <29977345.post@talk.nabble.com> <30007234.post@talk.nabble.com> <4CBF506C.9020100@egenix.com> <30273508.post@talk.nabble.com> Message-ID: <4CEAA60D.9050209@ingres.com> Andy Dustman wrote: > On Sun, Nov 21, 2010 at 3:02 PM, John Q. Public wrote: > >> Is this how my createdb() method should look like? >> How would you write this method so it is both readable and correct? >> Thank you for your time and patience. >> >> def createdb(self): >> try: >> con = sqlite3.connect(db) >> cur = con.cursor() >> >> cur.execute(''' >> CREATE TABLE t1 >> ( >> kid INTEGER PRIMARY KEY, >> c1 TEXT, >> c2 TEXT >> ) >> ''') >> >> cur.execute(''' >> CREATE TABLE t2 >> ( >> kid INTEGER PRIMARY KEY, >> c1 TEXT, >> c2 TEXT >> ) >> ''') >> >> cur.execute(''' >> CREATE TABLE t3 >> ( >> kid INTEGER PRIMARY KEY, >> c1 TEXT, >> c2 TEXT >> ) >> ''') >> >> con.commit() >> except: >> a = "ERROR: createdb did not commit. \n" >> b = "tried this sql: \n" >> raise IOError, "%s%s%s" % ( a, b, sql ) >> finally: >> cur.close() >> con.close() >> > > You never actually set sql anywhere, so you'll always get a NameError > instead of IOError. It would probably be better to not catch the > exception at all in this function. > If you are using Nose you can use this plugin http://exogen.github.com/nose-achievements/ and gain achievements, the code above should net you the; "Silence! I keel you!" achievement :-) All the achievements are visible at https://docs.google.com/View?id=dfsf8s3r_45388t5rdd unlocked the "Silence! I keel you!" achievement by checking in code with only bare except: clauses. Chris From vernondcole at gmail.com Mon Nov 22 20:33:58 2010 From: vernondcole at gmail.com (Vernon Cole) Date: Mon, 22 Nov 2010 12:33:58 -0700 Subject: [DB-SIG] Python db programming conventions In-Reply-To: References: <29977345.post@talk.nabble.com> <30007234.post@talk.nabble.com> <4CBF506C.9020100@egenix.com> <30273508.post@talk.nabble.com> Message-ID: On Sun, Nov 21, 2010 at 8:42 PM, Andy Dustman wrote: > > > You never actually set sql anywhere, so you'll always get a NameError > instead of IOError. It would probably be better to not catch the > exception at all in this function. > -- > Question the answers > _______________________________________________ > Thank you, Andy! I missed that. That's why open source code is a good thing. More eyes looking finds problems. This time I actually entered the code into a file and executed it. A working version follows: from __future__ import print_function import sqlite3 import sys def createdb(db): try: con = sqlite3.connect(db) cur = con.cursor() sql = ''' CREATE TABLE t1 ( kid INTEGER PRIMARY KEY, c1 TEXT, c2 TEXT )''' cur.execute(sql) sql = ''' CREATE TABLE t2 ( kid INTEGER PRIMARY KEY, c1 TEXT, c2 TEXT )''' cur.execute(sql) sql = ''' CREATE TABLE t3 ( kid INTEGER PRIMARY KEY, c1 TEXT, c2 TEXT )''' cur.execute(sql) con.commit() except sqlite3.Error: print("ERROR: createdb did not commit.", file=sys.stderr) print("tried this sql:", file=sys.stderr) print(sql, file=sys.stderr) raise finally: cur.close() con.close() if __name__ == '__main__': createdb('myDbName') This script will run once without errors. If you run it a second time, you get: ERROR: createdb did not commit. tried this sql: CREATE TABLE t1 ( kid INTEGER PRIMARY KEY, c1 TEXT, c2 TEXT ) Traceback (most recent call last): File "x.py", line 44, in createdb('myDbName') File "x.py", line 14, in createdb cur.execute(sql) sqlite3.OperationalError: table t1 already exists Which is exactly what you should expect. Note that using the naked "raise" statement results in a higher quality traceback with error text pinpointing the problem. I used print as a function, so this example should work in python versions from 2.6 to 3.2. -- Vernon -------------- next part -------------- An HTML attachment was scrubbed... URL: From sqlite3.user at gmail.com Tue Nov 23 05:16:57 2010 From: sqlite3.user at gmail.com (John Q. Public) Date: Mon, 22 Nov 2010 20:16:57 -0800 (PST) Subject: [DB-SIG] Python db programming conventions In-Reply-To: References: <29977345.post@talk.nabble.com> <30007234.post@talk.nabble.com> <4CBF506C.9020100@egenix.com> <30273508.post@talk.nabble.com> Message-ID: <30283771.post@talk.nabble.com> Thanks Andy and Vernon! The sql variable was left in the code unintentionally when I was rewriting the code in order to post it here. nevertheless, it was very enlightening to read your replies as I seldom have the opportunity to read other people python-db code. The little experience I have with database programming comes from C# + ms sql server - witch dictates very different programming style. I would appreciate it if you can reference me to open source python database programming projects so I can read and learn. Thanks again for your time and patience -- View this message in context: http://old.nabble.com/Python-db-programming-conventions-tp29977345p30283771.html Sent from the Python - db-sig mailing list archive at Nabble.com.