Last active
November 13, 2021 01:21
-
-
Save kissgyorgy/9e519fee57edcac6861164e63286e259 to your computer and use it in GitHub Desktop.
Python3: load multiple SQLite rows into one NamedTuple object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
from typing import namedtuple | |
class Environment(NamedTuple): | |
SLACK_CLIENT_ID: str | |
SLACK_CLIENT_SECRET: str | |
SECRET_KEY: str | |
conn = sqlite3.connect('config.db') | |
conn.row_factory = sqlite3.Row | |
cur = conn.execute('SELECT * FROM environment') | |
res = cur.fetchall() | |
env = Environment(**{r['name']: r['value'] for r in res}) | |
# env == Environment(SLACK_CLIENT_ID='1234.5678', SLACK_CLIENT_SECRET='akshdfjhafhadf', SECRET_KEY='verysecret') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment