Skip to content

Commit

Permalink
getitem
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Jun 4, 2024
1 parent 4cf0208 commit 357622b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion fastlite/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions fastlite/kw.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions nbs/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -580,7 +580,7 @@
{
"data": {
"text/plain": [
"Artist(ArtistId=1, Name='AC/DC')"
"{'ArtistId': 1, 'Name': 'AC/DC'}"
]
},
"execution_count": null,
Expand Down
19 changes: 13 additions & 6 deletions nbs/index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -456,7 +463,7 @@
"source": [
"#| eval: false\n",
"from db_dc import Track\n",
"Track(**dt.Track.get(1))"
"Track(**dt.Track[1])"
]
},
{
Expand Down Expand Up @@ -520,7 +527,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"`get` also uses the dataclass by default:"
"Indexing also uses the dataclass by default:"
]
},
{
Expand All @@ -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:"
]
},
{
Expand All @@ -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\")"
]
},
Expand Down Expand Up @@ -814,7 +821,7 @@
"source": [
"cats.xtra(uid=2)\n",
"cats.dataclass()\n",
"cat = cats.get(1)\n",
"cat = cats[1]\n",
"cat"
]
},
Expand Down

0 comments on commit 357622b

Please sign in to comment.