Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fastlite/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@
'fastlite.core.create_mod': ('core.html#create_mod', 'fastlite/core.py'),
'fastlite.core.diagram': ('core.html#diagram', 'fastlite/core.py'),
'fastlite.core.get_typ': ('core.html#get_typ', 'fastlite/core.py')},
'fastlite.db_dc': {},
'fastlite.kw': {}}}
284 changes: 284 additions & 0 deletions nbs/introspection.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "6a85290f",
"metadata": {},
"source": [
"Using introspection to dynamically create table classes and objects "
]
},
{
"cell_type": "markdown",
"id": "730e3ca1",
"metadata": {},
"source": [
"## Setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6329d113",
"metadata": {},
"outputs": [],
"source": [
"from fastlite import *\n",
"from fastcore.all import *"
]
},
{
"cell_type": "markdown",
"id": "3163c66c",
"metadata": {},
"source": [
"Assign a path for the database."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "754fcd24",
"metadata": {},
"outputs": [],
"source": [
"datapath = Path('farm.db')"
]
},
{
"cell_type": "markdown",
"id": "56cd4208",
"metadata": {},
"source": [
"First let's create a database and assign some tables to it"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bcc849fb",
"metadata": {},
"outputs": [],
"source": [
"farm_db = database(datapath)\n",
"class Chicken: id: int; breed: str; age: int; \n",
"class Dog: id: int; name: str; age: int;\n",
"chickens = farm_db.create(Chicken, pk='id')\n",
"dogs = farm_db.create(Dog, pk='id')"
]
},
{
"cell_type": "markdown",
"id": "59237a6c",
"metadata": {},
"source": [
"We'll insert records into both tables."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "38447a5a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Chicken(id=3, breed='Alai', age=2)"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chickens.insert(dict(breed='Alai', age=2))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "09f884c3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Dog(id=3, name='Rover', age=3)"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dogs.insert(dict(name='Rover', age=3))"
]
},
{
"cell_type": "markdown",
"id": "1fdc3674",
"metadata": {},
"source": [
"Now we'll remove everything we just created from the global namespace. This way we can load the database from scratch and be forced to use introspection."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "480370d1",
"metadata": {},
"outputs": [],
"source": [
"del farm_db\n",
"del Chicken\n",
"del Dog\n",
"del chickens\n",
"del dogs"
]
},
{
"cell_type": "markdown",
"id": "c889e7d3",
"metadata": {},
"source": [
"## Testing introspection"
]
},
{
"cell_type": "markdown",
"id": "f1aba07d",
"metadata": {},
"source": [
"We'll use instrospection to load the tables into the global namespace. We use a different name then before to be more certain we aren't accidentally using the old instance."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c2bbbf40",
"metadata": {},
"outputs": [],
"source": [
"db = database(datapath)\n",
"\n",
"g = globals()\n",
"for o in dir(db.t):\n",
" g[o+'s'] = t = getattr(db.t, o)\n",
" g[o.title()] = getattr(t, 'dataclass')()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6b9f60b5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"chicken, dog"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"db.t"
]
},
{
"cell_type": "markdown",
"id": "e3e41020",
"metadata": {},
"source": [
"Do our animals exist?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b65be98",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Chicken(id=1, breed='Alai', age=2),\n",
" Chicken(id=2, breed='Alai', age=2),\n",
" Chicken(id=3, breed='Alai', age=2)]"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chickens()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fde23bca",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Dog(id=1, name='Rover', age=3),\n",
" Dog(id=2, name='Rover', age=3),\n",
" Dog(id=3, name='Rover', age=3)]"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dogs()"
]
},
{
"cell_type": "markdown",
"id": "a3a79661",
"metadata": {},
"source": [
"## Cleanup"
]
},
{
"cell_type": "markdown",
"id": "d84b5bce",
"metadata": {},
"source": [
"We remove the database so each time the test is run clean."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7cc9973f",
"metadata": {},
"outputs": [],
"source": [
"if datapath.exists(): datapath.delete()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "python3",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}