fastduck


Namefastduck JSON
Version 0.0.11 PyPI version JSON
download
home_pagehttps://github.com/fredguth/fastduck
SummaryA bit of extra usability for duckdb... inspired by fastlite and sqlite_utils
upload_time2024-07-10 11:14:24
maintainerNone
docs_urlNone
authorFred Guth
requires_python>=3.10
licenseApache Software License 2.0
keywords nbdev jupyter notebook python duckdb sql
VCS
bugtrack_url
requirements fastcore duckdb graphviz numpy pandas ipykernel pyarrow
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # fastduck


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

`fastduck` provides some development experience improvements for the
standard `duckdb` python API.

## Install

``` sh
pip install fastduck
```

## How to use

~~import fastduck as fuck~~

``` python
from fastduck import database
```

``` python
db = database('../data/chinook.duckdb')
db
```

    DuckDBPyConnection (chinook_main)

``` python
dt = db.t
dt
```

    (chinook_main) Tables: Album, Artist, Customer, Employee, Genre, Invoice, InvoiceLine, MediaType, Playlist, PlaylistTrack, Track, todos

You can use this to grab a single table…

``` python
artist = dt.Artist
artist
```

#### DuckDBPyRelation BASE TABLE chinook.main.Artist

| ArtistId | Name                  |
|:---------|:----------------------|
| 1        | AC/DC                 |
| 2        | Accept                |
| 3        | Aerosmith             |
| …        | …                     |
| 274      | Nash Ensemble         |
| 275      | Philip Glass Ensemble |

275 rows x 2 cols

``` python
customer = dt['Customer']
customer
```

#### DuckDBPyRelation BASE TABLE chinook.main.Customer

| CustomerId | FirstName | LastName   | Company                                          | Address                         | City                | State | Country | PostalCode | Phone              | Fax                | Email                    | SupportRepId |
|:-----------|:----------|:-----------|:-------------------------------------------------|:--------------------------------|:--------------------|:------|:--------|:-----------|:-------------------|:-------------------|:-------------------------|:-------------|
| 1          | Luís      | Gonçalves  | Embraer - Empresa Brasileira de Aeronáutica S.A. | Av. Brigadeiro Faria Lima, 2170 | São José dos Campos | SP    | Brazil  | 12227-000  | +55 (12) 3923-5555 | +55 (12) 3923-5566 | luisg@embraer.com.br     | 3            |
| 2          | Leonie    | Köhler     |                                                  | Theodor-Heuss-Straße 34         | Stuttgart           |       | Germany | 70174      | +49 0711 2842222   |                    | leonekohler@surfeu.de    | 5            |
| 3          | François  | Tremblay   |                                                  | 1498 rue Bélanger               | Montréal            | QC    | Canada  | H2G 1A7    | +1 (514) 721-4711  |                    | ftremblay@gmail.com      | 3            |
| …          | …         | …          | …                                                | …                               | …                   | …     | …       | …          | …                  | …                  | …                        | …            |
| 58         | Manoj     | Pareek     |                                                  | 12,Community Centre             | Delhi               |       | India   | 110017     | +91 0124 39883988  |                    | manoj.pareek@rediff.com  | 3            |
| 59         | Puja      | Srivastava |                                                  | 3,Raj Bhavan Road               | Bangalore           |       | India   | 560001     | +91 080 22289999   |                    | puja_srivastava@yahoo.in | 3            |

59 rows x 13 cols

… or multiple tables at once:

``` python
dt['Artist', 'Album', 'Genre']
```

    [DuckDBPyRelation BASE TABLE chinook.main.Artist 
     ,
     DuckDBPyRelation BASE TABLE chinook.main.Album 
     ,
     DuckDBPyRelation BASE TABLE chinook.main.Genre 
     ]

It also provides auto-complete in Jupyter, IPython and nearly any other
interactive Python environment:

<img src="/nbs/images/autocomplete.png" width="400"
alt="Autocomplete in Jupyter" />

You can check if a table is in the database already:

``` python
'Artist' in dt
```

    True

Column work in a similar way to tables, using the `c` property:

``` python
ac = artist.c
ac, artist.columns
```

    (chinook.main.Artist Columns: ArtistId, Name, ['ArtistId', 'Name'])

Auto-complete works for columns too:

<img src="/nbs/images/columns_complete.png" width="300"
alt="Columns autocomplete in Jupyter" />

The tables and views of a database got some interesting new attributes….

``` python
artist.meta
```

    {'base': DuckDBPyConnection (chinook_main),
     'catalog': 'chinook',
     'schema': 'main',
     'name': 'Artist',
     'type': 'BASE TABLE',
     'comment': None,
     'shape': (275, 2)}

``` python
artist.model
```

    [{'name': 'ArtistId',
      'type': 'INTEGER',
      'nullable': False,
      'default': None,
      'pk': True},
     {'name': 'Name',
      'type': 'VARCHAR',
      'nullable': True,
      'default': None,
      'pk': False}]

``` python
artist.cls, type(artist.cls)
```

    (fastduck.core.Artist, type)

`duckdb` replacement scans keep working and are wonderful for usage in
SQL statements:

``` python
db.sql("select * from artist where artist.Name like 'AC/%'")
```

#### DuckDBPyRelation

| ArtistId | Name  |
|---------:|:------|
|        1 | AC/DC |

1 rows x 2 cols

You can view the results of a query as records

``` python
db.sql("select * from artist where artist.Name like 'AC/%'").to_recs()
```

    [{'ArtistId': 1, 'Name': 'AC/DC'}]

or as a list of lists

``` python
db.sql("select * from artist where artist.Name like 'AC/%'").to_list()
```

    [[1, 'AC/DC']]

And you there is also an alias for `sql` with `to_recs` simply called
`q`

``` python
db.q("select * from artist where artist.Name like 'AC/%'")
```

    [{'ArtistId': 1, 'Name': 'AC/DC'}]

#### Dataclass support

As we briefly saw, a `dataclass` type with the names, types and defaults
of the table is added to the Relation:

``` python
abm = db.t.Album
art = db.t.Artist
acca_sql = f"""
select abm.* 
from abm join art using (ArtistID)
where art.Name like 'AC/%'
"""
acca_dacca = db.q(acca_sql)
acca_dacca
```

    [{'AlbumId': 1,
      'Title': 'For Those About To Rock We Salute You',
      'ArtistId': 1},
     {'AlbumId': 4, 'Title': 'Let There Be Rock', 'ArtistId': 1}]

``` python
let_b_rock_obj = abm.cls(**acca_dacca[-1])
let_b_rock_obj
```

    Album(AlbumId=4, Title='Let There Be Rock', ArtistId=1)

You can get the definition of the dataclass using fastcore’s
`dataclass_src` – everything is treated as nullable, in order to handle
auto-generated database values:

``` python
from fastcore.xtras import hl_md, dataclass_src

src = dataclass_src(db.t.Album.cls)
hl_md(src, 'python')
```

