Skip to content

Instantly share code, notes, and snippets.

@ramannanda9
Created December 3, 2024 22:22
Show Gist options
  • Select an option

  • Save ramannanda9/7bd7778182552e02d44a79a8f3283f51 to your computer and use it in GitHub Desktop.

Select an option

Save ramannanda9/7bd7778182552e02d44a79a8f3283f51 to your computer and use it in GitHub Desktop.
using environment for build
class EnvironmentConfig(BaseModel):
environment: str = "development"
@classmethod
def from_env(cls, explicit_env: Optional[str] = None):
"""
Create an instance of the config, prioritizing explicitly provided environment
over environment variable, with a default fallback.
"""
# Check for explicitly provided environment
if explicit_env:
return cls(environment=explicit_env)
# Try to read from environment variable
env_from_os = os.getenv('APP_ENVIRONMENT')
if env_from_os:
return cls(environment=env_from_os)
# Return default if no environment specified
return cls()
@field_validator('environment')
@classmethod
def validate_environment(cls, v: str):
"""
Validate the environment value
"""
valid_envs = ['development', 'staging', 'production']
if v.lower() not in valid_envs:
raise ValueError(f'Invalid environment. Must be one of {valid_envs}')
return v.lower()
# Example usage
class MyApplication:
def __init__(self, config: Optional[EnvironmentConfig] = None):
# If no config provided, create a default one
self.config = config or EnvironmentConfig.from_env()
# Now you can use the validated environment
print(f"Running in {self.config.environment} environment")
@ryandgoldenberg
Copy link

p.s. w.r.t. environment, the contract exposed to users is that:

  • they supply environment variables in Leicester via conduis portal env
  • those variables will be made available via the AbstractPipeline.get_app_env method

platform environment information such as bootstrap servers, etc, are not made available. I'm open to changing that if needed, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment