Skip to content

Commit

Permalink
fixes #24
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Sep 6, 2024
1 parent 50d3b64 commit 9810af7
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 6 deletions.
1 change: 1 addition & 0 deletions fastlite/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'git_url': 'https://github.com/AnswerDotAI/fastlite',
'lib_path': 'fastlite'},
'syms': { 'fastlite.core': { 'fastlite.core.Database.create': ('core.html#database.create', 'fastlite/core.py'),
'fastlite.core.Database.import_file': ('core.html#database.import_file', 'fastlite/core.py'),
'fastlite.core.Database.q': ('core.html#database.q', 'fastlite/core.py'),
'fastlite.core.Database.t': ('core.html#database.t', 'fastlite/core.py'),
'fastlite.core.Database.v': ('core.html#database.v', 'fastlite/core.py'),
Expand Down
20 changes: 18 additions & 2 deletions fastlite/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from fastcore.xml import highlight
from fastcore.xtras import hl_md, dataclass_src
from sqlite_minutils.db import *
from sqlite_minutils.utils import rows_from_file,TypeTracker,Format
import types

try: from graphviz import Source
Expand Down Expand Up @@ -168,7 +169,22 @@ def create(
res.cls = cls
return res

# %% ../nbs/00_core.ipynb 58
# %% ../nbs/00_core.ipynb 55
@patch
def import_file(self:Database, table_name, file, format=None, pk=None):
"Import path or handle `file` to new table `table_name`"
if isinstance(file, str): file = file.encode()
if isinstance(file, bytes): file = io.BytesIO(file)
with maybe_open(file) as fp: rows, format_used = rows_from_file(fp, format=format)
tracker = TypeTracker()
rows = tracker.wrap(rows)
tbl = self[table_name]
tbl.insert_all(rows, alter=True)
tbl.transform(types=tracker.types)
if pk: tbl.transform(pk=pk)
return tbl

# %% ../nbs/00_core.ipynb 61
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 @@ -186,7 +202,7 @@ def _tnode(tbl):
</table>"""
return f"{tbl.name} [label=<{res}>];\n"

# %% ../nbs/00_core.ipynb 59
# %% ../nbs/00_core.ipynb 62
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
64 changes: 60 additions & 4 deletions nbs/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"from fastcore.xml import highlight\n",
"from fastcore.xtras import hl_md, dataclass_src\n",
"from sqlite_minutils.db import *\n",
"from sqlite_minutils.utils import rows_from_file,TypeTracker,Format\n",
"import types\n",
"\n",
"try: from graphviz import Source\n",
Expand Down Expand Up @@ -750,7 +751,7 @@
"metadata": {},
"outputs": [],
"source": [
"#| exports\n",
"#| export\n",
"@patch\n",
"def create(\n",
" self: Database,\n",
Expand Down Expand Up @@ -827,7 +828,7 @@
{
"data": {
"text/plain": [
"<Table cat (id, name, age, city)>"
"<Table cat (id, name, age, city, breed)>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -870,11 +871,12 @@
"name": "stdout",
"output_type": "stream",
"text": [
"CREATE TABLE [cat] (\n",
"CREATE TABLE \"cat\" (\n",
" [id] INTEGER PRIMARY KEY,\n",
" [name] TEXT,\n",
" [age] INTEGER,\n",
" [city] TEXT\n",
" [city] TEXT,\n",
" [breed] TEXT\n",
")\n"
]
}
Expand All @@ -892,6 +894,60 @@
"db.t.cat.drop()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"@patch\n",
"def import_file(self:Database, table_name, file, format=None, pk=None):\n",
" \"Import path or handle `file` to new table `table_name`\"\n",
" if isinstance(file, str): file = file.encode()\n",
" if isinstance(file, bytes): file = io.BytesIO(file)\n",
" with maybe_open(file) as fp: rows, format_used = rows_from_file(fp, format=format)\n",
" tracker = TypeTracker()\n",
" rows = tracker.wrap(rows)\n",
" tbl = self[table_name]\n",
" tbl.insert_all(rows, alter=True)\n",
" tbl.transform(types=tracker.types)\n",
" if pk: tbl.transform(pk=pk)\n",
" return tbl"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This uses [`sqlite_utils.utils.rows_from_file`](https://sqlite-utils.datasette.io/en/stable/reference.html#sqlite-utils-utils-rows-from-file) to load the file."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'id': 1, 'name': 'Alice', 'age': 30}, {'id': 2, 'name': 'Bob', 'age': 25}, {'id': 3, 'name': 'Charlie', 'age': 35}]\n"
]
}
],
"source": [
"db = Database(\":memory:\")\n",
"csv_data = \"\"\"id,name,age\n",
"1,Alice,30\n",
"2,Bob,25\n",
"3,Charlie,35\"\"\"\n",
"\n",
"table = db.import_file(\"people\", csv_data)\n",
"print(table())\n",
"table.drop()"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down

0 comments on commit 9810af7

Please sign in to comment.