Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python idiom for 'Try until no exception is raised'

Tags:

python

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.

So in - what I thought was, but most likely isn't - a stroke of genius I tried this:

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

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?

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?

(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 :) )

EDIT:

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):

EDIT 2: 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? :)

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()
like image 603
hopla Avatar asked Oct 21 '09 20:10

hopla


1 Answers

Approximately:

attempts = [
    { 'database'='postgres', 'user'='pgsql', ...},
    { 'database'='postgres', 'user'='postgres', 'host'='localhost', 'password'=getpass()},
    ...
]
conn = None
for attempt in attempts:
    try:
        conn = psycopg2.connect(**attempt)
        break
    except psycopg2.OperationalError:
        pass
if conn is None:
    raise a ruckus
CURSOR = conn.cursor()

Now, if you don't want to call getpass() unless it is necessary, you'd want to check if 'password' in attempt: attempt['password'] = getpass() or so.

Now about that global....

class MyCursor:
    def __init__(self):
        self.CURSOR = None
    def __call__(self):
        if self.CURSOR is None:
            <insert logic here>
        return self.CURSOR

get_cursor = MyCursor()

... though I think there are a couple of other ways to accomplish the same thing.

Bringing it all together:

class MyCursor:
    def __init__(self):
        self.CURSOR = None
    def __call__(self):
        if self.CURSOR is None:
            attempts = [
                {'database'='postgres', 'user'='postgres'},
                {'database'='postgres', 'user'='pgsql'},
                {'database'='postgres', 'user'='postgres', 'host'='localhost', 'password'=True},
                {'database'='postgres', 'user'='pgsql', 'host'='localhost', 'password'=True},
            ]
            conn = None
            for attempt in attempts:
                if 'password' in attempt:
                    attempt['password'] = getpass()
                try:
                    conn = psycopg2.connect(**attempt)
                    break # that didn't throw an exception, we're done
                except psycopg2.OperationalError:
                    pass
            if conn is None:
                raise a ruckus # nothin' worked
            self.CURSOR = conn.cursor()
        return self.CURSOR
get_cursor = MyCursor()

Note: completely untested

like image 69
retracile Avatar answered Sep 25 '22 10:09

retracile