Note that there are some explanatory texts on larger screens.

plurals
  1. POPython idiom for 'Try until no exception is raised'
    text
    copied!<p>I want my code to automatically try multiple ways to create a database connection. As soon as one works, the code needs to move on (i.e. it shouldn't try to other ways anymore). If they all fail well, then the script can just blow up.</p> <p>So in - what I thought was, but most likely isn't - a stroke of genius I tried this:</p> <pre><code>import psycopg2 from getpass import getpass # ouch, global variable, ooh well, it's just a simple script eh CURSOR = None def get_cursor(): """Create database connection and return standard cursor.""" global CURSOR if not CURSOR: # try to connect and get a cursor try: # first try the bog standard way: db postgres, user postgres and local socket conn = psycopg2.connect(database='postgres', user='postgres') except psycopg2.OperationalError: # maybe user pgsql? conn = psycopg2.connect(database='postgres', user='pgsql') except psycopg2.OperationalError: # maybe it was postgres, but on localhost? prolly need password then conn = psycopg2.connect(database='postgres', user='postgres', host='localhost', password=getpass()) except psycopg2.OperationalError: # or maybe it was pgsql and on localhost conn = psycopg2.connect(database='postgres', user='pgsql', host='localhost', password=getpass()) # allright, nothing blew up, so we have a connection # now make a cursor CURSOR = conn.cursor() # return existing or new cursor return CURSOR </code></pre> <p>But it seems that the second and subsequent except statements aren't catching the OperationalErrors anymore. Probably because Python only catches an exception once in a try...except statement?</p> <p>Is that so? If not: is there anything else I'm doing wrong? If so: how do you do something like this then? Is there a standard idiom?</p> <p>(I know there are ways around this problem, like having the user specify the connection parameters on the command line, but that's not my question ok :) )</p> <p><strong>EDIT:</strong></p> <p>I accepted retracile's excellent answer and I took in gnibbler's comment for using the for..else construct. The final code became (sorry, I'm not really following the max characters per line limit from pep8):</p> <p><strong>EDIT 2:</strong> As you can see from the comment on the Cursor class: I don't really know how to call this kind of class. It's not really a singleton (I can have multiple different instances of Cursor) but when calling get_cursor I do get the same cursor object everytime. So it's like a singleton factory? :)</p> <pre><code>import psycopg2 from getpass import getpass import sys class UnableToConnectError(Exception): pass class Cursor: """Cursor singleton factory?""" def __init__(self): self.CURSOR = None def __call__(self): if self.CURSOR is None: # try to connect and get a cursor attempts = [ {'database': 'postgres', 'user': 'postgres'}, {'database': 'postgres', 'user': 'pgsql'}, {'database': 'postgres', 'user': 'postgres', 'host': 'localhost', 'password': None}, {'database': 'postgres', 'user': 'pgsql', 'host': 'localhost', 'password': None}, ] for attempt in attempts: if 'password' in attempt: attempt['password'] = getpass(stream=sys.stderr) # tty and stderr are default in 2.6, but 2.5 uses sys.stdout, which I don't want try: conn = psycopg2.connect(**attempt) attempt.pop('password', None) sys.stderr.write("Succesfully connected using: %s\n\n" % attempt) break # no exception raised, we have a connection, break out of for loop except psycopg2.OperationalError: pass else: raise UnableToConnectError("Unable to connect: exhausted standard permutations of connection dsn.") # allright, nothing blew up, so we have a connection # now make a cursor self.CURSOR = conn.cursor() # return existing or new cursor return self.CURSOR get_cursor = Cursor() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload