Improve normalize_db_row by making it more strict on types.

This commit is contained in:
voussoir 2021-08-26 21:32:44 -07:00
parent 8af64598a2
commit 46be4f8cff
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

View file

@ -4,11 +4,20 @@ from . import constants
from . import exceptions from . import exceptions
from . import ytrss from . import ytrss
def normalize_db_row(db_row, table): def normalize_db_row(db_row, table) -> dict:
if isinstance(db_row, (list, tuple)): '''
db_row = dict(zip(constants.SQL_COLUMNS[table], db_row)) Raises KeyError if table is not one of the recognized tables.
Raises TypeError if db_row is not the right type.
'''
if isinstance(db_row, dict):
return db_row return db_row
if isinstance(db_row, (list, tuple)):
return dict(zip(constants.SQL_COLUMNS[table], db_row))
raise TypeError(f'db_row should be {dict}, {list}, or {tuple}, not {type(db_row)}.')
class Base: class Base:
def __init__(self, ycdldb): def __init__(self, ycdldb):
super().__init__() super().__init__()