Skip to content

Instantly share code, notes, and snippets.

@anandtripathi5
Created February 5, 2022 18:13
Show Gist options
  • Save anandtripathi5/1ae8340718b77468dd0f987740e1ab6d to your computer and use it in GitHub Desktop.
Save anandtripathi5/1ae8340718b77468dd0f987740e1ab6d to your computer and use it in GitHub Desktop.
Pydantic usage with python-dotenv
import os
from typing import Union
from pydantic import BaseSettings, Field
class Base(BaseSettings):
secret_key: str = Field('random_string', env='ANOTHER_SECRET_KEY')
port: int = 5050
username: str = "ANAND"
class Config:
case_sensitive = False
env_file = '.env'
class Dev(Base):
username = "TRIPATHI"
class Config:
env_file = 'dev.env'
class Prod(Base):
username = "Production"
port = 5051
class Config:
env_file = 'prod.env'
config = dict(
dev=Dev,
prod=Prod
)
settings: Union[Dev, Prod] = config[os.environ.get('ENV', 'dev').lower()]()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment