You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

11 KiB

⚒ & 🌐 🔢

📚 💼 👆 🈸 💪 💪 🔢⚖️ 📳, 🖼 ㊙ 🔑, 💽 🎓, 🎓 📧 🐕‍🦺, ️.

🏆 👫🔢 (💪 🔀), 💖 💽 📛. & 📚 💪 🚿, 💖 ㊙.

👉 🤔 ️ ⚠ 🚚 👫 🌐 🔢 👈🈸.

🌐 🔢

!!! tip 🚥 👆 💭 "🌐 🔢" & ⚙️ 👫, 💭 🆓 🚶📄 🔛.

🌐 🔢 (💭 "🇨🇻 {") 🔢 👈 🖖 🏞 🐍 📟, 🏃‍♂ ⚙️, & 💪👆 🐍 📟 (⚖️ 🎏 📋 👍).

👆 💪 ✍ & ⚙️ 🌐 🔢 🐚, 🍵 💆‍♂ 🐍:

=== "💾, 🇸🇻, 🚪 🎉"

<div class="termy">

```console
// You could create an env var MY_NAME with
$ export MY_NAME="Wade Wilson"

// Then you could use it with other programs, like
$ echo "Hello $MY_NAME"

Hello Wade Wilson
```

</div>

=== "🚪 📋"

<div class="termy">

```console
// Create an env var MY_NAME
$ $Env:MY_NAME = "Wade Wilson"

// Use it with other programs, like
$ echo "Hello $Env:MY_NAME"

Hello Wade Wilson
```

</div>

