diff options
author | Arun Isaac | 2022-09-28 18:39:54 +0530 |
---|---|---|
committer | Arun Isaac | 2022-09-29 16:15:47 +0530 |
commit | b23c36a8c7b66296cf972ac7b3dedfe8d427690c (patch) | |
tree | cd81f1621248bd4c2dea45a2ef5ca73a3b713595 /wqflask/utility | |
parent | 38a87ff73c64f5e6bbfb6d1aefdad8d6e06a1d46 (diff) | |
download | genenetwork2-b23c36a8c7b66296cf972ac7b3dedfe8d427690c.tar.gz |
Replace MonadicDictCursor with generator function.
* wqflask/utility/monads.py (MonadicDictCursor): Delete class.
(sql_query_mdict): New function.
* wqflask/scripts/index.py: Import sql_query_mdict instead of
MonadicDictCursor.
(main): Use sql_query_mdict.
Diffstat (limited to 'wqflask/utility')
-rw-r--r-- | wqflask/utility/monads.py | 26 |
1 files changed, 7 insertions, 19 deletions
diff --git a/wqflask/utility/monads.py b/wqflask/utility/monads.py index 3360fad2..2d708261 100644 --- a/wqflask/utility/monads.py +++ b/wqflask/utility/monads.py @@ -105,22 +105,10 @@ class MonadicDict(UserDict): except KeyError: pass -class MonadicDictCursor(DictCursor): - """Monadic version of MySQLdb.cursors.DictCursor. - - Monadic version of MySQLdb.cursors.DictCursor that returns a - MonadicDict instead of the built-in dictionary. - - Execute a SQL query and retrieve results as MonadicDict - objects. Each row object in the following code is a MonadicDict. - >>> with conn.cursor(MonadicDictCursor) as cursor: - ... cursor.execute("SELECT foo FROM bar") - ... for row in cursor.fetchall(): - ... print(row) - """ - def fetchone(self): - return Just(MonadicDict(row)) if (row := super().fetchone()) else Nothing - def fetchmany(self, size=None): - return [MonadicDict(row) for row in super().fetchmany(size=size)] - def fetchall(self): - return [MonadicDict(row) for row in super().fetchall()] + +def sql_query_mdict(conn, query): + """Execute SQL query and return a generator of MonadicDict objects.""" + with conn.cursor(DictCursor) as cursor: + cursor.execute(query) + while (row := cursor.fetchone()): + yield MonadicDict(row) |