Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #86008

Re: try pattern for database connection with the close method

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 <ckaynor@zindagigames.com>
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&gt;': 0.22; 'to:name:python-list@python.org': 0.22; 'finally,': 0.24; '&gt;': 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 <ckaynor@zindagigames.com>
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" <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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.18943.1424488028.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On Fri, Feb 20, 2015 at 6:42 PM, Mario Figueiredo <marfig@gmail.com> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

try pattern for database connection with the close method Mario Figueiredo <marfig@gmail.com> - 2015-02-21 03:42 +0100
  Re: try pattern for database connection with the close method Chris Kaynor <ckaynor@zindagigames.com> - 2015-02-20 18:59 -0800
  Re: try pattern for database connection with the close method Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-02-21 12:22 +0000
    Re: try pattern for database connection with the close method Mario Figueiredo <marfig@gmail.com> - 2015-02-22 19:41 +0100
      Re: try pattern for database connection with the close method Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-02-22 19:07 +0000
        Re: try pattern for database connection with the close method Mario Figueiredo <marfig@gmail.com> - 2015-02-23 00:25 +0100
      Re: try pattern for database connection with the close method Skip Montanaro <skip.montanaro@gmail.com> - 2015-02-22 13:15 -0600
        Re: try pattern for database connection with the close method Mario Figueiredo <marfig@gmail.com> - 2015-02-23 00:30 +0100
  Re: try pattern for database connection with the close method Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-21 08:16 -0700
  Re: try pattern for database connection with the close method Peter Otten <__peter__@web.de> - 2015-02-21 16:22 +0100
    Re: try pattern for database connection with the close method Mario Figueiredo <marfig@gmail.com> - 2015-02-23 00:35 +0100
  Re: try pattern for database connection with the close method Peter Otten <__peter__@web.de> - 2015-02-21 16:27 +0100
  Re: try pattern for database connection with the close method Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-21 08:50 -0700
  Re: try pattern for database connection with the close method Peter Otten <__peter__@web.de> - 2015-02-21 18:02 +0100

csiph-web