🇨🇻 {🐍

👆 💪🌐 🔢 🏞 🐍, 📶 (⚖️ ⏮️ 🙆 🎏 👩‍🔬), & ⤴️👫 🐍.

🖼 👆 💪 ✔️ 📁 main.py ⏮️:

import os

name = os.getenv("MY_NAME", "World")
print(f"Hello {name} from Python")

!!! tip 🥈 os.getenv() 🔢 💲 📨.

🚥 🚫 🚚, ⚫️ `None` 🔢, 📥 👥 🚚 `"World"` 🔢 💲 ⚙️.

⤴️ 👆 💪 🤙 👈 🐍 📋:

// Here we don't set the env var yet
$ python main.py

// As we didn't set the env var, we get the default value

Hello World from Python

// But if we create an environment variable first
$ export MY_NAME="Wade Wilson"

// And then call the program again
$ python main.py

// Now it can read the environment variable

Hello Wade Wilson from Python

🌐 🔢 💪 ⚒ 🏞 📟, 💪📟, & 🚫 ✔️ 🏪 (💕 git) ⏮️ 🎂 📁, ️ ⚠ ⚙️ 👫 📳 ⚖️ ⚒.

👆 💪🌐 🔢 🕴 🎯 📋 👼, 👈 🕴 💪 👈 📋, & 🕴 🚮 📐.

👈, ✍ ▶️️ ⏭ 📋 ️, 🔛 🎏 ⏸:

// Create an env var MY_NAME in line for this program call
$ MY_NAME="Wade Wilson" python main.py

// Now it can read the environment variable

Hello Wade Wilson from Python

// The env var no longer exists afterwards
$ python main.py

Hello World from Python

!!! tip 👆 💪🌅 🔃 1️⃣2️⃣-⚖ 📱: 📁.

🆎 & 🔬

👫 🌐 🔢 💪 🕴 🍵🎻, 👫 🔢 🐍 & ✔️ 🔗 ⏮️ 🎏 📋 & 🎂 ⚙️ (& ⏮️ 🎏 🏃‍♂ ⚙️, 💾, 🚪, 🇸🇻).

👈👈 🙆 💲🐍 ➡️ 🌐 🔢 🔜 str, & 🙆 🛠️ 🎏 🆎 ⚖️ 🔬 ✔️ 🔨 📟.

Pydantic Settings

👐, Pydantic 🚚 👑 🚙 🍵 👫👟 ➡️ 🌐 🔢 ⏮️ Pydantic: ⚒ 🧾.

Settings 🎚

🗄 BaseSettings ➡️ Pydantic & ✍ 🎧-🎓, 📶 🌅 💖 ⏮️ Pydantic 🏷.

🎏 🌌 ⏮️ Pydantic 🏷, 👆 📣 🎓 🔢 ⏮️ 🆎 ✍, & 🎲 🔢 💲.

👆 💪 ⚙️ 🌐 🎏 🔬 ⚒ & 🧰 👆 ⚙️ Pydantic 🏷, 💖 🎏 📊 🆎 & 🌖 🔬 ⏮️ Field().

{!../../../docs_src/settings/tutorial001.py!}

!!! tip 🚥 👆 💚 🕳 📁 & 📋, 🚫 ⚙️ 👉 🖼, ⚙️ 🏁 1️⃣ 🔛.

⤴️, 🕐 👆👐 👈 Settings 🎓 (👉 💼, settings 🎚), Pydantic 🔜🌐 🔢 💼-😛 🌌,, ↖-💼 🔢 APP_NAME 🔜🔢 app_name.

🔜 🗜 & ✔ 💽. , 🕐 👆 ⚙️ 👈 settings 🎚, 👆 🔜 ✔️ 📊 🆎 👆 📣 ( items_per_user 🔜 int).

⚙️ settings

⤴️ 👆 💪 ⚙️ 🆕 settings 🎚 👆 🈸:

{!../../../docs_src/settings/tutorial001.py!}

🏃 💽

⏭, 👆 🔜 🏃 💽 🚶‍♀️ 📳 🌐 🔢, 🖼 👆 💪ADMIN_EMAIL & APP_NAME ⏮️:

$ ADMIN_EMAIL="[email protected]" APP_NAME="ChimichangApp" uvicorn main:app

<span style="color: green;">INFO</span>:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

!!! tip ⚒ 💗 🇨🇻 {👁 📋 🎏 👫 ⏮️ 🚀, & 🚮 👫 🌐📋.

& ⤴️ admin_email🔜"[email protected]".

app_name 🔜 "ChimichangApp".

& items_per_user 🔜 🚧 🚮 🔢 💲 50.

1️⃣ 🕹

👆 💪 🚮 👈1️⃣ 🕹 📁 👆 👀 🦏 🈸 - 💗 📁{.internal-link target=_blank}.

🖼, 👆 💪 ✔️ 📁 config.py ⏮️:

{!../../../docs_src/settings/app01/config.py!}

& ⤴️ ⚙️ 📁 main.py:

{!../../../docs_src/settings/app01/main.py!}

!!! tip 👆 🔜 💪 📁 __init__.py 👆 👀 🔛 🦏 🈸 - 💗 📁{.internal-link target=_blank}.

🔗

🍾 5️⃣📆🚚➡️ 🔗, ↩️ ✔️ 🌐 🎚 ⏮️ settings 👈 ⚙️ 🌐.

👉 💪 ✴️⏮️ 🔬, 📶 🔐 🔗 ⏮️ 👆 👍 🛃 ⚒.

📁 📁

👟 ➡️ ⏮️ 🖼, 👆 config.py 📁 💪 👀 💖:

{!../../../docs_src/settings/app02/config.py!}

👀 👈 🔜 👥 🚫🔢 👐 settings = Settings().

👑 📱 📁

🔜 👥🔗 👈 📨 🆕 config.Settings().

{!../../../docs_src/settings/app02/main.py!}

!!! tip 👥 🔜 🔬 @lru_cache() 🍖.

🔜 👆 💪 🤔 `get_settings()` 😐 🔢.

& ⤴️ 👥 💪 🚚 ➡️ 🛠️ 🔢 🔗 & ⚙️ 🙆 👥 💪 ️.

{!../../../docs_src/settings/app02/main.py!}

⚒ & 🔬

⤴️ 🔜 📶 🚚 🎏 ⚒ 🎚 ⏮️ 🔬 🏗 🔗 🔐 get_settings:

{!../../../docs_src/settings/app02/test_main.py!}

🔗 🔐 👥🆕 💲 admin_email 🕐 🏗 🆕 Settings 🎚, & ⤴️ 👥 📨 👈 🆕 🎚.

⤴️ 👥 💪 💯 👈 ⚙️.

👂 .env 📁

🚥 👆 ✔️ 📚👈 🎲 🔀 📚, 🎲 🎏 🌐, 5️⃣📆🚮 👫 🔛 📁 & ⤴️👫 ➡️ 🚥 👫 🌐 🔢.

👉 💡🥃 👈 ✔️ 📛, 👫 🌐 🔢 🛎 🥉 📁 .env, & 📁 🤙 "🇨🇻".

!!! tip 📁 ▶️ ⏮️ ❣ (.) 🕵‍♂ 📁 🖥-💖 ⚙️, 💖 💾 & 🇸🇻.

✋️ 🇨🇻 📁 🚫 🤙 ✔️ ✔️ 👈 ☑ 📁.

Pydantic ✔️ 🐕‍🦺 👂 ➡️ 👉 🆎 📁 ⚙️ 🔢 🗃. 👆 💪🌖 Pydantic ⚒: 🇨🇻 (.🇨🇻) 🐕‍🦺.

!!! tip 👉 👷, 👆 💪 pip install python-dotenv.

.env 📁

👆 💪 ✔️ .env 📁 ⏮️:

ADMIN_EMAIL="[email protected]"
APP_NAME="ChimichangApp"

✍ ⚒ ➡️ .env

& ⤴️👆 config.py ⏮️:

{!../../../docs_src/settings/app03/config.py!}

📥 👥🎓 Config 🔘 👆 Pydantic Settings 🎓, & ⚒ env_file 📁 ⏮️ 🇨🇻 📁 👥 💚 ⚙️.

!!! tip Config 🎓 ⚙️ Pydantic 📳. 👆 💪🌖 Pydantic 🏷 📁

🏗 Settings 🕴 🕐 ⏮️ lru_cache

👂 📁 ➡️ 💾 🛎 ⚠ (🐌) 🛠️, 👆 🎲 💚 ️ 🕴 🕐 & ⤴️ 🏤-⚙️ 🎏 ⚒ 🎚, ↩️ 👂 🔠 📨.

🔠 🕰 👥:

Settings()

🆕 Settings 🎚 🔜 ✍, & 🏗 🔜.env 📁 🔄.

🚥 🔗 🔢 💖:

def get_settings():
    return Settings()

👥 🔜👈 🎚 🔠 📨, & 👥 🔜 👂 .env 📁 🔠 📨. 👶 👶

👥 ⚙️ @lru_cache() 👨‍🎨 🔛 🔝, Settings 🎚 🔜 ✍ 🕴 🕐, 🥇 🕰 🤙. 👶 👶

{!../../../docs_src/settings/app03/main.py!}

⤴️ 🙆 🏁 🤙 get_settings() 🔗📨, ↩️ 🛠️ 🔗 📟 get_settings() & 🏗 🆕 Settings 🎚, 🔜 📨 🎏 🎚 👈 📨 🔛 🥇 🤙, 🔄 & 🔄.

lru_cache 📡

@lru_cache() 🔀 🔢 🎀 📨 🎏 💲 👈 📨 🥇 🕰, ↩️ 💻 🔄, 🛠️ 📟 🔢 🔠 🕰.

, 🔢 🔛 🔜 🛠️ 🕐 🔠 🌀 . & ⤴️ 💲 📨 🔠 👈 🌀 🔜 ⚙️ 🔄 & 🔄 🕐 🔢 🤙 ⏮️ 🎏 🌀 .

🖼, 🚥 👆 ✔️ 🔢:

@lru_cache()
def say_hi(name: str, salutation: str = "Ms."):
    return f"Hello {salutation} {name}"

👆 📋 💪 🛠️ 💖 👉:

sequenceDiagram

participant code as Code
participant function as say_hi()
participant execute as Execute function

    rect rgba(0, 255, 0, .1)
        code ->> function: say_hi(name="Camila")
        function ->> execute: execute function code
        execute ->> code: return the result
    end

    rect rgba(0, 255, 255, .1)
        code ->> function: say_hi(name="Camila")
        function ->> code: return stored result
    end

    rect rgba(0, 255, 0, .1)
        code ->> function: say_hi(name="Rick")
        function ->> execute: execute function code
        execute ->> code: return the result
    end

    rect rgba(0, 255, 0, .1)
        code ->> function: say_hi(name="Rick", salutation="Mr.")
        function ->> execute: execute function code
        execute ->> code: return the result
    end

    rect rgba(0, 255, 255, .1)
        code ->> function: say_hi(name="Rick")
        function ->> code: return stored result
    end

    rect rgba(0, 255, 255, .1)
        code ->> function: say_hi(name="Camila")
        function ->> code: return stored result
    end

💼 👆 🔗 get_settings(), 🔢 🚫 🙆 , 🕧 📨 🎏 💲.

👈 🌌, 🎭 🌖 🚥 🌐 🔢. ⚙️ 🔗 🔢, ⤴️ 👥 💪 🔐 💪 🔬.

@lru_cache() 🍕 functools 🍕 🐍 🐩 🗃, 👆 💪🌅 🔃 🐍 🩺 @lru_cache().

🌃

👆 💪 ⚙️ Pydantic ⚒ 🍵⚖️ 📳 👆 🈸, ⏮️ 🌐 🏋️ Pydantic 🏷.

  • ⚙️ 🔗 👆 💪 📉 🔬.
  • 👆 💪 ⚙️ .env 📁 ⏮️ ️.
  • ⚙️ @lru_cache() ➡️ 👆 👂 🇨🇻 📁 🔄 & 🔄 🔠 📨, 🤝 👆 🔐 ⏮️ 🔬.