Skip to content

โš’ & ๐ŸŒ ๐Ÿ”ข

๐Ÿ“š ๐Ÿ’ผ ๐Ÿ‘† ๐Ÿˆธ ๐Ÿ’ช ๐Ÿ’ช ๐Ÿ”ข โš’ โš–๏ธ ๐Ÿ“ณ, ๐Ÿ–ผ ใŠ™ ๐Ÿ”‘, ๐Ÿ’ฝ ๐ŸŽ“, ๐ŸŽ“ ๐Ÿ“ง ๐Ÿ•โ€๐Ÿฆบ, โ™’๏ธ.

๐Ÿ† ๐Ÿ‘ซ โš’ ๐Ÿ”ข (๐Ÿ’ช ๐Ÿ”€), ๐Ÿ’– ๐Ÿ’ฝ ๐Ÿ“›. & ๐Ÿ“š ๐Ÿ’ช ๐Ÿšฟ, ๐Ÿ’– ใŠ™.

๐Ÿ‘‰ ๐Ÿค” โšซ๏ธ โš  ๐Ÿšš ๐Ÿ‘ซ ๐ŸŒ ๐Ÿ”ข ๐Ÿ‘ˆ โœ ๐Ÿˆธ.

๐ŸŒ ๐Ÿ”ข

Tip

๐Ÿšฅ ๐Ÿ‘† โช ๐Ÿ’ญ โšซ๏ธโ” "๐ŸŒ ๐Ÿ”ข" & โ” โš™๏ธ ๐Ÿ‘ซ, ๐Ÿ’ญ ๐Ÿ†“ ๐Ÿšถ โญ ๐Ÿ“„ ๐Ÿ”›.

๐ŸŒ ๐Ÿ”ข (๐Ÿ’ญ "๐Ÿ‡จ๐Ÿ‡ป {") ๐Ÿ”ข ๐Ÿ‘ˆ ๐Ÿ–– ๐Ÿž ๐Ÿ ๐Ÿ“Ÿ, ๐Ÿƒโ€โ™‚ โš™๏ธ, & ๐Ÿ’ช โœ ๐Ÿ‘† ๐Ÿ ๐Ÿ“Ÿ (โš–๏ธ ๐ŸŽ ๐Ÿ“‹ ๐Ÿ‘).

๐Ÿ‘† ๐Ÿ’ช โœ & โš™๏ธ ๐ŸŒ ๐Ÿ”ข ๐Ÿš, ๐Ÿต ๐Ÿ’†โ€โ™‚ ๐Ÿ:

// 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
// 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

