Skip to content

Commit 12f3f94

Browse files
committed
Add a better hook to the README
1 parent 52f2f5f commit 12f3f94

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

README.md

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,10 @@
66
A simple embedded language for running inline SQL in Python programs.
77

88
```python
9-
import pandas as pd
109
from inline_sql import sql, sql_val
1110

12-
13-
def head_data(count: int) -> pd.DataFrame:
14-
return sql^ "SELECT * FROM 'cars.csv' LIMIT $count"
15-
16-
17-
cars = head_data(50)
18-
19-
origin_counts = sql^ """
20-
SELECT origin, COUNT() FROM cars
21-
GROUP BY origin
22-
ORDER BY count DESC
23-
"""
24-
print(origin_counts)
25-
26-
most_common = origin_counts.origin[0]
27-
print(sql_val^ """
28-
SELECT AVG(horsepower) FROM cars
29-
WHERE origin = $most_common
30-
""")
11+
assert sql_val^ "SELECT 1 + 1" == 2
12+
assert sql_val^ "SELECT COUNT() FROM 'disasters.csv'" == 803
3113
```
3214

3315
Operations in the `inline_sql` library directly use an in-memory database. You can access local datasets (pandas frames), CSV files, and interpolate variables seamlessly into queries. Internally, this is implemented as a small wrapper around [DuckDB](https://duckdb.org/).
@@ -76,6 +58,35 @@ The exported `sql` and `sql_val` variables are magic objects that can be used to
7658

7759
You can run any SQL query as described in the [DuckDB documentation](https://duckdb.org/docs/guides/).
7860

61+
## Library Use
62+
63+
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:
64+
65+
```python
66+
import pandas as pd
67+
from inline_sql import sql, sql_val
68+
69+
70+
def head_data(count: int) -> pd.DataFrame:
71+
return sql^ "SELECT * FROM 'cars.csv' LIMIT $count"
72+
73+
74+
cars = head_data(50)
75+
76+
origin_counts = sql^ """
77+
SELECT origin, COUNT() FROM cars
78+
GROUP BY origin
79+
ORDER BY count DESC
80+
"""
81+
print(origin_counts)
82+
83+
most_common = origin_counts.origin[0]
84+
print(sql_val^ """
85+
SELECT AVG(horsepower) FROM cars
86+
WHERE origin = $most_common
87+
""")
88+
```
89+
7990
## Acknowledgements
8091

8192
Created by Eric Zhang ([@ekzhang1](https://twitter.com/ekzhang1)). Licensed under the [MIT license](LICENSE).

0 commit comments

Comments
 (0)