python3 work with values of fetchall and DictCursor

This code works with python3 if I use normal cursor (i.e. cur = con.cursor()).

How can I make this code work if I want to use DictCursor? (i.e. cur = con.cursor(mdb.cursors.DictCursor))

include numpy as np
import pymysql as mdb
--- cut ---
cur.execute("select id from history where category=%s;",("DATA"))
rows = cur.fetchall()
num_rows = int(cur.rowcount)
# recast this nested tuple to a python list and flatten it so it's a proper iterable:
x = map(list, list(rows))              # change the type
x = sum(x, [])                            # flatten
D = np.fromiter(iter=x, dtype=float, count=-1)
---

Answers


Well, flattening the IDs could be simplified as:

cur = connection.cursor()

# Query IDs.
cur.execute("SELECT id FROM history WHERE category = %s;", ['DATA'])
rows = cur.fetchall()
num_rows = int(cur.rowcount)

# Create a generator yielding the 0-th column (id) from each row.
x = (row[0] for row in rows)

D = np.fromiter(iter=x, dtype=float, count=-1)

So using a dict cursor should as simple as converting the index lookup to key:

cur = connection.cursor(pymysql.cursors.DictCursor)

# Query IDs.
cur.execute("SELECT id FROM history WHERE category = %s;", ['DATA'])
rows = cur.fetchall()
num_rows = int(cur.rowcount)

# Create a generator yielding the id field from each row.
x = (row['id'] for row in rows)

D = np.fromiter(iter=x, dtype=float, count=-1)

Need Your Help

How do I compile abinit on a Cray XC30?

compilation cross-compiling hpc cray

I am trying to compile the abinit code (http://www.abinit.org/) on a Cray XC30 machine (http://www.archer.ac.uk). Does anyone have a step-by-step procedure for doing this?