Skip to content

Commit d1618fa

Browse files
committed
fixes #64
1 parent 08b3db7 commit d1618fa

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

fastlite/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
'syms': { 'fastlite.core': { 'fastlite.core.Database.create': ('core.html#database.create', 'fastlite/core.py'),
99
'fastlite.core.Database.get_tables': ('core.html#database.get_tables', 'fastlite/core.py'),
1010
'fastlite.core.Database.import_file': ('core.html#database.import_file', 'fastlite/core.py'),
11+
'fastlite.core.Database.link_dcs': ('core.html#database.link_dcs', 'fastlite/core.py'),
1112
'fastlite.core.Database.q': ('core.html#database.q', 'fastlite/core.py'),
1213
'fastlite.core.Database.set_classes': ('core.html#database.set_classes', 'fastlite/core.py'),
1314
'fastlite.core.Database.t': ('core.html#database.t', 'fastlite/core.py'),

fastlite/core.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from fastcore.xtras import hl_md, dataclass_src
1717
from apswutils.db import *
1818
from apswutils.utils import rows_from_file,TypeTracker,Format
19+
from apswutils.db import NotFoundError
1920
import types
2021

2122
try: from graphviz import Source
@@ -102,9 +103,18 @@ def create_mod(db, mod_fn, with_views=False, store=True, suf=''):
102103
"Create module for dataclasses for `db`"
103104
mod_fn = str(mod_fn)
104105
if not mod_fn.endswith('.py'): mod_fn+='.py'
106+
dcs = all_dcs(db, with_views, store=store, suf=suf)
107+
strlist = ', '.join([f'"{o.__name__}"' for o in dcs])
105108
with open(mod_fn, 'w') as f:
109+
print(f'__all__ = [{strlist}]', file=f)
106110
print('from dataclasses import dataclass', file=f)
107-
for o in all_dcs(db, with_views, store=store, suf=suf): print(dataclass_src(o), file=f)
111+
for o in dcs: print(dataclass_src(o), file=f)
112+
113+
# %% ../nbs/00_core.ipynb
114+
@patch
115+
def link_dcs(self:Database, mod):
116+
"Set the internal dataclass type links for tables using `mod` (created via `create_mod`)"
117+
for o in mod.__all__: self.t[o.lower()].cls = getattr(mod, o)
108118

109119
# %% ../nbs/00_core.ipynb
110120
@patch
@@ -131,7 +141,8 @@ def __call__(
131141
if as_cls and hasattr(self,'cls'):
132142
if with_pk: res = ((k,self.cls(**v)) for k,v in res)
133143
else: res = (self.cls(**o) for o in res)
134-
return next(res) if fetchone else list(res)
144+
try: return next(res) if fetchone else list(res)
145+
except StopIteration: raise NotFoundError from None
135146

136147
# %% ../nbs/00_core.ipynb
137148
@patch

nbs/00_core.ipynb

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"from fastcore.xtras import hl_md, dataclass_src\n",
4343
"from apswutils.db import *\n",
4444
"from apswutils.utils import rows_from_file,TypeTracker,Format\n",
45+
"from apswutils.db import NotFoundError\n",
4546
"import types\n",
4647
"\n",
4748
"try: from graphviz import Source\n",
@@ -444,6 +445,26 @@
444445
"art1_obj"
445446
]
446447
},
448+
{
449+
"cell_type": "code",
450+
"execution_count": null,
451+
"metadata": {},
452+
"outputs": [
453+
{
454+
"data": {
455+
"text/plain": [
456+
"__main__.Artist"
457+
]
458+
},
459+
"execution_count": null,
460+
"metadata": {},
461+
"output_type": "execute_result"
462+
}
463+
],
464+
"source": [
465+
"artist.cls"
466+
]
467+
},
447468
{
448469
"cell_type": "markdown",
449470
"metadata": {},
@@ -504,9 +525,12 @@
504525
" \"Create module for dataclasses for `db`\"\n",
505526
" mod_fn = str(mod_fn)\n",
506527
" if not mod_fn.endswith('.py'): mod_fn+='.py'\n",
528+
" dcs = all_dcs(db, with_views, store=store, suf=suf)\n",
529+
" strlist = ', '.join([f'\"{o.__name__}\"' for o in dcs])\n",
507530
" with open(mod_fn, 'w') as f:\n",
531+
" print(f'__all__ = [{strlist}]', file=f)\n",
508532
" print('from dataclasses import dataclass', file=f)\n",
509-
" for o in all_dcs(db, with_views, store=store, suf=suf): print(dataclass_src(o), file=f)"
533+
" for o in dcs: print(dataclass_src(o), file=f)"
510534
]
511535
},
512536
{
@@ -518,6 +542,19 @@
518542
"create_mod(db, 'db_dc')"
519543
]
520544
},
545+
{
546+
"cell_type": "code",
547+
"execution_count": null,
548+
"metadata": {},
549+
"outputs": [],
550+
"source": [
551+
"#| export\n",
552+
"@patch\n",
553+
"def link_dcs(self:Database, mod):\n",
554+
" \"Set the internal dataclass type links for tables using `mod` (created via `create_mod`)\"\n",
555+
" for o in mod.__all__: self.t[o.lower()].cls = getattr(mod, o)"
556+
]
557+
},
521558
{
522559
"cell_type": "code",
523560
"execution_count": null,
@@ -571,7 +608,8 @@
571608
" if as_cls and hasattr(self,'cls'):\n",
572609
" if with_pk: res = ((k,self.cls(**v)) for k,v in res)\n",
573610
" else: res = (self.cls(**o) for o in res)\n",
574-
" return next(res) if fetchone else list(res)"
611+
" try: return next(res) if fetchone else list(res)\n",
612+
" except StopIteration: raise NotFoundError from None"
575613
]
576614
},
577615
{
@@ -688,6 +726,29 @@
688726
"artist.fetchone('Name=?', ('AC/DC',))"
689727
]
690728
},
729+
{
730+
"cell_type": "code",
731+
"execution_count": null,
732+
"metadata": {},
733+
"outputs": [
734+
{
735+
"ename": "NotFoundError",
736+
"evalue": "",
737+
"output_type": "error",
738+
"traceback": [
739+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
740+
"\u001b[0;31mNotFoundError\u001b[0m Traceback (most recent call last)",
741+
"Cell \u001b[0;32mIn[41], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43martist\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfetchone\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mName=?\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mAC/D\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n",
742+
"Cell \u001b[0;32mIn[39], line 12\u001b[0m, in \u001b[0;36mfetchone\u001b[0;34m(self, where, where_args, select, as_cls, xtra, **kwargs)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;129m@patch\u001b[39m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mfetchone\u001b[39m(\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28mself\u001b[39m:(Table\u001b[38;5;241m|\u001b[39mView),\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 9\u001b[0m xtra:\u001b[38;5;28mdict\u001b[39m\u001b[38;5;241m|\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, \u001b[38;5;66;03m# Extra constraints\u001b[39;00m\n\u001b[1;32m 10\u001b[0m \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m\u001b[38;5;28mlist\u001b[39m:\n\u001b[1;32m 11\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mShortcut for `__call__` that returns one item\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m---> 12\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mwhere\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mwhere\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mwhere_args\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mwhere_args\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mselect\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mselect\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mas_cls\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mas_cls\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mxtra\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mxtra\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfetchone\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n",
743+
"Cell \u001b[0;32mIn[35], line 27\u001b[0m, in \u001b[0;36m__call__\u001b[0;34m(self, where, where_args, order_by, limit, offset, select, with_pk, as_cls, xtra, fetchone, **kwargs)\u001b[0m\n\u001b[1;32m 25\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m: res \u001b[38;5;241m=\u001b[39m (\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcls(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mo) \u001b[38;5;28;01mfor\u001b[39;00m o \u001b[38;5;129;01min\u001b[39;00m res)\n\u001b[1;32m 26\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m: \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mnext\u001b[39m(res) \u001b[38;5;28;01mif\u001b[39;00m fetchone \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28mlist\u001b[39m(res)\n\u001b[0;32m---> 27\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mStopIteration\u001b[39;00m: \u001b[38;5;28;01mraise\u001b[39;00m NotFoundError \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n",
744+
"\u001b[0;31mNotFoundError\u001b[0m: "
745+
]
746+
}
747+
],
748+
"source": [
749+
"artist.fetchone('Name=?', ('AC/D',))"
750+
]
751+
},
691752
{
692753
"cell_type": "code",
693754
"execution_count": null,

0 commit comments

Comments
 (0)