Skip to content

Commit

Permalink
DRY & clean up sqlite row_factory (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxr authored Jan 28, 2025
1 parent 7ad3a9c commit b63e46b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
8 changes: 5 additions & 3 deletions sqlite_export_for_ynab/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ async def sync(token: str, db: Path, full_refresh: bool) -> None:
db.parent.mkdir(parents=True, exist_ok=True)

with sqlite3.connect(db) as con:
con.row_factory = lambda c, row: dict(
zip([name for name, *_ in c.description], row, strict=True)
)
con.row_factory = _row_factory
cur = con.cursor()

if full_refresh:
Expand Down Expand Up @@ -177,6 +175,10 @@ async def sync(token: str, db: Path, full_refresh: bool) -> None:
print("Done")


def _row_factory(c: sqlite3.Cursor, row: tuple[Any, ...]) -> dict[str, Any]:
return {d[0]: r for d, r in zip(c.description, row, strict=True)}


def contents(filename: str) -> str:
return (resources.files(ddl) / filename).read_text()

Expand Down
5 changes: 2 additions & 3 deletions testing/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest
from aioresponses import aioresponses

from sqlite_export_for_ynab._main import _row_factory
from sqlite_export_for_ynab._main import contents

BUDGET_ID_1 = str(uuid4())
Expand Down Expand Up @@ -186,11 +187,9 @@
@pytest.fixture
def cur():
with sqlite3.connect(":memory:") as con:
con.row_factory = _row_factory
cursor = con.cursor()
cursor.executescript(contents("create-relations.sql"))
cursor.row_factory = lambda c, row: dict(
zip([name for name, *_ in c.description], row, strict=True)
)
yield cursor


Expand Down

0 comments on commit b63e46b

Please sign in to comment.