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

@@ -6,6 +6,20 @@ Now let's talk a bit about why the `id` field **can't be `NULL`** on the databas
But the same `id` field actually **can be `None`** in the Python code, so we declare the type with `Optional[int]`, and set the default value to `Field(default=None)`:
//// tab | Python 3.10+
```Python hl_lines="4"
# Code above omitted 👆
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py[ln:4-8]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="4"
# Code above omitted 👆
@@ -14,12 +28,26 @@ But the same `id` field actually **can be `None`** in the Python code, so we dec
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py!}
```
////
///
Next, I'll show you a bit more about the synchronization of data between the database and the Python code.
@@ -30,6 +58,20 @@ When do we get an actual `int` from the database in that `id` field? Let's see a
When we create a new `Hero` instance, we don't set the `id`:
//// tab | Python 3.10+
```Python hl_lines="3-6"
# Code above omitted 👆
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py[ln:21-24]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="3-6"
# Code above omitted 👆
@@ -38,12 +80,26 @@ When we create a new `Hero` instance, we don't set the `id`:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py!}
```
////
///
### How `Optional` Helps
@@ -72,6 +128,20 @@ But by declaring it with `Optional[int]`, the editor will help us to avoid writi
We can confirm that by printing our heroes before adding them to the database:
//// tab | Python 3.10+
```Python hl_lines="9-11"
# Code above omitted 👆
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py[ln:21-29]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="9-11"
# Code above omitted 👆
@@ -80,12 +150,26 @@ We can confirm that by printing our heroes before adding them to the database:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py!}
```
////
///
That will output:
@@ -117,6 +201,20 @@ After we add the `Hero` instance objects to the **session**, the IDs are *still*
We can verify by creating a session using a `with` block and adding the objects. And then printing them again:
//// tab | Python 3.10+
```Python hl_lines="19-21"
# Code above omitted 👆
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py[ln:21-39]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="19-21"
# Code above omitted 👆
@@ -125,12 +223,26 @@ We can verify by creating a session using a `with` block and adding the objects.
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py!}
```
////
///
This will, again, output the `id`s of the objects as `None`:
@@ -156,7 +268,21 @@ As we saw before, the **session** is smart and doesn't talk to the database ever
Then we can `commit` the changes in the session, and print again:
```Python hl_lines="13 16-18"
//// tab | Python 3.10+
```Python hl_lines="13 16-18"
# Code above omitted 👆
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py[ln:31-46]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="13 16-18"
# Code above omitted 👆
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py[ln:33-48]!}
@@ -164,12 +290,26 @@ Then we can `commit` the changes in the session, and print again:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py!}
```
////
///
And now, something unexpected happens, look at the output, it seems as if the `Hero` instance objects had no data at all:
@@ -228,7 +368,21 @@ We didn't access the object's attributes, like `hero.name`. We only accessed the
To confirm and understand how this **automatic expiration and refresh** of data when accessing attributes work, we can print some individual fields (instance attributes):
```Python hl_lines="21-23 26-28"
//// tab | Python 3.10+
```Python hl_lines="21-23 26-28"
# Code above omitted 👆
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py[ln:31-56]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="21-23 26-28"
# Code above omitted 👆
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py[ln:33-58]!}
@@ -236,12 +390,26 @@ To confirm and understand how this **automatic expiration and refresh** of data
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py!}
```
////
///
Now we are actually accessing the attributes, because instead of printing the whole object `hero_1`:
@@ -311,7 +479,6 @@ Hero 2 name: Spider-Boy
Hero 3 name: Rusty-Man
// Because the Session already refreshed these objects with all their data and the session knows they are not expired, it doesn't have to go again to the database for the names 🤓
```
</div>
@@ -324,7 +491,21 @@ But what if you want to **explicitly refresh** the data?
You can do that too with `session.refresh(object)`:
```Python hl_lines="30-32 35-37"
//// tab | Python 3.10+
```Python hl_lines="30-32 35-37"
# Code above omitted 👆
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py[ln:31-65]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="30-32 35-37"
# Code above omitted 👆
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py[ln:33-67]!}
@@ -332,12 +513,26 @@ You can do that too with `session.refresh(object)`:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py!}
```
////
///
When Python executes this code:
@@ -396,6 +591,20 @@ Now, as a final experiment, we can also print data after the **session** is clos
There are no surprises here, it still works:
//// tab | Python 3.10+
```Python hl_lines="40-42"
# Code above omitted 👆
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py[ln:31-70]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="40-42"
# Code above omitted 👆
@@ -404,12 +613,26 @@ There are no surprises here, it still works:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py!}
```
////
///
And the output shows again the same data:
@@ -445,12 +668,26 @@ And as we created the **engine** with `echo=True`, we can see the SQL statements
///
```{ .python .annotate }
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial002_py310.py!}
```
{!./docs_src/tutorial/automatic_id_none_refresh/annotations/en/tutorial002.md!}
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/automatic_id_none_refresh/tutorial002.py!}
```
{!./docs_src/tutorial/automatic_id_none_refresh/annotations/en/tutorial002.md!}
////
And here's all the output generated by running this program, all together:
<div class="termy">