Name | inline-sql JSON |
Version |
0.1.2
JSON |
| download |
home_page | None |
Summary | Inline SQL in any Python program, on local dataframes |
upload_time | 2024-02-17 00:49:35 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.7 |
license | None |
keywords |
database
language
sql
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Inline SQL
[![PyPI - Version](https://img.shields.io/pypi/v/inline-sql.svg)](https://pypi.org/project/inline-sql)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/inline-sql.svg)](https://pypi.org/project/inline-sql)
A simple embedded language for running inline SQL in Python programs.
```python
from inline_sql import sql, sql_val
assert sql_val^ "SELECT 1 + 1" == 2
x = 5
assert sql_val^ "SELECT $x * 2" == 10
df = sql^ "SELECT * FROM (VALUES (1, 10), (2, 20)) df (x, y)"
assert sql_val^ "SELECT SUM(x) + SUM(y) FROM df" == 33
```
Operations in the `inline_sql` library run directly inside your process. You can query local datasets (pandas frames), CSV files, and even interpolate variables seamlessly. This is implemented as a small wrapper around [DuckDB](https://duckdb.org/), so it is [extremely fast](https://duckdb.org/2021/05/14/sql-on-pandas.html).
## Installation
Supports Python 3.7+, tested on all major operating systems.
```console
pip install inline-sql
```
## Usage
The exported `sql` and `sql_val` variables are magic objects that can be used to run queries. Queries can read from local dataframes by name, and they can embed parameters using dollar-sign notation.
```python
>>> from inline_sql import sql, sql_val
>>> sql_val^ "SELECT 1 + 1"
2
>>> x = 5
>>> sql_val^ "SELECT 2 * $x"
10
>>> sql^ "SELECT * FROM 'disasters.csv' LIMIT 5"
Entity Year Deaths
0 All natural disasters 1900 1267360
1 All natural disasters 1901 200018
2 All natural disasters 1902 46037
3 All natural disasters 1903 6506
4 All natural disasters 1905 22758
>>> disasters = sql^ "SELECT * FROM 'disasters.csv'"
>>> def total_deaths(entity: str) -> float:
... return sql_val^ "SELECT SUM(deaths) FROM disasters WHERE Entity = $entity"
...
>>> total_deaths("Drought")
11731294.0
>>> total_deaths("Earthquake")
2576801.0
```
You can run any SQL query as described in the [DuckDB documentation](https://duckdb.org/docs/guides/).
## Library Use
You can use `inline_sql` as a library. Since results from queries are ordinary `pandas.DataFrame` objects, they work in functions and application code. Here's a longer example:
```python
import pandas as pd
from inline_sql import sql, sql_val
def head_data(count: int) -> pd.DataFrame:
return sql^ "SELECT * FROM 'cars.csv' LIMIT $count"
cars = head_data(50)
origin_counts = sql^ """
SELECT origin, COUNT() FROM cars
GROUP BY origin
ORDER BY count DESC
"""
print(origin_counts)
most_common = origin_counts.origin[0]
print(sql_val^ """
SELECT AVG(horsepower) FROM cars
WHERE origin = $most_common
""")
```
In general, `sql_val` is used to run scalar queries, while `sql` is used to run queries that return tables.
## Acknowledgements
Created by Eric Zhang ([@ekzhang1](https://twitter.com/ekzhang1)). Licensed under the [MIT license](LICENSE).
Raw data
{
"_id": null,
"home_page": null,
"name": "inline-sql",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": "database,language,sql",
"author": null,
"author_email": "Eric Zhang <ekzhang1@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/80/8f/ec43ae1be399c3283aa41a48c84d4e01f09e1864eb7d962b62f4b74de90b/inline_sql-0.1.2.tar.gz",
"platform": null,
"description": "# Inline SQL\n\n[![PyPI - Version](https://img.shields.io/pypi/v/inline-sql.svg)](https://pypi.org/project/inline-sql)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/inline-sql.svg)](https://pypi.org/project/inline-sql)\n\nA simple embedded language for running inline SQL in Python programs.\n\n```python\nfrom inline_sql import sql, sql_val\n\nassert sql_val^ \"SELECT 1 + 1\" == 2\n\nx = 5\nassert sql_val^ \"SELECT $x * 2\" == 10\n\ndf = sql^ \"SELECT * FROM (VALUES (1, 10), (2, 20)) df (x, y)\"\nassert sql_val^ \"SELECT SUM(x) + SUM(y) FROM df\" == 33\n```\n\nOperations in the `inline_sql` library run directly inside your process. You can query local datasets (pandas frames), CSV files, and even interpolate variables seamlessly. This is implemented as a small wrapper around [DuckDB](https://duckdb.org/), so it is [extremely fast](https://duckdb.org/2021/05/14/sql-on-pandas.html).\n\n## Installation\n\nSupports Python 3.7+, tested on all major operating systems.\n\n```console\npip install inline-sql\n```\n\n## Usage\n\nThe exported `sql` and `sql_val` variables are magic objects that can be used to run queries. Queries can read from local dataframes by name, and they can embed parameters using dollar-sign notation.\n\n```python\n>>> from inline_sql import sql, sql_val\n\n>>> sql_val^ \"SELECT 1 + 1\"\n2\n\n>>> x = 5\n\n>>> sql_val^ \"SELECT 2 * $x\"\n10\n\n>>> sql^ \"SELECT * FROM 'disasters.csv' LIMIT 5\"\n Entity Year Deaths\n0 All natural disasters 1900 1267360\n1 All natural disasters 1901 200018\n2 All natural disasters 1902 46037\n3 All natural disasters 1903 6506\n4 All natural disasters 1905 22758\n\n>>> disasters = sql^ \"SELECT * FROM 'disasters.csv'\"\n\n>>> def total_deaths(entity: str) -> float:\n... return sql_val^ \"SELECT SUM(deaths) FROM disasters WHERE Entity = $entity\"\n...\n\n>>> total_deaths(\"Drought\")\n11731294.0\n\n>>> total_deaths(\"Earthquake\")\n2576801.0\n```\n\nYou can run any SQL query as described in the [DuckDB documentation](https://duckdb.org/docs/guides/).\n\n## Library Use\n\nYou can use `inline_sql` as a library. Since results from queries are ordinary `pandas.DataFrame` objects, they work in functions and application code. Here's a longer example:\n\n```python\nimport pandas as pd\nfrom inline_sql import sql, sql_val\n\n\ndef head_data(count: int) -> pd.DataFrame:\n return sql^ \"SELECT * FROM 'cars.csv' LIMIT $count\"\n\n\ncars = head_data(50)\n\norigin_counts = sql^ \"\"\"\n SELECT origin, COUNT() FROM cars\n GROUP BY origin\n ORDER BY count DESC\n\"\"\"\nprint(origin_counts)\n\nmost_common = origin_counts.origin[0]\nprint(sql_val^ \"\"\"\n SELECT AVG(horsepower) FROM cars\n WHERE origin = $most_common\n\"\"\")\n```\n\nIn general, `sql_val` is used to run scalar queries, while `sql` is used to run queries that return tables.\n\n## Acknowledgements\n\nCreated by Eric Zhang ([@ekzhang1](https://twitter.com/ekzhang1)). Licensed under the [MIT license](LICENSE).\n",
"bugtrack_url": null,
"license": null,
"summary": "Inline SQL in any Python program, on local dataframes",
"version": "0.1.2",
"project_urls": {
"Documentation": "https://github.com/ekzhang/inline-sql#readme",
"Issues": "https://github.com/ekzhang/inline-sql/issues",
"Source": "https://github.com/ekzhang/inline-sql"
},
"split_keywords": [
"database",
"language",
"sql"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "2e18469972571d201e43556e298069f7dddbe6417a90031e1987dfa86f802e8f",
"md5": "af9702c16e8a67fecac687721f6bc008",
"sha256": "5db7c6691fa4cda7588e450e6b2219de27b0ae099d82125e65768629c245b2dc"
},
"downloads": -1,
"filename": "inline_sql-0.1.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "af9702c16e8a67fecac687721f6bc008",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 5339,
"upload_time": "2024-02-17T00:49:33",
"upload_time_iso_8601": "2024-02-17T00:49:33.576100Z",
"url": "https://files.pythonhosted.org/packages/2e/18/469972571d201e43556e298069f7dddbe6417a90031e1987dfa86f802e8f/inline_sql-0.1.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "808fec43ae1be399c3283aa41a48c84d4e01f09e1864eb7d962b62f4b74de90b",
"md5": "45ed9a8e98640a02b4959362bc6f2267",
"sha256": "c0ad4826c1eb243dbcfcb1958a94191578d65bddc3a26633c60862062a86612d"
},
"downloads": -1,
"filename": "inline_sql-0.1.2.tar.gz",
"has_sig": false,
"md5_digest": "45ed9a8e98640a02b4959362bc6f2267",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 22363,
"upload_time": "2024-02-17T00:49:35",
"upload_time_iso_8601": "2024-02-17T00:49:35.366569Z",
"url": "https://files.pythonhosted.org/packages/80/8f/ec43ae1be399c3283aa41a48c84d4e01f09e1864eb7d962b62f4b74de90b/inline_sql-0.1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-02-17 00:49:35",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ekzhang",
"github_project": "inline-sql#readme",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "inline-sql"
}