Skip to content

Commit

Permalink
Document transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
pydanny committed Aug 23, 2024
1 parent 12fec43 commit 01b48e0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
4 changes: 2 additions & 2 deletions fastlite/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def create(
res.cls = cls
return res

# %% ../nbs/00_core.ipynb 55
# %% ../nbs/00_core.ipynb 58
def _edge(tbl):
return "\n".join(f"{fk.table}:{fk.column} -> {fk.other_table}:{fk.other_column};"
for fk in tbl.foreign_keys)
Expand All @@ -184,7 +184,7 @@ def _tnode(tbl):
</table>"""
return f"{tbl.name} [label=<{res}>];\n"

# %% ../nbs/00_core.ipynb 56
# %% ../nbs/00_core.ipynb 59
def diagram(tbls, ratio=0.7, size="10", neato=False, render=True):
layout = "\nlayout=neato;\noverlap=prism;\noverlap_scaling=0.5;""" if neato else ""
edges = "\n".join(map(_edge, tbls))
Expand Down
65 changes: 51 additions & 14 deletions nbs/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: graphviz in /Users/daniel.roy.greenfeld/.virtualenvs/fasthtml/lib/python3.10/site-packages (0.20.3)\n",
"\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.0\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.2\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"outputs": [],
"source": [
"#| export\n",
"from dataclasses import dataclass, field, make_dataclass, fields, Field, is_dataclass, MISSING\n",
Expand Down Expand Up @@ -824,6 +812,55 @@
"Cat(1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To transform a table after creation, use the `.create()` method again, this time with the `transform` keyword set to `True`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Table cat (id, name, age, city)>"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class Cat: id: int; name: str; age: int; city: str = \"Unknown\"; breed: str = \"Unknown\"\n",
"cats = db.create(Cat, transform=True)\n",
"cats"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Cat(id=1, name=UNSET, age=UNSET, city='Unknown', breed='Unknown')"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Cat(1)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -952,7 +989,7 @@
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"<!-- Generated by graphviz version 11.0.0 (20240428.1522)\n",
"<!-- Generated by graphviz version 12.0.0 (20240704.0754)\n",
" -->\n",
"<!-- Title: G Pages: 1 -->\n",
"<svg width=\"720pt\" height=\"505pt\"\n",
Expand Down

0 comments on commit 01b48e0

Please sign in to comment.