Add source examples for Python 3.10 and 3.9 with updated syntax (#842)

Co-authored-by: Esteban Maya Cadavid <emayacadavid9@gmail.com>
This commit is contained in:
Sebastián Ramírez
2024-03-21 17:49:38 -05:00
committed by GitHub
parent 4c3f242ae2
commit 9141c8a920
39 changed files with 7456 additions and 25 deletions

View File

@@ -33,10 +33,22 @@ And now we will update `select_heroes()` to filter the data.
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python hl_lines="36-41"
{!./docs_src/tutorial/select/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python hl_lines="36-41"
{!./docs_src/tutorial/select/tutorial001.py!}
```
////
///
If you already executed the previous examples and have a database with data, **remove the database file** before running each example, that way you won't have duplicate data and you will be able to get the same results.
@@ -189,6 +201,20 @@ Let's review some of the code we used to read data with **SQLModel**.
We care specially about the **select** statement:
//// tab | Python 3.10+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/select/tutorial001_py310.py[ln:34-39]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5"
# Code above omitted 👆
@@ -197,18 +223,46 @@ We care specially about the **select** statement:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/select/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/select/tutorial001.py!}
```
////
///
## Filter Rows Using `WHERE` with **SQLModel**
Now, the same way that we add `WHERE` to a SQL statement to filter rows, we can add a `.where()` to a **SQLModel** `select()` statement to filter rows, which will filter the objects returned:
//// tab | Python 3.10+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/where/tutorial001_py310.py[ln:34-39]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5"
# Code above omitted 👆
@@ -217,12 +271,26 @@ Now, the same way that we add `WHERE` to a SQL statement to filter rows, we can
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/where/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/where/tutorial001.py!}
```
////
///
It's a very small change, but it's packed of details. Let's explore them.
@@ -469,6 +537,20 @@ Now that we know how `.where()` works, let's finish the code.
It's actually the same as in previous chapters for selecting data:
//// tab | Python 3.10+
```Python hl_lines="6-8"
# Code above omitted 👆
{!./docs_src/tutorial/where/tutorial001_py310.py[ln:34-39]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="6-8"
# Code above omitted 👆
@@ -477,12 +559,26 @@ It's actually the same as in previous chapters for selecting data:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/where/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/where/tutorial001.py!}
```
////
///
We take that statement, that now includes a `WHERE`, and we `exec()` it to get the results.
@@ -532,6 +628,20 @@ But we can use other standard Python comparisons. ✨
We could get the rows where a column is **not** equal to a value using `!=`:
//// tab | Python 3.10+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/where/tutorial002_py310.py[ln:34-39]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5"
# Code above omitted 👆
@@ -540,12 +650,26 @@ We could get the rows where a column is **not** equal to a value using `!=`:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/where/tutorial002_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/where/tutorial002.py!}
```
////
///
That would output:
@@ -559,6 +683,20 @@ secret_name='Tommy Sharp' age=48 id=3 name='Rusty-Man'
Let's update the function `create_heroes()` and add some more rows to make the next comparison examples clearer:
//// tab | Python 3.10+
```Python hl_lines="4-10 13-19"
# Code above omitted 👆
{!./docs_src/tutorial/where/tutorial003_py310.py[ln:21-39]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="4-10 13-19"
# Code above omitted 👆
@@ -567,12 +705,26 @@ Let's update the function `create_heroes()` and add some more rows to make the n
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/where/tutorial003_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/where/tutorial003.py!}
```
////
///
Now that we have several heroes with different ages, it's gonna be more obvious what the next comparisons do.
@@ -581,6 +733,20 @@ Now that we have several heroes with different ages, it's gonna be more obvious
Now let's use `>` to get the rows where a column is **more than** a value:
//// tab | Python 3.10+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/where/tutorial003_py310.py[ln:42-47]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5"
# Code above omitted 👆
@@ -589,12 +755,26 @@ Now let's use `>` to get the rows where a column is **more than** a value:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/where/tutorial003_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/where/tutorial003.py!}
```
////
///
That would output:
@@ -615,6 +795,20 @@ Notice that it didn't select `Black Lion`, because the age is not *strictly* gre
Let's do that again, but with `>=` to get the rows where a column is **more than or equal** to a value:
//// tab | Python 3.10+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/where/tutorial004_py310.py[ln:42-47]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5"
# Code above omitted 👆
@@ -623,12 +817,26 @@ Let's do that again, but with `>=` to get the rows where a column is **more than
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/where/tutorial004_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/where/tutorial004.py!}
```
////
///
Because we are using `>=`, the age `35` will be included in the output:
@@ -650,6 +858,20 @@ This time we got `Black Lion` too because although the age is not *strictly* gre
Similarly, we can use `<` to get the rows where a column is **less than** a value:
//// tab | Python 3.10+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/where/tutorial005_py310.py[ln:42-47]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5"
# Code above omitted 👆
@@ -658,12 +880,26 @@ Similarly, we can use `<` to get the rows where a column is **less than** a valu
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/where/tutorial005_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/where/tutorial005.py!}
```
////
///
And we get the younger one with an age in the database:
@@ -682,6 +918,20 @@ We could imagine that **Spider-Boy** is even **younger**. But because we don't k
Finally, we can use `<=` to get the rows where a column is **less than or equal** to a value:
//// tab | Python 3.10+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/where/tutorial006_py310.py[ln:42-47]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5"
# Code above omitted 👆
@@ -690,12 +940,26 @@ Finally, we can use `<=` to get the rows where a column is **less than or equal*
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/where/tutorial006_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/where/tutorial006.py!}
```
////
///
And we get the younger ones, `35` and below:
@@ -721,6 +985,20 @@ We can use the same standard Python comparison operators like `<`, `<=`, `>`, `>
Because `.where()` returns the same special select object back, we can add more `.where()` calls to it:
//// tab | Python 3.10+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/where/tutorial007_py310.py[ln:42-47]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5"
# Code above omitted 👆
@@ -729,12 +1007,26 @@ Because `.where()` returns the same special select object back, we can add more
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/where/tutorial007_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/where/tutorial007.py!}
```
////
///
This will select the rows `WHERE` the `age` is **greater than or equal** to `35`, `AND` also the `age` is **less than** `40`.
@@ -776,6 +1068,20 @@ age=36 id=6 name='Dr. Weird' secret_name='Steve Weird'
As an alternative to using multiple `.where()` we can also pass several expressions to a single `.where()`:
//// tab | Python 3.10+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/where/tutorial008_py310.py[ln:42-47]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5"
# Code above omitted 👆
@@ -784,12 +1090,26 @@ As an alternative to using multiple `.where()` we can also pass several expressi
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/where/tutorial008_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/where/tutorial008.py!}
```
////
///
This is the same as the above, and will result in the same output with the two heroes:
@@ -807,24 +1127,64 @@ But we can also combine expressions using `OR`. Which means that **any** (but no
To do it, you can import `or_`:
//// tab | Python 3.10+
```Python hl_lines="1"
{!./docs_src/tutorial/where/tutorial009_py310.py[ln:1]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="3"
{!./docs_src/tutorial/where/tutorial009.py[ln:1-3]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/where/tutorial009_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/where/tutorial009.py!}
```
////
///
And then pass both expressions to `or_()` and put it inside `.where()`.
For example, here we select the heroes that are the youngest OR the oldest:
//// tab | Python 3.10+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/where/tutorial009_py310.py[ln:42-47]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5"
# Code above omitted 👆
@@ -833,12 +1193,26 @@ For example, here we select the heroes that are the youngest OR the oldest:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/where/tutorial009_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/where/tutorial009.py!}
```
////
///
When we run it, this generates the output:
@@ -890,22 +1264,62 @@ We can tell the editor that this class attribute is actually a special **SQLMode
To do that, we can import `col()` (as short for "column"):
//// tab | Python 3.10+
```Python hl_lines="1"
{!./docs_src/tutorial/where/tutorial011_py310.py[ln:1]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="3"
{!./docs_src/tutorial/where/tutorial011.py[ln:1-3]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/where/tutorial011_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/where/tutorial011.py!}
```
////
///
And then put the **class attribute** inside `col()` when using it in a `.where()`:
//// tab | Python 3.10+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/where/tutorial011_py310.py[ln:42-47]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5"
# Code above omitted 👆
@@ -914,12 +1328,26 @@ And then put the **class attribute** inside `col()` when using it in a `.where()
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/where/tutorial011_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/where/tutorial011.py!}
```
////
///
So, now the comparison is not: