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

@@ -16,10 +16,22 @@ We'll continue with the same examples we have been using in the previous chapter
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/indexes/tutorial002_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/indexes/tutorial002.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.
@@ -28,6 +40,20 @@ If you already executed the previous examples and have a database with data, **r
We have been iterating over the rows in a `result` object like:
//// tab | Python 3.10+
```Python hl_lines="7-8"
# Code above omitted 👆
{!./docs_src/tutorial/indexes/tutorial002_py310.py[ln:42-47]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="7-8"
# Code above omitted 👆
@@ -36,18 +62,46 @@ We have been iterating over the rows in a `result` object like:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/indexes/tutorial002_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/indexes/tutorial002.py!}
```
////
///
But let's say that we are not interested in all the rows, just the **first** one.
We can call the `.first()` method on the `results` object to get the first row:
//// tab | Python 3.10+
```Python hl_lines="7"
# Code above omitted 👆
{!./docs_src/tutorial/one/tutorial001_py310.py[ln:42-47]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="7"
# Code above omitted 👆
@@ -56,12 +110,26 @@ We can call the `.first()` method on the `results` object to get the first row:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/one/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/one/tutorial001.py!}
```
////
///
This will return the first object in the `results` (if there was any).
@@ -103,6 +171,20 @@ It would be possible that the SQL query doesn't find any row.
In that case, `.first()` will return `None`:
//// tab | Python 3.10+
```Python hl_lines="5 7"
# Code above omitted 👆
{!./docs_src/tutorial/one/tutorial002_py310.py[ln:42-47]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5 7"
# Code above omitted 👆
@@ -111,12 +193,26 @@ In that case, `.first()` will return `None`:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/one/tutorial002_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/one/tutorial002.py!}
```
////
///
In this case, as there's no hero with an age less than 25, `.first()` will return `None`.
@@ -150,6 +246,20 @@ And if there was more than one, it would mean that there's an error in the syste
In that case, instead of `.first()` we can use `.one()`:
//// tab | Python 3.10+
```Python hl_lines="7"
# Code above omitted 👆
{!./docs_src/tutorial/one/tutorial003_py310.py[ln:42-47]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="7"
# Code above omitted 👆
@@ -158,12 +268,26 @@ In that case, instead of `.first()` we can use `.one()`:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/one/tutorial003_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/one/tutorial003.py!}
```
////
///
Here we know that there's only one `"Deadpond"`, and there shouldn't be any more than one.
@@ -220,6 +344,20 @@ sqlalchemy.exc.MultipleResultsFound: Multiple rows were found when exactly one w
Of course, even if we don't duplicate the data, we could get the same error if we send a query that finds more than one row and expect exactly one with `.one()`:
//// tab | Python 3.10+
```Python hl_lines="5 7"
# Code above omitted 👆
{!./docs_src/tutorial/one/tutorial004_py310.py[ln:42-47]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5 7"
# Code above omitted 👆
@@ -228,12 +366,26 @@ Of course, even if we don't duplicate the data, we could get the same error if w
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/one/tutorial004_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/one/tutorial004.py!}
```
////
///
That would find 2 rows, and would end up with the same error.
@@ -242,6 +394,20 @@ That would find 2 rows, and would end up with the same error.
And also, if we get no rows at all with `.one()`, it will also raise an error:
//// tab | Python 3.10+
```Python hl_lines="5 7"
# Code above omitted 👆
{!./docs_src/tutorial/one/tutorial005_py310.py[ln:42-47]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5 7"
# Code above omitted 👆
@@ -250,12 +416,26 @@ And also, if we get no rows at all with `.one()`, it will also raise an error:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/one/tutorial005_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/one/tutorial005.py!}
```
////
///
In this case, as there are no heroes with an age less than 25, `.one()` will raise an error.
@@ -289,6 +469,20 @@ sqlalchemy.exc.NoResultFound: No row was found when one was required
Of course, with `.first()` and `.one()` you would also probably write all that in a more compact form most of the time, all in a single line (or at least a single Python statement):
//// tab | Python 3.10+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/one/tutorial006_py310.py[ln:42-45]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5"
# Code above omitted 👆
@@ -297,12 +491,26 @@ Of course, with `.first()` and `.one()` you would also probably write all that i
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/one/tutorial006_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/one/tutorial006.py!}
```
////
///
That would result in the same as some examples above.
@@ -313,6 +521,20 @@ In many cases you might want to select a single row by its Id column with the **
You could do it the same way we have been doing with a `.where()` and then getting the first item with `.first()`:
//// tab | Python 3.10+
```Python hl_lines="5 7"
# Code above omitted 👆
{!./docs_src/tutorial/one/tutorial007_py310.py[ln:42-47]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5 7"
# Code above omitted 👆
@@ -321,12 +543,26 @@ You could do it the same way we have been doing with a `.where()` and then getti
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/one/tutorial007_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/one/tutorial007.py!}
```
////
///
That would work correctly, as expected. But there's a shorter version. 👇
@@ -335,6 +571,20 @@ That would work correctly, as expected. But there's a shorter version. 👇
As selecting a single row by its Id column with the **primary key** is a common operation, there's a shortcut for it:
//// tab | Python 3.10+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/one/tutorial008_py310.py[ln:42-45]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5"
# Code above omitted 👆
@@ -343,12 +593,26 @@ As selecting a single row by its Id column with the **primary key** is a common
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/one/tutorial008_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/one/tutorial008.py!}
```
////
///
`session.get(Hero, 1)` is an equivalent to creating a `select()`, then filtering by Id using `.where()`, and then getting the first item with `.first()`.
@@ -378,6 +642,20 @@ Hero: secret_name='Dive Wilson' age=None id=1 name='Deadpond'
`.get()` behaves similar to `.first()`, if there's no data it will simply return `None` (instead of raising an error):
//// tab | Python 3.10+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/one/tutorial009_py310.py[ln:42-45]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5"
# Code above omitted 👆
@@ -386,12 +664,26 @@ Hero: secret_name='Dive Wilson' age=None id=1 name='Deadpond'
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/one/tutorial009_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/one/tutorial009.py!}
```
////
///
Running that will output: