[Tutor] Checking if a GtkEntry is empty

Steven D'Aprano steve at pearwood.info
Sat Feb 25 03:08:02 CET 2012


Anthony Papillion wrote:

> class Manager(object):
>     def __init__(self):
>         builder = gtk.Builder()
>         builder.add_from_file("winMain.glade")
>         builder.connect_signals(self)
>         self.window = builder.get_object("winMain")
>         self.window.show()
> 
>         conn = None
>         try:
>             conn = lite.connect('docs.db')
>             sql =  "create table if not exists documents(id integer,
> name text, location text, tags text)"
>             c = conn.cursor()
>             c.execute(sql)
>             conn.commit()
>         except lite.Error, e:
>             print "Error: " + e.args[0]


Are you aware that the database connection gets made, but not stored anywhere? 
Once the Manager instance is created, the database connection is thrown away.

My guess is that you mean to use self.conn instead of conn in the above.

Which brings me to another point. It seems to me that the above is poor design.

When you create a Manager instance, if something goes wrong with the database 
entry, you end up with an instance with an unspecified, and possibly broken, 
inst.conn. That's bad, because the caller can't trust the value of inst.conn 
to be in a sensible state, and they may not have any easy way to tell the 
difference between inst.conn is safe to use and inst.conn is broken.

In other words, you should use one of these designs instead:

     # broken connections are never allowed and are always fatal errors
     def __init__(self):
         builder = gtk.Builder()
         builder.add_from_file("winMain.glade")
         builder.connect_signals(self)
         self.window = builder.get_object("winMain")
         self.window.show()
         conn = lite.connect('docs.db')
         sql =  ( "create table if not exists documents("
                  "id integer, name text, location text, tags text)" )
         c = conn.cursor()
         c.execute(sql)
         conn.commit()
         self.conn = conn


     # broken connections are allowed and given a warning
     def __init__(self):
         builder = gtk.Builder()
         builder.add_from_file("winMain.glade")
         builder.connect_signals(self)
         self.window = builder.get_object("winMain")
         self.window.show()
         try:
             conn = lite.connect('docs.db')
             sql =  ( "create table if not exists documents("
                      "id integer, name text, location text, tags text)" )
             c = conn.cursor()
             c.execute(sql)
             conn.commit()
         except lite.Error, e:
             print "Warning:", e.args[0]  # Warning, not an error.
             conn = None
         self.conn = conn


Notice that it is important to set conn = None *after* catching the exception, 
because if you do it before, it will be changed to a broken connection before 
you save it as self.conn.

Also note that you shouldn't frighten users with "Error" when it isn't an 
error, merely a warning, nor should you hide useful tracebacks for fatal errors.



-- 
Steven


More information about the Tutor mailing list