Machine Learning

Manage Environmental Variations for Pydantic


Introduction

Developers work in applications to be sent to a specific server to allow anyone to use those. Usually in the machine where these apps live, the Environmental Impacts Allows the app to work. These variety can be API extrvel keys, your data URL and much more.

With local advancement However, it is not just true to announce this variables on a machine because it is a slow and dirty process. So I would like to share in this short reading how I use the pydantic that is a safe manner in a safe way.

.Env file

What you usually do on the Python project to keep your environmental variables in a file called .env. This is a text file containing all variables in key : value format. You can also use one of the variables to declare one of the variables by installing the {} Syntax.

The following is an example:

#.env file

OPENAI_API_KEY="sk-your private key"
OPENAI_MODEL_ID="gpt-4o-mini"

# Development settings
DOMAIN=example.org
ADMIN_EMAIL=admin@${DOMAIN}

WANDB_API_KEY="your-private-key"
WANDB_PROJECT="myproject"
WANDB_ENTITY="my-entity"

SERPAPI_KEY= "your-api-key"
PERPLEXITY_TOKEN = "your-api-token"

Note the .NV file should remain confidential, so it is important that this file is addressed .gitignore File, to make sure you Never push it to GithubBesides, some engineers can steal your keys and use the tools you paid.

EMPV.Expple File

To relieve the health of developers who will include your last location, you can enter an Ev.examp file on your project. This is a file that consists of only locks what you should come into .env file. In this way, some people know which APIs, tokens, or secrecy often need to set up the printing text.

#env.example

OPENAI_API_KEY=""
OPENAI_MODEL_ID=""

DOMAIN=""
ADMIN_EMAIL=""

WANDB_API_KEY=""
WANDB_PROJECT=""
WANDB_ENTITY=""

SERPAPI_KEY= ""
PERPLEXITY_TOKEN = ""

Python-Dotenv is a library you use variable to load the .iv file. To enter this library:

pip install python-dotenv

Now you can use the Load_Dotenv loading. Then find reference to these variables with OS Module.

import os
from dotenv import load_dotenv

load_dotenv()

OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
OPENAI_MODEL_ID = os.getenv('OPENAI_MODEL_ID')

This method will start to look into your file .NIV file the variable loaded out there. If this file is not available, flexibility will be taken from the host. This means you can use the .NV file with your local development but then the code is sent to the hosting environment such as a visible machine as a vocal machine or a docket container.

The Widiles

Pydantic is one of the most widely used libraries in Python for data verification. It also is used to separate classes in JSON and back. Generate JSon Sconema, reduce the need for manual schema management. It also provides a built-in data verification, to ensure that serierized data attaches to the expected format. Finally, it is easily met with popular web structures like Fastapi.

Pydantic settings Is the Pydantic feature required to load and verify settings or editing classes from the environment.

!pip install pydantic-settings

We will build a combined class Settings. This section will inherit BaseSettings. This makes the default behavior to find the prices of any fields that will be read from .env file. If no VAR is found in the .iV file will be used the default value when provided.

from pydantic_settings import BaseSettings, SettingsConfigDict

from pydantic import (
    AliasChoices,
    Field,
    RedisDsn,
)


class Settings(BaseSettings):
    auth_key: str = Field(validation_alias="my_auth_key")  
    api_key: str = Field(alias="my_api_key")  

    redis_dsn: RedisDsn = Field(
        'redis://user:pass@localhost:6379/1', #default value
        validation_alias=AliasChoices('service_redis_dsn', 'redis_url'),  
    )

    model_config = SettingsConfigDict(env_prefix='my_prefix_')

In Settings The class above described many fields. This page Field The class is used to provide additional information about the attribute.

In our case, we set up a validation_alias. So the only variable name to look at .env file is full. In the article considers above, the natural variable my_auth_key will be read instead of Athor_key.

You can have multiple aliases to view in .env file that can be specified with leverging AliasChoises(choise1, choise2).

Last quality model_config It contains all flexible in respect of a particular topic (eg DB connection). And this variable will keep all .env var starts with the beginning env_prefix.

The settings are available and use

The next step will actually exist when using these settings to your Python project.

from pydantic_settings import BaseSettings, SettingsConfigDict

from pydantic import (
    AliasChoices,
    Field,
    RedisDsn,
)


class Settings(BaseSettings):
    auth_key: str = Field(validation_alias="my_auth_key")  
    api_key: str = Field(alias="my_api_key")  

    redis_dsn: RedisDsn = Field(
        'redis://user:pass@localhost:6379/1', #default value
        validation_alias=AliasChoices('service_redis_dsn', 'redis_url'),  
    )

    model_config = SettingsConfigDict(env_prefix='my_prefix_')

# create immediately a settings object
settings = Settings()

Now use settings in some parts of our code code.

from Settings import settings

print(settings.auth_key) 

Finally you have simple access to your settings, and the Pydantic helps you make sure that the secrets have the correct format. To get developed verification tips against Eydantic documents:

The last thoughts

Managing project configuration is a boring part of the software development. Secrets like API keys, DB connection is what often enables your request. With Zi-Maily you can combine this variables in your code and is still working, but for obvious reasons, this cannot be a good habit. In this article, I showed you the introduction to how to use the Pydantic settings to have a formal and safe way to manage your configuration.

💼 Lickimin ️ | 🐦 x (Twitter) | 💻 website

Source link

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button