A pure python library inspired by the NuGet library dapper.
pydapper is built on top of the dbapi 2.0 spec to provide more convenient methods for working with databases in python, with both sync and async dbapi support.
See the documentation for more details and examples for configuring all of the connectors pydapper supports.
It is recommended to only install the database apis you need for your use case. Example below is for psycopg2!
pip install pydapper[psycopg2]
poetry add pydapper -E psycopg2
The database support docs go into further detail about how to connect to the different drivers pydapper supports.
In addition to psycopg2
, pydapper also supports.
pymssql
mysql-connector-python
oracledb
google-cloud-bigquery
sqlite3
psycopg
aiopg
psycopg
from psycopg2 import connect
@dataclass
class Task:
id: int
description: str
due_date: datetime.date
with connect("postgresql://pydapper:pydapper@localhost/pydapper") as conn:
with conn.cursor() as cursor:
cursor.execute("select id, description, due_date from task")
headers = [i[0] for i in cursor.description]
data = cursor.fetchall()
list_data = [Task(**dict(zip(headers, row))) for row in data]
from dataclasses import dataclass
import datetime
import pydapper
@dataclass
class Task:
id: int
description: str
due_date: datetime.date
with pydapper.connect("postgresql+psycopg2://pydapper:pydapper@locahost/pydapper") as commands:
tasks = commands.query("select id, description, due_date from task;", model=Task)
(This script is complete, it should run "as is")
If you find this project useful, consider buying me a coffee!