โœ ๐Ÿ‡จ๐Ÿ‡ป {๐Ÿ

๐Ÿ‘† ๐Ÿ’ช โœ ๐ŸŒ ๐Ÿ”ข ๐Ÿž ๐Ÿ, ๐Ÿ“ถ (โš–๏ธ โฎ๏ธ ๐Ÿ™† ๐ŸŽ ๐Ÿ‘ฉโ€๐Ÿ”ฌ), & โคด๏ธ โœ ๐Ÿ‘ซ ๐Ÿ.

๐Ÿ–ผ ๐Ÿ‘† ๐Ÿ’ช โœ”๏ธ ๐Ÿ“ 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().

from fastapi import FastAPI
from pydantic_settings import BaseSettings


class Settings(BaseSettings):
    app_name: str = "Awesome API"
    admin_email: str
    items_per_user: int = 50


settings = Settings()
app = FastAPI()


@app.get("/info")
async def info():
    return {
        "app_name": settings.app_name,
        "admin_email": settings.admin_email,
        "items_per_user": settings.items_per_user,
    }

Tip

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ’š ๐Ÿ•ณ โฉ ๐Ÿ“ & ๐Ÿ“‹, ๐Ÿšซ โš™๏ธ ๐Ÿ‘‰ ๐Ÿ–ผ, โš™๏ธ ๐Ÿ 1๏ธโƒฃ ๐Ÿ”›.

โคด๏ธ, ๐Ÿ•โ” ๐Ÿ‘† โœ ๐Ÿ‘ ๐Ÿ‘ˆ Settings ๐ŸŽ“ (๐Ÿ‘‰ ๐Ÿ’ผ, settings ๐ŸŽš), Pydantic ๐Ÿ”œ โœ ๐ŸŒ ๐Ÿ”ข ๐Ÿ’ผ-๐Ÿ˜› ๐ŸŒŒ,, โ†–-๐Ÿ’ผ ๐Ÿ”ข APP_NAME ๐Ÿ”œ โœ ๐Ÿ”ข app_name.

โญ โšซ๏ธ ๐Ÿ”œ ๐Ÿ—œ & โœ” ๐Ÿ’ฝ. , ๐Ÿ•โ” ๐Ÿ‘† โš™๏ธ ๐Ÿ‘ˆ settings ๐ŸŽš, ๐Ÿ‘† ๐Ÿ”œ โœ”๏ธ ๐Ÿ“Š ๐Ÿ†Ž ๐Ÿ‘† ๐Ÿ“ฃ (โœ… items_per_user ๐Ÿ”œ int).

โš™๏ธ settings

โคด๏ธ ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐Ÿ†• settings ๐ŸŽš ๐Ÿ‘† ๐Ÿˆธ:

from fastapi import FastAPI
from pydantic_settings import BaseSettings


class Settings(BaseSettings):
    app_name: str = "Awesome API"
    admin_email: str
    items_per_user: int = 50


settings = Settings()
app = FastAPI()


@app.get("/info")
async def info():
    return {
        "app_name": settings.app_name,
        "admin_email": settings.admin_email,
        "items_per_user": settings.items_per_user,
    }

๐Ÿƒ ๐Ÿ’ฝ

โญ, ๐Ÿ‘† ๐Ÿ”œ ๐Ÿƒ ๐Ÿ’ฝ ๐Ÿšถโ€โ™€๏ธ ๐Ÿ“ณ ๐ŸŒ ๐Ÿ”ข, ๐Ÿ–ผ ๐Ÿ‘† ๐Ÿ’ช โš’ ADMIN_EMAIL & APP_NAME โฎ๏ธ:

$ ADMIN_EMAIL="deadpool@example.com" 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 โš’ ๐Ÿ”œ โš’ "deadpool@example.com".

app_name ๐Ÿ”œ "ChimichangApp".

& items_per_user ๐Ÿ”œ ๐Ÿšง ๐Ÿšฎ ๐Ÿ”ข ๐Ÿ’ฒ 50.

โš’ โž•1๏ธโƒฃ ๐Ÿ•น

๐Ÿ‘† ๐Ÿ’ช ๐Ÿšฎ ๐Ÿ‘ˆ โš’ โž•1๏ธโƒฃ ๐Ÿ•น ๐Ÿ“ ๐Ÿ‘† ๐Ÿ‘€ ๐Ÿฆ ๐Ÿˆธ - ๐Ÿ’— ๐Ÿ“.

๐Ÿ–ผ, ๐Ÿ‘† ๐Ÿ’ช โœ”๏ธ ๐Ÿ“ config.py โฎ๏ธ:

from pydantic_settings import BaseSettings


class Settings(BaseSettings):
    app_name: str = "Awesome API"
    admin_email: str
    items_per_user: int = 50


settings = Settings()

& โคด๏ธ โš™๏ธ โšซ๏ธ ๐Ÿ“ main.py:

from fastapi import FastAPI

from .config import settings

app = FastAPI()


@app.get("/info")
async def info():
    return {
        "app_name": settings.app_name,
        "admin_email": settings.admin_email,
        "items_per_user": settings.items_per_user,
    }

Tip

๐Ÿ‘† ๐Ÿ”œ ๐Ÿ’ช ๐Ÿ“ __init__.py ๐Ÿ‘† ๐Ÿ‘€ ๐Ÿ”› ๐Ÿฆ ๐Ÿˆธ - ๐Ÿ’— ๐Ÿ“.

โš’ ๐Ÿ”—

๐Ÿพ โšซ๏ธ 5๏ธโƒฃ๐Ÿ“† โš  ๐Ÿšš โš’ โšช๏ธโžก๏ธ ๐Ÿ”—, โ†ฉ๏ธ โœ”๏ธ ๐ŸŒ ๐ŸŽš โฎ๏ธ settings ๐Ÿ‘ˆ โš™๏ธ ๐ŸŒ.

๐Ÿ‘‰ ๐Ÿ’ช โœด๏ธ โš  โฎ๏ธ ๐Ÿ”ฌ, โšซ๏ธ ๐Ÿ“ถ โฉ ๐Ÿ” ๐Ÿ”— โฎ๏ธ ๐Ÿ‘† ๐Ÿ‘ ๐Ÿ›ƒ โš’.

๐Ÿ“ ๐Ÿ“

๐Ÿ‘Ÿ โšช๏ธโžก๏ธ โฎ๏ธ ๐Ÿ–ผ, ๐Ÿ‘† config.py ๐Ÿ“ ๐Ÿ’ช ๐Ÿ‘€ ๐Ÿ’–:

from pydantic_settings import BaseSettings


class Settings(BaseSettings):
    app_name: str = "Awesome API"
    admin_email: str
    items_per_user: int = 50

๐Ÿ‘€ ๐Ÿ‘ˆ ๐Ÿ”œ ๐Ÿ‘ฅ ๐Ÿšซ โœ ๐Ÿ”ข ๐Ÿ‘ settings = Settings().

๐Ÿ‘‘ ๐Ÿ“ฑ ๐Ÿ“

๐Ÿ”œ ๐Ÿ‘ฅ โœ ๐Ÿ”— ๐Ÿ‘ˆ ๐Ÿ“จ ๐Ÿ†• config.Settings().

from functools import lru_cache

from fastapi import Depends, FastAPI

from .config import Settings

app = FastAPI()


@lru_cache()
def get_settings():
    return Settings()


@app.get("/info")
async def info(settings: Settings = Depends(get_settings)):
    return {
        "app_name": settings.app_name,
        "admin_email": settings.admin_email,
        "items_per_user": settings.items_per_user,
    }

Tip

๐Ÿ‘ฅ ๐Ÿ”œ ๐Ÿ”ฌ @lru_cache() ๐Ÿ–.

๐Ÿ”œ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿค” get_settings() ๐Ÿ˜ ๐Ÿ”ข.

& โคด๏ธ ๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿšš โšซ๏ธ โšช๏ธโžก๏ธ โžก ๐Ÿ› ๏ธ ๐Ÿ”ข ๐Ÿ”— & โš™๏ธ โšซ๏ธ ๐Ÿ™† ๐Ÿ‘ฅ ๐Ÿ’ช โšซ๏ธ.

from functools import lru_cache

from fastapi import Depends, FastAPI

from .config import Settings

app = FastAPI()


@lru_cache()
def get_settings():
    return Settings()


@app.get("/info")
async def info(settings: Settings = Depends(get_settings)):
    return {
        "app_name": settings.app_name,
        "admin_email": settings.admin_email,
        "items_per_user": settings.items_per_user,
    }

โš’ & ๐Ÿ”ฌ

โคด๏ธ โšซ๏ธ ๐Ÿ”œ ๐Ÿ“ถ โฉ ๐Ÿšš ๐ŸŽ โš’ ๐ŸŽš โฎ๏ธ ๐Ÿ”ฌ ๐Ÿ— ๐Ÿ”— ๐Ÿ” get_settings:

from fastapi.testclient import TestClient

from .config import Settings
from .main import app, get_settings

client = TestClient(app)


def get_settings_override():
    return Settings(admin_email="testing_admin@example.com")


app.dependency_overrides[get_settings] = get_settings_override


def test_app():
    response = client.get("/info")
    data = response.json()
    assert data == {
        "app_name": "Awesome API",
        "admin_email": "testing_admin@example.com",
        "items_per_user": 50,
    }

๐Ÿ”— ๐Ÿ” ๐Ÿ‘ฅ โš’ ๐Ÿ†• ๐Ÿ’ฒ admin_email ๐Ÿ•โ” ๐Ÿ— ๐Ÿ†• Settings ๐ŸŽš, & โคด๏ธ ๐Ÿ‘ฅ ๐Ÿ“จ ๐Ÿ‘ˆ ๐Ÿ†• ๐ŸŽš.

โคด๏ธ ๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿ’ฏ ๐Ÿ‘ˆ โšซ๏ธ โš™๏ธ.

๐Ÿ‘‚ .env ๐Ÿ“

๐Ÿšฅ ๐Ÿ‘† โœ”๏ธ ๐Ÿ“š โš’ ๐Ÿ‘ˆ ๐ŸŽฒ ๐Ÿ”€ ๐Ÿ“š, ๐ŸŽฒ ๐ŸŽ ๐ŸŒ, โšซ๏ธ 5๏ธโƒฃ๐Ÿ“† โš  ๐Ÿšฎ ๐Ÿ‘ซ ๐Ÿ”› ๐Ÿ“ & โคด๏ธ โœ ๐Ÿ‘ซ โšช๏ธโžก๏ธ โšซ๏ธ ๐Ÿšฅ ๐Ÿ‘ซ ๐ŸŒ ๐Ÿ”ข.

๐Ÿ‘‰ ๐Ÿ’ก โš  ๐Ÿฅƒ ๐Ÿ‘ˆ โšซ๏ธ โœ”๏ธ ๐Ÿ“›, ๐Ÿ‘ซ ๐ŸŒ ๐Ÿ”ข ๐Ÿ›Ž ๐Ÿฅ‰ ๐Ÿ“ .env, & ๐Ÿ“ ๐Ÿค™ "๐Ÿ‡จ๐Ÿ‡ป".

Tip

๐Ÿ“ โ–ถ๏ธ โฎ๏ธ โฃ (.) ๐Ÿ•ตโ€โ™‚ ๐Ÿ“ ๐Ÿ–ฅ-๐Ÿ’– โš™๏ธ, ๐Ÿ’– ๐Ÿ’พ & ๐Ÿ‡ธ๐Ÿ‡ป.

โœ‹๏ธ ๐Ÿ‡จ๐Ÿ‡ป ๐Ÿ“ ๐Ÿšซ ๐Ÿค™ โœ”๏ธ โœ”๏ธ ๐Ÿ‘ˆ โ˜‘ ๐Ÿ“.

Pydantic โœ”๏ธ ๐Ÿ•โ€๐Ÿฆบ ๐Ÿ‘‚ โšช๏ธโžก๏ธ ๐Ÿ‘‰ ๐Ÿ†Ž ๐Ÿ“ โš™๏ธ ๐Ÿ”ข ๐Ÿ—ƒ. ๐Ÿ‘† ๐Ÿ’ช โœ ๐ŸŒ– Pydantic โš’: ๐Ÿ‡จ๐Ÿ‡ป (.๐Ÿ‡จ๐Ÿ‡ป) ๐Ÿ•โ€๐Ÿฆบ.

Tip

๐Ÿ‘‰ ๐Ÿ‘ท, ๐Ÿ‘† ๐Ÿ’ช pip install python-dotenv.

.env ๐Ÿ“

๐Ÿ‘† ๐Ÿ’ช โœ”๏ธ .env ๐Ÿ“ โฎ๏ธ:

ADMIN_EMAIL="deadpool@example.com"
APP_NAME="ChimichangApp"

โœ โš’ โšช๏ธโžก๏ธ .env

& โคด๏ธ โ„น ๐Ÿ‘† config.py โฎ๏ธ:

from pydantic_settings import BaseSettings


class Settings(BaseSettings):
    app_name: str = "Awesome API"
    admin_email: str
    items_per_user: int = 50

    class Config:
        env_file = ".env"

๐Ÿ“ฅ ๐Ÿ‘ฅ โœ ๐ŸŽ“ Config ๐Ÿ”˜ ๐Ÿ‘† Pydantic Settings ๐ŸŽ“, & โš’ env_file ๐Ÿ“ โฎ๏ธ ๐Ÿ‡จ๐Ÿ‡ป ๐Ÿ“ ๐Ÿ‘ฅ ๐Ÿ’š โš™๏ธ.

Tip

Config ๐ŸŽ“ โš™๏ธ Pydantic ๐Ÿ“ณ. ๐Ÿ‘† ๐Ÿ’ช โœ ๐ŸŒ– Pydantic ๐Ÿท ๐Ÿ“

๐Ÿ— Settings ๐Ÿ•ด ๐Ÿ• โฎ๏ธ lru_cache

๐Ÿ‘‚ ๐Ÿ“ โšช๏ธโžก๏ธ ๐Ÿ’พ ๐Ÿ›Ž โš  (๐ŸŒ) ๐Ÿ› ๏ธ, ๐Ÿ‘† ๐ŸŽฒ ๐Ÿ’š โšซ๏ธ ๐Ÿ•ด ๐Ÿ• & โคด๏ธ ๐Ÿค-โš™๏ธ ๐ŸŽ โš’ ๐ŸŽš, โ†ฉ๏ธ ๐Ÿ‘‚ โšซ๏ธ ๐Ÿ”  ๐Ÿ“จ.

โœ‹๏ธ ๐Ÿ”  ๐Ÿ•ฐ ๐Ÿ‘ฅ:

Settings()

๐Ÿ†• Settings ๐ŸŽš ๐Ÿ”œ โœ, & ๐Ÿ— โšซ๏ธ ๐Ÿ”œ โœ .env ๐Ÿ“ ๐Ÿ”„.

๐Ÿšฅ ๐Ÿ”— ๐Ÿ”ข ๐Ÿ’–:

def get_settings():
    return Settings()

๐Ÿ‘ฅ ๐Ÿ”œ โœ ๐Ÿ‘ˆ ๐ŸŽš ๐Ÿ”  ๐Ÿ“จ, & ๐Ÿ‘ฅ ๐Ÿ”œ ๐Ÿ‘‚ .env ๐Ÿ“ ๐Ÿ”  ๐Ÿ“จ. ๐Ÿ‘ถ ๐Ÿ‘ถ

โœ‹๏ธ ๐Ÿ‘ฅ โš™๏ธ @lru_cache() ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ”› ๐Ÿ”, Settings ๐ŸŽš ๐Ÿ”œ โœ ๐Ÿ•ด ๐Ÿ•, ๐Ÿฅ‡ ๐Ÿ•ฐ โšซ๏ธ ๐Ÿค™. ๐Ÿ‘ถ ๐Ÿ‘ถ

from functools import lru_cache

from fastapi import Depends, FastAPI

from . import config

app = FastAPI()


@lru_cache()
def get_settings():
    return config.Settings()


@app.get("/info")
async def info(settings: config.Settings = Depends(get_settings)):
    return {
        "app_name": settings.app_name,
        "admin_email": settings.admin_email,
        "items_per_user": settings.items_per_user,
    }

โคด๏ธ ๐Ÿ™† ๐Ÿ ๐Ÿค™ 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() โžก๏ธ ๐Ÿ‘† โŽ ๐Ÿ‘‚ ๐Ÿ‡จ๐Ÿ‡ป ๐Ÿ“ ๐Ÿ”„ & ๐Ÿ”„ ๐Ÿ”  ๐Ÿ“จ, โช ๐Ÿค ๐Ÿ‘† ๐Ÿ” โšซ๏ธ โฎ๏ธ ๐Ÿ”ฌ.