views:

111

answers:

1

Is there any way to get a backend-neutral dictionary cursor in Django? This would be a cursor that is a dict rather than a tuple. I am forced to use Oracle for the school project I'm working on.

in Python's MySQLDb module it's called a DictCursor.

With WoLpH's inspiring suggestion I know I am very close..

def dict_cursor(cursor):
    for row in cursor:
        yield dict(zip(cursor.description, row))

Iterating and printing each row cursor used to result in:

(482072, 602592, 1)
(656680, 820855, 2)
(574968, 718712, 4)
(557532, 696918, 3))

But with dict_cursor I get:

{('NET_SPENT', <type 'cx_Oracle.NUMBER'>, 127, 22, 0, 0, 1): 482072, ('LOT', <type 'cx_Oracle.NUMBER'>, 12, 22, 11, 0, 0): 1, ('NET_COLLECTED', <type 'cx_Oracle.NUMBER'>, 127, 22, 0, 0, 1): 602592}
{('NET_SPENT', <type 'cx_Oracle.NUMBER'>, 127, 22, 0, 0, 1): 656680, ('LOT', <type 'cx_Oracle.NUMBER'>, 12, 22, 11, 0, 0): 2, ('NET_COLLECTED', <type 'cx_Oracle.NUMBER'>, 127, 22, 0, 0, 1): 820855}
{('NET_SPENT', <type 'cx_Oracle.NUMBER'>, 127, 22, 0, 0, 1): 574968, ('LOT', <type 'cx_Oracle.NUMBER'>, 12, 22, 11, 0, 0): 4, ('NET_COLLECTED', <type 'cx_Oracle.NUMBER'>, 127, 22, 0, 0, 1): 718712}
{('NET_SPENT', <type 'cx_Oracle.NUMBER'>, 127, 22, 0, 0, 1): 557532, ('LOT', <type 'cx_Oracle.NUMBER'>, 12, 22, 11, 0, 0): 3, ('NET_COLLECTED', <type 'cx_Oracle.NUMBER'>, 127, 22, 0, 0, 1): 696918}

I only want it to use the key, e.g. 'NET SPENT'.

After refining it a little more, this seems to work:

def dict_cursor(cursor):
    for row in cursor:
        out = {}
        for i,col in enumerate(cursor.description):
            out[col[0]] = row[i]
        yield out

-

{'NET_COLLECTED': 602592, 'NET_SPENT': 482072, 'LOT': 1}
{'NET_COLLECTED': 820855, 'NET_SPENT': 656680, 'LOT': 2}
{'NET_COLLECTED': 718712, 'NET_SPENT': 574968, 'LOT': 4}
{'NET_COLLECTED': 696918, 'NET_SPENT': 557532, 'LOT': 3}
+2  A: 

You could write it in a couple of lines :)

def dict_cursor(cursor):
    description = [x[0] for x in cursor.description]
    for row in cursor:
        yield dict(zip(description, row))

Or if you really want to save space:

simplify_description = lambda cursor: [x[0] for x in cursor.description]
dict_cursor = lambda c, d: dict(zip(d, r) for r in c))
WoLpH
That one-liner won't work b/c you've lost your yield statement, and I don't see how you can yield in one line since yield can't be part of a list..
pyrony
You're completely right, I've modified it :)
WoLpH
When doing this with columns like date it returns something like datetime.date(2010,10,21) rather than returning an actual date object. I'm trying to return a custom SQL result set as JSON to my browser.
mikec
@Mikec: a `datetime.date` object _is_ an actual date object. The problem is, the `json` standard does not define a date object so you'll have to convert it to a string yourself. You can do that either in the query or you can modify your json encoder to do it for you.
WoLpH