From 357622b40fcb604d40c9c686a01db4140c5e2b84 Mon Sep 17 00:00:00 2001 From: Jeremy Howard Date: Tue, 4 Jun 2024 17:12:30 +1000 Subject: [PATCH] getitem --- fastlite/core.py | 2 +- fastlite/kw.py | 4 ++++ nbs/00_core.ipynb | 4 ++-- nbs/index.ipynb | 19 +++++++++++++------ 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/fastlite/core.py b/fastlite/core.py index f5b0c93..33508d4 100644 --- a/fastlite/core.py +++ b/fastlite/core.py @@ -10,7 +10,7 @@ from fastcore.utils import * from fastcore.xml import highlight from fastcore.xtras import hl_md, dataclass_src -from .db import * +from sqlite_utils.db import * try: from graphviz import Source except ImportError: pass diff --git a/fastlite/kw.py b/fastlite/kw.py index 2062a66..c45b9bf 100644 --- a/fastlite/kw.py +++ b/fastlite/kw.py @@ -69,6 +69,10 @@ def get(self:Table, pk_values: list|tuple|str|int, as_cls:bool=True)->Any: if as_cls and hasattr(self,'cls'): row = self.cls(**row) return row +@patch +def __getitem__(self:Table, pk_values): + return self.get(pk_values) + @patch def create( diff --git a/nbs/00_core.ipynb b/nbs/00_core.ipynb index 09b7af4..c34fb9e 100644 --- a/nbs/00_core.ipynb +++ b/nbs/00_core.ipynb @@ -38,7 +38,7 @@ "from fastcore.utils import *\n", "from fastcore.xml import highlight\n", "from fastcore.xtras import hl_md, dataclass_src\n", - "from fastlite.db import *\n", + "from sqlite_utils.db import *\n", "\n", "try: from graphviz import Source\n", "except ImportError: pass" @@ -580,7 +580,7 @@ { "data": { "text/plain": [ - "Artist(ArtistId=1, Name='AC/DC')" + "{'ArtistId': 1, 'Name': 'AC/DC'}" ] }, "execution_count": null, diff --git a/nbs/index.ipynb b/nbs/index.ipynb index 4748a56..66fdb71 100644 --- a/nbs/index.ipynb +++ b/nbs/index.ipynb @@ -437,6 +437,13 @@ "create_mod(db, 'db_dc')" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Indexing into a table does a query on primary key:" + ] + }, { "cell_type": "code", "execution_count": null, @@ -456,7 +463,7 @@ "source": [ "#| eval: false\n", "from db_dc import Track\n", - "Track(**dt.Track.get(1))" + "Track(**dt.Track[1])" ] }, { @@ -520,7 +527,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "`get` also uses the dataclass by default:" + "Indexing also uses the dataclass by default:" ] }, { @@ -540,14 +547,14 @@ } ], "source": [ - "album.get(5)" + "album[5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "If you set `xtra` fields, then `get` is also filtered by those. As a result, for instance in this case, nothing is returned since album 5 is not created by artist 1:" + "If you set `xtra` fields, then indexing is also filtered by those. As a result, for instance in this case, nothing is returned since album 5 is not created by artist 1:" ] }, { @@ -566,7 +573,7 @@ "source": [ "album.xtra(ArtistId=1)\n", "\n", - "try: album.get(5)\n", + "try: album[5]\n", "except NotFoundError: print(\"Not found\")" ] }, @@ -814,7 +821,7 @@ "source": [ "cats.xtra(uid=2)\n", "cats.dataclass()\n", - "cat = cats.get(1)\n", + "cat = cats[1]\n", "cat" ] },