2024-04-13 21:59:43 +00:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "6f6fbc7e-4fb9-4353-b2ee-9ea819a3c896",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/smgr/.pyenv/versions/co4/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"import vegetables"
]
},
{
"cell_type": "code",
2024-04-18 01:42:26 +00:00
"execution_count": 2,
2024-04-13 21:59:43 +00:00
"id": "88fd0ea8-9c94-4569-a51b-823a04f32f55",
"metadata": {},
2024-04-18 01:42:26 +00:00
"outputs": [
{
"data": {
"text/plain": [
"{'age': 5}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
2024-04-13 21:59:43 +00:00
"source": [
"tomato = vegetables.Tomato('t1', 5)\n",
"\n",
"# test a register collation action\n",
"tomato.collate('ripe')"
]
2024-04-18 01:42:26 +00:00
},
{
"cell_type": "code",
"execution_count": 3,
"id": "348926d9-7137-4eff-a919-508788553dd2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['9ca6772e-6621-4511-a4a6-ad451a1da91f',\n",
" '2a91b423-4e08-491c-b1d2-5ec25259191e',\n",
" '4a9edb2b-4ac5-467e-82ef-b254829ac2a2']"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vegetables.vegetable_mapper.collect(tomato, ['ripe'])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "4e5e7319-11bf-4051-951b-08c84e9f3874",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Component (SQLTable)> vegetable+tomato+tomato_aging_states+tomato_cooking_states"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vegetables.vegetable_mapper.compose(tomato, action_groups=['aging', 'cooking'])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "aa290686-8074-4038-a3cc-ce6817844653",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Column('id', Integer(), table=<vegetable>, primary_key=True, nullable=False),\n",
" Column('name', String(), table=<vegetable>),\n",
" Column('color', String(), table=<vegetable>),\n",
" Column('id', Integer(), table=<tomato>, primary_key=True, nullable=False),\n",
" Column('name', String(), ForeignKey('vegetable.name'), table=<tomato>),\n",
" Column('radius', Integer(), table=<tomato>),\n",
" Column('id', Integer(), table=<tomato_aging_states>, primary_key=True, nullable=False),\n",
" Column('name', String(), ForeignKey('tomato.name'), table=<tomato_aging_states>),\n",
" Column('state', String(), table=<tomato_aging_states>),\n",
" Column('age', Integer(), table=<tomato_aging_states>)]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(vegetables.vegetable_mapper.compose(tomato, action_groups=['aging']).obj.columns)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "f3c7e37d-ba9e-4bae-ae44-adc922bf5f4c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(vegetables.Tomato, vegetables.Vegetable, co3.co3.CO3, object)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tomato.__class__.__mro__"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "c21d2c54-39e2-4de3-93bc-763896ed348e",
"metadata": {},
"outputs": [],
"source": [
"from co3.databases import SQLDatabase\n",
"\n",
"db = SQLDatabase('sqlite://') #, echo=True)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "a785d202-99d3-4ae7-859e-ee22b481f8df",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"db.recreate(vegetables.vegetable_schema)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "cda01cb0-1666-4cb1-aa64-bcdca871aff5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{<Component (SQLTable)> vegetable: [{'name': 't1', 'color': 'red'}],\n",
" <Component (SQLTable)> tomato: [{'name': 't1', 'radius': 5}],\n",
" <Component (SQLTable)> tomato_aging_states: [{'name': 't1',\n",
" 'state': 'ripe',\n",
" 'age': 2}]}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vegetables.vegetable_mapper.collector.inserts"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "af7124ed-3031-4f28-89a6-553eb5b3cc7a",
"metadata": {},
"outputs": [],
"source": [
"with db.engine.connect() as connection:\n",
" db.manager.insert_many(\n",
" connection, \n",
" vegetables.vegetable_mapper.collector.inserts,\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "0149e14e-5d07-42af-847d-af5c190f8946",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'id': 1, 'name': 't1', 'radius': 5}]\n"
]
}
],
"source": [
"with db.engine.connect() as connection:\n",
" print(db.accessor.select(\n",
" connection, \n",
" vegetables.vegetable_schema.get_component('tomato')\n",
" ))"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "668d1b8c-b47f-4a58-914d-e43402443fe6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'id': 1, 'name': 't1', 'color': 'red', 'id_1': 1, 'name_1': 't1', 'radius': 5}]\n"
]
}
],
"source": [
"agg_table = vegetables.vegetable_mapper.compose(tomato)\n",
"\n",
"with db.engine.connect() as connection:\n",
" agg_res = db.accessor.select(\n",
" connection, \n",
" agg_table,\n",
" mappings=True,\n",
" )\n",
"\n",
"print(agg_res)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "a051d72d-a867-46dc-bb5e-69341f39a056",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'id': 1, 'name': 't1', 'color': 'red', 'id_1': 1, 'name_1': 't1', 'radius': 5, 'id_2': 1, 'name_2': 't1', 'state': 'ripe', 'age': 2}]\n"
]
}
],
"source": [
"agg_table = vegetables.vegetable_mapper.compose(tomato, action_groups=['aging'])#, outer=True)\n",
"\n",
"with db.engine.connect() as connection:\n",
" agg_res = db.accessor.select(\n",
" connection, \n",
" agg_table,\n",
" mappings=True,\n",
" )\n",
"\n",
"print(agg_res)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "6a80cfd7-3175-4526-96e0-374765d64a27",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"sqlalchemy.engine.row.RowMapping"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(agg_res[0])"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "7cf05ddd-2328-4051-9cf8-4ac01352405e",
"metadata": {},
"outputs": [],
"source": [
"import sqlalchemy as sa\n",
"from co3.engines import SQLEngine\n",
"\n",
"a = SQLEngine.execute(db.engine.connect(), sa.select(agg_table.obj))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "c1edf68e-1fde-4a1f-8ec3-084713a8da45",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.mappings().all()\n"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "8b8a9e47-7f5f-4828-a99e-5d9a12697f46",
"metadata": {},
"outputs": [],
"source": [
"tomato2 = vegetables.Tomato('t2', 8)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "062aa4de-7aea-4fd3-b5db-82af147d023e",
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'Tomato' object has no attribute 'action_map'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[38], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mvegetables\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvegetable_mapper\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcollect\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtomato2\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/Documents/projects/ontolog/co3/build/__editable__.co3-0.1.1-py3-none-any/co3/mapper.py:198\u001b[0m, in \u001b[0;36mMapper.collect\u001b[0;34m(self, obj, action_keys, action_groups)\u001b[0m\n\u001b[1;32m 179\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m'''\u001b[39;00m\n\u001b[1;32m 180\u001b[0m \u001b[38;5;124;03mStages inserts up the inheritance chain, and down through components.\u001b[39;00m\n\u001b[1;32m 181\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 195\u001b[0m \u001b[38;5;124;03mReturns: dict with keys and values relevant for associated SQLite tables\u001b[39;00m\n\u001b[1;32m 196\u001b[0m \u001b[38;5;124;03m'''\u001b[39;00m\n\u001b[1;32m 197\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m action_keys \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m--> 198\u001b[0m action_keys \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlist\u001b[39m(\u001b[43mobj\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43maction_map\u001b[49m\u001b[38;5;241m.\u001b[39mkeys())\n\u001b[1;32m 200\u001b[0m receipts \u001b[38;5;241m=\u001b[39m []\n\u001b[1;32m 201\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m _cls \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mreversed\u001b[39m(obj\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__mro__\u001b[39m[:\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m2\u001b[39m]):\n",
"\u001b[0;31mAttributeError\u001b[0m: 'Tomato' object has no attribute 'action_map'"
]
}
],
"source": [
"vegetables.vegetable_mapper.collect(tomato2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4673ddc8-3f76-4d8c-8186-bbed4a682e0d",
"metadata": {},
"outputs": [],
"source": [
"db.insert(vegetables.vegetable_schema.get_component('tomato'), "
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "9314be4e-c1d5-4af8-ad23-0b208d24b3eb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'id': 1, 'name': 't1', 'radius': 5}]"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"db.select(vegetables.vegetable_schema.get_component('tomato'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a2efd060-f298-4ca6-8a58-7ed5acf1dd15",
"metadata": {},
"outputs": [],
"source": []
2024-04-13 21:59:43 +00:00
}
],
"metadata": {
"kernelspec": {
"display_name": "co3",
"language": "python",
"name": "co3"
},
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}