Skip to content

Commit

Permalink
try sqlalchemy
Browse files Browse the repository at this point in the history
  • Loading branch information
bollwyvl committed Aug 21, 2024
1 parent 8addc9c commit b3fe376
Show file tree
Hide file tree
Showing 4 changed files with 277 additions and 0 deletions.
175 changes: 175 additions & 0 deletions examples/files/04_sqlalchemy.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "10273bf9-9c77-4ef8-aee4-8d72bd358232",
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"\n",
"if __name__ == \"__main__\" and \"pyodide\" in sys.modules:\n",
" %pip install -r requirements.txt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6af7fc9a-051a-4830-9a5b-3c26f26b5de8",
"metadata": {},
"outputs": [],
"source": [
"from ipyprofiler import Pyinstrument\n",
"\n",
"ps = Pyinstrument()\n",
"ps.tabs(layout={\"min_height\": \"60vh\"})"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1101695d-7a9c-49eb-b66f-89c27a8792bd",
"metadata": {},
"outputs": [],
"source": [
"with ps.profile(\"just importing\", interval=0.001):\n",
" import sqlalchemy"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8070e5c5-5448-49f8-9a8b-c607a1c8194d",
"metadata": {},
"outputs": [],
"source": [
"from typing import List\n",
"from typing import Optional\n",
"from sqlalchemy import ForeignKey\n",
"from sqlalchemy import String, text\n",
"from sqlalchemy.orm import DeclarativeBase\n",
"from sqlalchemy.orm import Mapped\n",
"from sqlalchemy.orm import mapped_column\n",
"from sqlalchemy.orm import relationship\n",
"from sqlalchemy.orm import Session"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dc4b136f-e0af-4ee7-a767-388fab24ad6a",
"metadata": {},
"outputs": [],
"source": [
"from sqlalchemy import create_engine"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c4e9fbd-3572-49f0-94ae-6f6f80a054e7",
"metadata": {},
"outputs": [],
"source": [
"class Base(DeclarativeBase):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bc29a302-d87b-4660-85b8-8bccf5434394",
"metadata": {},
"outputs": [],
"source": [
"class User(Base):\n",
" __tablename__ = \"user_account\"\n",
" id: Mapped[int] = mapped_column(primary_key=True)\n",
" name: Mapped[str] = mapped_column(String(30))\n",
" fullname: Mapped[Optional[str]]\n",
" addresses: Mapped[List[\"Address\"]] = relationship(\n",
" back_populates=\"user\", cascade=\"all, delete-orphan\"\n",
" )\n",
" def __repr__(self) -> str:\n",
" return f\"User(id={self.id!r}, name={self.name!r}, fullname={self.fullname!r})\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3af7dc12-696c-4fba-97ce-cf4a236dc572",
"metadata": {},
"outputs": [],
"source": [
"class Address(Base):\n",
" __tablename__ = \"address\"\n",
" id: Mapped[int] = mapped_column(primary_key=True)\n",
" email_address: Mapped[str]\n",
" user_id: Mapped[int] = mapped_column(ForeignKey(\"user_account.id\"))\n",
" user: Mapped[\"User\"] = relationship(back_populates=\"addresses\")\n",
" def __repr__(self) -> str:\n",
" return f\"Address(id={self.id!r}, email_address={self.email_address!r})\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7673897f-742c-4e90-984e-0fe128ded456",
"metadata": {},
"outputs": [],
"source": [
"engine = create_engine(\"sqlite://\", echo=True)\n",
"Base.metadata.create_all(engine)\n",
"\n",
"with ps.profile(\"look, ma, an engine\", interval=0.001):\n",
" with Session(engine) as session:\n",
" spongebob = User(\n",
" name=\"spongebob\",\n",
" fullname=\"Spongebob Squarepants\",\n",
" addresses=[Address(email_address=\"[email protected]\")],\n",
" )\n",
" sandy = User(\n",
" name=\"sandy\",\n",
" fullname=\"Sandy Cheeks\",\n",
" addresses=[\n",
" Address(email_address=\"[email protected]\"),\n",
" Address(email_address=\"[email protected]\"),\n",
" ],\n",
" )\n",
" patrick = User(name=\"patrick\", fullname=\"Patrick Star\")\n",
" session.add_all([spongebob, sandy, patrick])\n",
" session.commit()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cf7eb2d5-68c4-4307-af1d-d552c5cc41c2",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
1 change: 1 addition & 0 deletions examples/files/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ipywidgets
ipyprofiler[pyinstrument]
sqlalchemy
Loading

0 comments on commit b3fe376

Please sign in to comment.