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


Groups > comp.lang.python > #57805

Re: Try-except for flow control in reading Sqlite

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <antoon.pardon@rece.vub.ac.be>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'received:134': 0.05; 'schema': 0.05; 'correct.': 0.07; 'advance': 0.07; 'exist,': 0.09; 'lookup': 0.09; 'lost.': 0.09; 'parsing': 0.09; 'try:': 0.09; 'creates': 0.14; '(easier': 0.16; 'fails.': 0.16; 'flow.': 0.16; 'forgiveness': 0.16; 'sqlite': 0.16; 'subject:flow': 0.16; 'wrote:': 0.18; 'file,': 0.19; 'this?': 0.23; 'header:User- Agent:1': 0.23; 'controlling': 0.24; 'entries': 0.24; 'script': 0.25; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; "doesn't": 0.30; 'database,': 0.30; '-0700,': 0.31; "d'aprano": 0.31; 'file:': 0.31; 'ordinary': 0.31; 'steven': 0.31; 'file': 0.32; 'another': 0.32; 'text': 0.33; 'open': 0.33; 'checking': 0.33; "i'd": 0.34; 'problem': 0.35; 'except': 0.35; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'acceptable': 0.36; 'achieving': 0.36; 'doing': 0.36; 'hi,': 0.36; 'error.': 0.37; 'to:addr:python-list': 0.38; 'previous': 0.38; 'to:addr:python.org': 0.39; 'between': 0.67; 'fails,': 0.84; 'pardon': 0.84; 'victor': 0.84; 'race': 0.95; '2013': 0.98
X-IronPort-Anti-Spam-Filtered true
X-IronPort-Anti-Spam-Result Ap8EAOwxblKGuA9G/2dsb2JhbABZv3iCeoE2gxkBAQV4EQsYCRYPCQMCAQIBRRMIAogDrxKJFI9cFoQWA5Qqg2CGJ4tggyc
Date Mon, 28 Oct 2013 10:48:16 +0100
From Antoon Pardon <antoon.pardon@rece.vub.ac.be>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:10.0.12) Gecko/20130116 Icedove/10.0.12
MIME-Version 1.0
To python-list@python.org
Subject Re: Try-except for flow control in reading Sqlite
References <ac7cf18f-d4f4-41ef-966d-a35d2d0e39a8@googlegroups.com> <526e01a1$0$2838$c3e8da3$76491128@news.astraweb.com>
In-Reply-To <526e01a1$0$2838$c3e8da3$76491128@news.astraweb.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
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.1692.1382953705.18130.python-list@python.org> (permalink)
Lines 43
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1382953705 news.xs4all.nl 15910 [2001:888:2000:d::a6]:37872
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:57805

Show key headers only | View raw


Op 28-10-13 07:18, Steven D'Aprano schreef:
> On Sun, 27 Oct 2013 20:43:07 -0700, Victor Hooi wrote:
> 
>> Hi,
>>
>> I'd like to double-check something regarding using try-except for
>> controlling flow.
>>
>> I have a script that needs to lookup things in a SQLite database.
>>
>> If the SQLite database file doesn't exist, I'd like to create an empty
>> database, and then setup the schema.
>>
>> Is it acceptable to use try-except in order to achieve this? E.g.:
>>
>>     try:
>>         # Try to open up the SQLite file, and lookup the required
>>         entries
>>     except OSError:
>>         # Open an empty SQLite file, and create the schema
> 
> Yes, that's the right way to do it.
> 
> 
>> My thinking is that it is (easier to ask forgiveness than permission),
>> but I just wanted to check if there is a better way of achieving this?
>>
>> I'd also be doing the same thing for checking if a file is gzipped or
>> not - we try to open it as a gzip, then as an ordinary text file, and if
>> that also fails, raise a parsing error.
> 
> Correct.
> 
> The problem with checking in advance is that there is a race condition 
> between checking and the using the file:

There is also a race condition here. You open the SQLite file and it
fails. Then another process creates the SQLite file and stores things
in it and then when you open the SQLite file as an empty file and create
the schema, the previous work is lost.

-- 
Antoon Pardon

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


Thread

Try-except for flow control in reading Sqlite Victor Hooi <victorhooi@gmail.com> - 2013-10-27 20:43 -0700
  Re: Try-except for flow control in reading Sqlite Steven D'Aprano <steve@pearwood.info> - 2013-10-28 06:18 +0000
    Re: Try-except for flow control in reading Sqlite Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-10-28 10:48 +0100
  Re: Try-except for flow control in reading Sqlite Chris Angelico <rosuav@gmail.com> - 2013-10-28 17:36 +1100
    Re: Try-except for flow control in reading Sqlite Victor Hooi <victorhooi@gmail.com> - 2013-10-27 23:57 -0700
      Re: Try-except for flow control in reading Sqlite Chris Angelico <rosuav@gmail.com> - 2013-10-28 18:01 +1100
        Re: Try-except for flow control in reading Sqlite Steven D'Aprano <steve@pearwood.info> - 2013-10-28 07:19 +0000
      Re: Try-except for flow control in reading Sqlite Chris Angelico <rosuav@gmail.com> - 2013-10-28 18:02 +1100
  Re: Try-except for flow control in reading Sqlite Burak Arslan <burak.arslan@arskom.com.tr> - 2013-10-28 10:17 +0200
  Re: Try-except for flow control in reading Sqlite Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-10-28 19:43 -0400
    Re: Try-except for flow control in reading Sqlite Victor Hooi <victorhooi@gmail.com> - 2013-10-31 16:08 -0700
      Re: Try-except for flow control in reading Sqlite Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-10-31 22:45 -0400

csiph-web