``` python
@dataclass
class Album:
    AlbumId: int32 = None
    Title: str = None
    ArtistId: int32 = None
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/fredguth/fastduck",
    "name": "fastduck",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "nbdev jupyter notebook python duckdb sql",
    "author": "Fred Guth",
    "author_email": "fredguth@fredguth.com",
    "download_url": "https://files.pythonhosted.org/packages/bb/3b/888dd13147a308428e763f26bf5506de37cad93f7fd1e185a6a0efc1d6ee/fastduck-0.0.11.tar.gz",
    "platform": null,
    "description": "# fastduck\n\n\n<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->\n\n`fastduck` provides some development experience improvements for the\nstandard `duckdb` python API.\n\n## Install\n\n``` sh\npip install fastduck\n```\n\n## How to use\n\n~~import fastduck as fuck~~\n\n``` python\nfrom fastduck import database\n```\n\n``` python\ndb = database('../data/chinook.duckdb')\ndb\n```\n\n    DuckDBPyConnection (chinook_main)\n\n``` python\ndt = db.t\ndt\n```\n\n    (chinook_main) Tables: Album, Artist, Customer, Employee, Genre, Invoice, InvoiceLine, MediaType, Playlist, PlaylistTrack, Track, todos\n\nYou can use this to grab a single table\u2026\n\n``` python\nartist = dt.Artist\nartist\n```\n\n#### DuckDBPyRelation BASE TABLE chinook.main.Artist\n\n| ArtistId | Name                  |\n|:---------|:----------------------|\n| 1        | AC/DC                 |\n| 2        | Accept                |\n| 3        | Aerosmith             |\n| \u2026        | \u2026                     |\n| 274      | Nash Ensemble         |\n| 275      | Philip Glass Ensemble |\n\n275 rows x 2 cols\n\n``` python\ncustomer = dt['Customer']\ncustomer\n```\n\n#### DuckDBPyRelation BASE TABLE chinook.main.Customer\n\n| CustomerId | FirstName | LastName   | Company                                          | Address                         | City                | State | Country | PostalCode | Phone              | Fax                | Email                    | SupportRepId |\n|:-----------|:----------|:-----------|:-------------------------------------------------|:--------------------------------|:--------------------|:------|:--------|:-----------|:-------------------|:-------------------|:-------------------------|:-------------|\n| 1          | Lu\u00eds      | Gon\u00e7alves  | Embraer - Empresa Brasileira de Aeron\u00e1utica S.A. | Av. Brigadeiro Faria Lima, 2170 | S\u00e3o Jos\u00e9 dos Campos | SP    | Brazil  | 12227-000  | +55 (12) 3923-5555 | +55 (12) 3923-5566 | luisg@embraer.com.br     | 3            |\n| 2          | Leonie    | K\u00f6hler     |                                                  | Theodor-Heuss-Stra\u00dfe 34         | Stuttgart           |       | Germany | 70174      | +49 0711 2842222   |                    | leonekohler@surfeu.de    | 5            |\n| 3          | Fran\u00e7ois  | Tremblay   |                                                  | 1498 rue B\u00e9langer               | Montr\u00e9al            | QC    | Canada  | H2G 1A7    | +1 (514) 721-4711  |                    | ftremblay@gmail.com      | 3            |\n| \u2026          | \u2026         | \u2026          | \u2026                                                | \u2026                               | \u2026                   | \u2026     | \u2026       | \u2026          | \u2026                  | \u2026                  | \u2026                        | \u2026            |\n| 58         | Manoj     | Pareek     |                                                  | 12,Community Centre             | Delhi               |       | India   | 110017     | +91 0124 39883988  |                    | manoj.pareek@rediff.com  | 3            |\n| 59         | Puja      | Srivastava |                                                  | 3,Raj Bhavan Road               | Bangalore           |       | India   | 560001     | +91 080 22289999   |                    | puja_srivastava@yahoo.in | 3            |\n\n59 rows x 13 cols\n\n\u2026 or multiple tables at once:\n\n``` python\ndt['Artist', 'Album', 'Genre']\n```\n\n    [DuckDBPyRelation BASE TABLE chinook.main.Artist \n     ,\n     DuckDBPyRelation BASE TABLE chinook.main.Album \n     ,\n     DuckDBPyRelation BASE TABLE chinook.main.Genre \n     ]\n\nIt also provides auto-complete in Jupyter, IPython and nearly any other\ninteractive Python environment:\n\n<img src=\"/nbs/images/autocomplete.png\" width=\"400\"\nalt=\"Autocomplete in Jupyter\" />\n\nYou can check if a table is in the database already:\n\n``` python\n'Artist' in dt\n```\n\n    True\n\nColumn work in a similar way to tables, using the `c` property:\n\n``` python\nac = artist.c\nac, artist.columns\n```\n\n    (chinook.main.Artist Columns: ArtistId, Name, ['ArtistId', 'Name'])\n\nAuto-complete works for columns too:\n\n<img src=\"/nbs/images/columns_complete.png\" width=\"300\"\nalt=\"Columns autocomplete in Jupyter\" />\n\nThe tables and views of a database got some interesting new attributes\u2026.\n\n``` python\nartist.meta\n```\n\n    {'base': DuckDBPyConnection (chinook_main),\n     'catalog': 'chinook',\n     'schema': 'main',\n     'name': 'Artist',\n     'type': 'BASE TABLE',\n     'comment': None,\n     'shape': (275, 2)}\n\n``` python\nartist.model\n```\n\n    [{'name': 'ArtistId',\n      'type': 'INTEGER',\n      'nullable': False,\n      'default': None,\n      'pk': True},\n     {'name': 'Name',\n      'type': 'VARCHAR',\n      'nullable': True,\n      'default': None,\n      'pk': False}]\n\n``` python\nartist.cls, type(artist.cls)\n```\n\n    (fastduck.core.Artist, type)\n\n`duckdb` replacement scans keep working and are wonderful for usage in\nSQL statements:\n\n``` python\ndb.sql(\"select * from artist where artist.Name like 'AC/%'\")\n```\n\n#### DuckDBPyRelation\n\n| ArtistId | Name  |\n|---------:|:------|\n|        1 | AC/DC |\n\n1 rows x 2 cols\n\nYou can view the results of a query as records\n\n``` python\ndb.sql(\"select * from artist where artist.Name like 'AC/%'\").to_recs()\n```\n\n    [{'ArtistId': 1, 'Name': 'AC/DC'}]\n\nor as a list of lists\n\n``` python\ndb.sql(\"select * from artist where artist.Name like 'AC/%'\").to_list()\n```\n\n    [[1, 'AC/DC']]\n\nAnd you there is also an alias for `sql` with `to_recs` simply called\n`q`\n\n``` python\ndb.q(\"select * from artist where artist.Name like 'AC/%'\")\n```\n\n    [{'ArtistId': 1, 'Name': 'AC/DC'}]\n\n#### Dataclass support\n\nAs we briefly saw, a `dataclass` type with the names, types and defaults\nof the table is added to the Relation:\n\n``` python\nabm = db.t.Album\nart = db.t.Artist\nacca_sql = f\"\"\"\nselect abm.* \nfrom abm join art using (ArtistID)\nwhere art.Name like 'AC/%'\n\"\"\"\nacca_dacca = db.q(acca_sql)\nacca_dacca\n```\n\n    [{'AlbumId': 1,\n      'Title': 'For Those About To Rock We Salute You',\n      'ArtistId': 1},\n     {'AlbumId': 4, 'Title': 'Let There Be Rock', 'ArtistId': 1}]\n\n``` python\nlet_b_rock_obj = abm.cls(**acca_dacca[-1])\nlet_b_rock_obj\n```\n\n    Album(AlbumId=4, Title='Let There Be Rock', ArtistId=1)\n\nYou can get the definition of the dataclass using fastcore\u2019s\n`dataclass_src` \u2013 everything is treated as nullable, in order to handle\nauto-generated database values:\n\n``` python\nfrom fastcore.xtras import hl_md, dataclass_src\n\nsrc = dataclass_src(db.t.Album.cls)\nhl_md(src, 'python')\n```\n\n``` python\n@dataclass\nclass Album:\n    AlbumId: int32 = None\n    Title: str = None\n    ArtistId: int32 = None\n```\n",
    "bugtrack_url": null,
    "license": "Apache Software License 2.0",
    "summary": "A bit of extra usability for duckdb... inspired by fastlite and sqlite_utils",
    "version": "0.0.11",
    "project_urls": {
        "Homepage": "https://github.com/fredguth/fastduck"
    },
    "split_keywords": [
        "nbdev",
        "jupyter",
        "notebook",
        "python",
        "duckdb",
        "sql"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "950e6fb6e35fd8890509a892c53764c7b058c9cb3a36b62ef77c578b4c634431",
                "md5": "3f420ea87620d522fd8cc01ed32aaa8e",
                "sha256": "278699ee70ac55732f76a023f378cc07cd164a71ac785e59688f5278e516e0b8"
            },
            "downloads": -1,
            "filename": "fastduck-0.0.11-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3f420ea87620d522fd8cc01ed32aaa8e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 14241,
            "upload_time": "2024-07-10T11:14:23",
            "upload_time_iso_8601": "2024-07-10T11:14:23.470100Z",
            "url": "https://files.pythonhosted.org/packages/95/0e/6fb6e35fd8890509a892c53764c7b058c9cb3a36b62ef77c578b4c634431/fastduck-0.0.11-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bb3b888dd13147a308428e763f26bf5506de37cad93f7fd1e185a6a0efc1d6ee",
                "md5": "0bbedf732206b7ea0d35c81a4da316d4",
                "sha256": "5042a8f3251515cbef350987cfb13b793520a4cb6fe059d97e87c5fa2c4b18f0"
            },
            "downloads": -1,
            "filename": "fastduck-0.0.11.tar.gz",
            "has_sig": false,
            "md5_digest": "0bbedf732206b7ea0d35c81a4da316d4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 17264,
            "upload_time": "2024-07-10T11:14:24",
            "upload_time_iso_8601": "2024-07-10T11:14:24.925583Z",
            "url": "https://files.pythonhosted.org/packages/bb/3b/888dd13147a308428e763f26bf5506de37cad93f7fd1e185a6a0efc1d6ee/fastduck-0.0.11.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-10 11:14:24",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "fredguth",
    "github_project": "fastduck",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "fastcore",
            "specs": [
                [
                    ">=",
                    "1.5.41"
                ]
            ]
        },
        {
            "name": "duckdb",
            "specs": []
        },
        {
            "name": "graphviz",
            "specs": []
        },
        {
            "name": "numpy",
            "specs": []
        },
        {
            "name": "pandas",
            "specs": []
        },
        {
            "name": "ipykernel",
            "specs": []
        },
        {
            "name": "pyarrow",
            "specs": []
        }
    ],
    "lcname": "fastduck"
}
        
Elapsed time: 0.49466s