add a function to convert a tuple into a dictionary

This commit is contained in:
Camryn Thomas 2024-01-14 15:25:45 -06:00
parent cfa45fc970
commit 941ebc4a24
Signed by: cptlobster
GPG Key ID: 6D341D688163A176

@ -66,6 +66,19 @@ class DBConnector:
finally:
return result
def _read_dict(self) -> list[dict]:
"""Read the cursor as a list of dictionaries. psycopg2 defaults to using a list of tuples, so we want to convert
each row into a dictionary before we return it."""
cols = [i.name for i in self.cur.description]
results = []
for row in self.cur:
row_dict = {}
for i in range(0, len(row)):
if row[i]:
row_dict = {**row_dict, cols[i]: row[i]}
results.append(row_dict)
return results
def read(self, **kwargs) -> list[dict]:
"""Read rows from a database that match the specified filters.