Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.025 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'else:': 0.03; 'finally:': 0.07; 'subject:method': 0.09; 'try:': 0.09; 'comments:': 0.16; 'corrupt': 0.16; 'different,': 0.16; 'lite': 0.16; 'wrote:': 0.18; 'normally': 0.19; 'feb': 0.22; 'import': 0.22; 'email addr:gmail.com>': 0.22; 'to:name:python-list@python.org': 0.22; 'finally,': 0.24; '>': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'raise': 0.29; 'statement': 0.30; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'indentation': 0.31; 'file': 0.32; 'probably': 0.32; 'call.': 0.33; 'fri,': 0.33; 'level.': 0.33; 'subject:the': 0.34; 'received:74.125.82': 0.34; 'could': 0.34; 'subject:with': 0.35; 'except': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'transition': 0.36; 'two': 0.37; 'skip:& 10': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'received:74.125': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'remove': 0.60; 'such': 0.63; '20,': 0.68; 'statement,': 0.68; 'bulk': 0.74; '2015': 0.84; 'subject:try': 0.84 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=eoDEhilWyb9/C0qFW/ADnITr+Ol+aPwYlj3ikEqPKcg=; b=LssSR+G8Y/Qnt+rq0mSxGi7zlcRgkit1I+GW7myI4bQOjkwl5OoeL6T7yRTy/wzqQF lpO7kfe50POmtLVFOuhJn8Vr16uxr+HEakZE+QNs4yXe5wRCeE+UlmIGbMzhXrf9qADb yJKyF1XxD+tvFgoun+2Ae2CwcAu2hPdGaGJ/24dAGuXih2H99Ez/Pd7SHWmkojxmTOy7 2byNKNATnOUzxdKad86L9Q0XoxLNKMYjZj4anyX5zgT76X3CTmJapAIWjif74lGD3C6Q qnAkwy1xJ6hF5Pj6vqp6c6WQCHv+4ocnLBLlGdp9W5WvDCgUDXPtAfMi8sBkQ0ougcER 3Nrg== X-Gm-Message-State: ALoCoQmgz/h/97yy62YhGoDPGomOcaC3QTMUOk+WSBqe7j8cejh1TLXm7Vennw2ypteGZANh0xkW X-Received: by 10.180.73.241 with SMTP id o17mr719726wiv.16.1424487563359; Fri, 20 Feb 2015 18:59:23 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <6trfeate2ppvm1mcapgr0g4g2fd3vceab6@4ax.com> References: <6trfeate2ppvm1mcapgr0g4g2fd3vceab6@4ax.com> From: Chris Kaynor Date: Fri, 20 Feb 2015 18:59:03 -0800 Subject: Re: try pattern for database connection with the close method To: "python-list@python.org" Content-Type: multipart/alternative; boundary=f46d04374947338bde050f905bc8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 79 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424488028 news.xs4all.nl 2898 [2001:888:2000:d::a6]:49459 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86008 --f46d04374947338bde050f905bc8 Content-Type: text/plain; charset=UTF-8 On Fri, Feb 20, 2015 at 6:42 PM, Mario Figueiredo wrote: > import sqlite3 as lite > > try: > db = lite.connect('data.db') > except lite.DatabaseError: > raise OSError('database file corrupt or not found.') > else: > try: > with db: > db.execute(sql, parms) > except lite.IntegrityError: > raise ValueError('invalid data') > finally: > db.close() Two comments: You could remove the "else" statement, as it will work exactly the same with or without it. This will reduce the indentation of the bulk of the code by 1 level. You MIGHT be able to remove the finally...close as the with-statement probably does the same thing. I do not know sqlite3, however, so it may do something different, such as committing, but that would normally be on some transition object you get from a call. Basically, you could probably get the same result with (untested): try: db = lite.connect('data.db') except lite.DatabaseError: raise OSError('database file corrupt or not found.') try: with db: db.execute(sql, parms) except lite.IntegrityError: raise ValueError('invalid data') # You may still need the finally, depending on what the with statement does in sqlite3 - you'd have to check the documentation. Chris --f46d04374947338bde050f905bc8 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On Fri, Feb 20, 2015 at 6:42 PM, Mario Figueiredo <marfig@gmail.com> wrote:
> =C2= =A0 =C2=A0 =C2=A0 =C2=A0 import sqlite3 as lite
>
> =C2=A0 =C2= =A0 =C2=A0 =C2=A0 try:
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 db= =3D lite.connect('data.db')
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 ex= cept lite.DatabaseError:
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = raise OSError('database file corrupt or not found.')
> =C2=A0= =C2=A0 =C2=A0 =C2=A0 else:
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 try:
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 wi= th db:
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 db.execute(sql, parms)
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 except lite.IntegrityError:
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 raise ValueError('invalid data')
>= ; =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 finally:
> =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 db.close()

Two comments:
You could remove the "else" statement, as it will work exac= tly the same with or without it. This will reduce the indentation of the bu= lk of the code by 1 level.
You MIGHT be able to remove the finally...clo= se as the with-statement probably does the same thing. I do not know sqlite= 3, however, so it may do something different, such as committing, but that = would normally be on some transition object you get from a call.

Bas= ically, you could probably get the same result with (untested):
try:
= =C2=A0 =C2=A0 db =3D lite.connect('data.db')
except lite.Databas= eError:
=C2=A0 =C2=A0 raise OSError('database file corrupt or not fo= und.')
try:
=C2=A0 =C2=A0 with db:
=C2=A0 =C2=A0 =C2=A0 =C2=A0= db.execute(sql, parms)
except lite.IntegrityError:
=C2=A0 =C2=A0 rai= se ValueError('invalid data')
# You may still need the finally, = depending on what the with statement does in sqlite3 - you'd have to ch= eck the documentation.

Chris
--f46d04374947338bde050f905bc8--