Skip to content

Commit 357622b

Browse files
committed
getitem
1 parent 4cf0208 commit 357622b

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

fastlite/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from fastcore.utils import *
1111
from fastcore.xml import highlight
1212
from fastcore.xtras import hl_md, dataclass_src
13-
from .db import *
13+
from sqlite_utils.db import *
1414

1515
try: from graphviz import Source
1616
except ImportError: pass

fastlite/kw.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ def get(self:Table, pk_values: list|tuple|str|int, as_cls:bool=True)->Any:
6969
if as_cls and hasattr(self,'cls'): row = self.cls(**row)
7070
return row
7171

72+
@patch
73+
def __getitem__(self:Table, pk_values):
74+
return self.get(pk_values)
75+
7276

7377
@patch
7478
def create(

nbs/00_core.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"from fastcore.utils import *\n",
3939
"from fastcore.xml import highlight\n",
4040
"from fastcore.xtras import hl_md, dataclass_src\n",
41-
"from fastlite.db import *\n",
41+
"from sqlite_utils.db import *\n",
4242
"\n",
4343
"try: from graphviz import Source\n",
4444
"except ImportError: pass"
@@ -580,7 +580,7 @@
580580
{
581581
"data": {
582582
"text/plain": [
583-
"Artist(ArtistId=1, Name='AC/DC')"
583+
"{'ArtistId': 1, 'Name': 'AC/DC'}"
584584
]
585585
},
586586
"execution_count": null,

nbs/index.ipynb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,13 @@
437437
"create_mod(db, 'db_dc')"
438438
]
439439
},
440+
{
441+
"cell_type": "markdown",
442+
"metadata": {},
443+
"source": [
444+
"Indexing into a table does a query on primary key:"
445+
]
446+
},
440447
{
441448
"cell_type": "code",
442449
"execution_count": null,
@@ -456,7 +463,7 @@
456463
"source": [
457464
"#| eval: false\n",
458465
"from db_dc import Track\n",
459-
"Track(**dt.Track.get(1))"
466+
"Track(**dt.Track[1])"
460467
]
461468
},
462469
{
@@ -520,7 +527,7 @@
520527
"cell_type": "markdown",
521528
"metadata": {},
522529
"source": [
523-
"`get` also uses the dataclass by default:"
530+
"Indexing also uses the dataclass by default:"
524531
]
525532
},
526533
{
@@ -540,14 +547,14 @@
540547
}
541548
],
542549
"source": [
543-
"album.get(5)"
550+
"album[5]"
544551
]
545552
},
546553
{
547554
"cell_type": "markdown",
548555
"metadata": {},
549556
"source": [
550-
"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:"
557+
"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:"
551558
]
552559
},
553560
{
@@ -566,7 +573,7 @@
566573
"source": [
567574
"album.xtra(ArtistId=1)\n",
568575
"\n",
569-
"try: album.get(5)\n",
576+
"try: album[5]\n",
570577
"except NotFoundError: print(\"Not found\")"
571578
]
572579
},
@@ -814,7 +821,7 @@
814821
"source": [
815822
"cats.xtra(uid=2)\n",
816823
"cats.dataclass()\n",
817-
"cat = cats.get(1)\n",
824+
"cat = cats[1]\n",
818825
"cat"
819826
]
820827
},

0 commit comments

Comments
 (0)