|
42 | 42 | "from fastcore.xtras import hl_md, dataclass_src\n",
|
43 | 43 | "from apswutils.db import *\n",
|
44 | 44 | "from apswutils.utils import rows_from_file,TypeTracker,Format\n",
|
| 45 | + "from apswutils.db import NotFoundError\n", |
45 | 46 | "import types\n",
|
46 | 47 | "\n",
|
47 | 48 | "try: from graphviz import Source\n",
|
|
444 | 445 | "art1_obj"
|
445 | 446 | ]
|
446 | 447 | },
|
| 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 | + }, |
447 | 468 | {
|
448 | 469 | "cell_type": "markdown",
|
449 | 470 | "metadata": {},
|
|
504 | 525 | " \"Create module for dataclasses for `db`\"\n",
|
505 | 526 | " mod_fn = str(mod_fn)\n",
|
506 | 527 | " 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", |
507 | 530 | " with open(mod_fn, 'w') as f:\n",
|
| 531 | + " print(f'__all__ = [{strlist}]', file=f)\n", |
508 | 532 | " 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)" |
510 | 534 | ]
|
511 | 535 | },
|
512 | 536 | {
|
|
518 | 542 | "create_mod(db, 'db_dc')"
|
519 | 543 | ]
|
520 | 544 | },
|
| 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 | + }, |
521 | 558 | {
|
522 | 559 | "cell_type": "code",
|
523 | 560 | "execution_count": null,
|
|
571 | 608 | " if as_cls and hasattr(self,'cls'):\n",
|
572 | 609 | " if with_pk: res = ((k,self.cls(**v)) for k,v in res)\n",
|
573 | 610 | " 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" |
575 | 613 | ]
|
576 | 614 | },
|
577 | 615 | {
|
|
688 | 726 | "artist.fetchone('Name=?', ('AC/DC',))"
|
689 | 727 | ]
|
690 | 728 | },
|
| 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 | + }, |
691 | 752 | {
|
692 | 753 | "cell_type": "code",
|
693 | 754 | "execution_count": null,
|
|
0 commit comments