class Config
Configuration class that manages application-wide settings, directory structures, API keys, and operational parameters for a statistical analysis application.
/tf/active/vicechatdev/vice_ai/smartstat_config.py
9 - 92
moderate
Purpose
The Config class serves as a centralized configuration management system for a Flask-based statistical analysis application. It defines all necessary settings including Flask configuration, file system paths, database connections, LLM API credentials, statistical analysis parameters, and cleanup policies. The class automatically creates required directories upon instantiation and provides a single source of truth for all application configuration values.
Source Code
class Config:
"""Base configuration class"""
# Flask settings
SECRET_KEY = os.environ.get('SECRET_KEY') or 'smartstat-dev-key-change-in-production'
MAX_CONTENT_LENGTH = 100 * 1024 * 1024 # 100MB max file size
# Application directories
BASE_DIR = Path(__file__).parent
UPLOAD_FOLDER = BASE_DIR / 'uploads'
GENERATED_SCRIPTS_FOLDER = BASE_DIR / 'smartstat_scripts' # Changed from 'generated_scripts' to match actual usage
REPORTS_FOLDER = BASE_DIR / 'reports'
SESSIONS_FOLDER = BASE_DIR / 'sessions'
SANDBOX_FOLDER = BASE_DIR / 'sandbox'
OUTPUT_DIR = BASE_DIR / 'output' # For agent-generated files
# Database
DATABASE_URL = os.environ.get('DATABASE_URL') or f'sqlite:///{BASE_DIR}/smartstat.db'
def __init__(self):
# Ensure directories exist
for directory in [self.UPLOAD_FOLDER, self.GENERATED_SCRIPTS_FOLDER,
self.REPORTS_FOLDER, self.SESSIONS_FOLDER, self.SANDBOX_FOLDER,
self.OUTPUT_DIR]:
directory.mkdir(parents=True, exist_ok=True)
# LLM API Configuration (matching your existing setup)
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY') or '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'
GEMINI_API_KEY = os.environ.get('GEMINI_API_KEY') or 'AIzaSyBN6Vv4ag8kl2NWjoj96Zz2Qs9GZdO6uEw'
AZURE_OPENAI_ENDPOINT = os.environ.get('AZURE_OPENAI_ENDPOINT') or 'https://vice-llm-2.openai.azure.com/'
AZURE_OPENAI_API_KEY = os.environ.get('AZURE_OPENAI_API_KEY') or '8DaDtzYz3HePiypmFb6JQmJd3zUCtyCQkiYE8bePRnpyk2YNkJZRJQQJ99BAACfhMk5XJ3w3AAABACOGyJVB'
# LLM Model Configuration
AVAILABLE_MODELS = {
'gpt-4o': {
'name': 'GPT-4o (OpenAI)',
'provider': 'openai',
'description': 'OpenAI GPT-4o - Latest model with excellent reasoning and analysis'
},
'claude-sonnet-4-5-20250929': {
'name': 'Claude Sonnet 4.5 (Anthropic)',
'provider': 'anthropic',
'description': 'Anthropic Claude Sonnet 4.5 - Latest model with superior analytical and coding capabilities'
},
'gemini-2.0-flash-exp': {
'name': 'Gemini 2.0 Flash (Google)',
'provider': 'gemini',
'description': 'Google Gemini 2.0 Flash - Experimental model with enhanced reasoning'
}
}
DEFAULT_MODEL = 'gpt-4o'
# Anthropic API Configuration
ANTHROPIC_API_KEY = os.environ.get('ANTHROPIC_API_KEY') or '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'
# Statistical Analysis Settings
DEFAULT_SIGNIFICANCE_LEVEL = 0.05
MAX_DATASET_ROWS = 100000 # Medium size datasets
MAX_COLUMNS = 500
DEFAULT_SAMPLE_SIZE = 1000 # For large dataset sampling
# Python execution settings
SCRIPT_TIMEOUT = 300 # 5 minutes max execution time
ALLOWED_IMPORTS = [
'pandas', 'numpy', 'scipy', 'matplotlib', 'seaborn', 'statsmodels',
'sklearn', 'math', 'statistics', 'datetime', 'json', 'warnings'
]
# SQL Server settings
MSSQL_DRIVER = '{ODBC Driver 18 for SQL Server}'
MSSQL_TIMEOUT = 60
# File format settings
ALLOWED_EXTENSIONS = {'csv', 'xlsx', 'xls', 'txt', 'tsv'}
# Report settings
REPORT_TEMPLATE_DIR = BASE_DIR / 'templates' / 'reports'
# Cleanup settings
AUTO_CLEANUP_ENABLED = True # Enable automatic cleanup
KEEP_RECENT_ANALYSES = 8 # Number of recent analyses to keep per session
CLEANUP_OLD_SESSIONS_DAYS = 15 # Days after which to clean up entire sessions
MAX_ANALYSES_PER_SESSION = 10 # Maximum analyses before forced cleanup
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
- | - |
Parameter Details
__init__: The constructor takes no parameters. It automatically creates all required directories (uploads, scripts, reports, sessions, sandbox, output) if they don't exist, using mkdir with parents=True and exist_ok=True to ensure safe directory creation.
Return Value
Instantiation returns a Config object with all class attributes accessible. The __init__ method returns None but has the side effect of creating necessary directories. All attributes are accessible as class variables or instance variables after instantiation.
Class Interface
Methods
__init__(self) -> None
Purpose: Initializes the Config instance and creates all required directories if they don't exist
Returns: None, but creates directories as a side effect
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
SECRET_KEY |
str | Flask secret key for session management and CSRF protection, sourced from environment or default | class |
MAX_CONTENT_LENGTH |
int | Maximum allowed file upload size in bytes (100MB) | class |
BASE_DIR |
Path | Base directory path of the application, derived from the config file location | class |
UPLOAD_FOLDER |
Path | Directory path for storing uploaded files | class |
GENERATED_SCRIPTS_FOLDER |
Path | Directory path for storing generated statistical analysis scripts | class |
REPORTS_FOLDER |
Path | Directory path for storing generated analysis reports | class |
SESSIONS_FOLDER |
Path | Directory path for storing user session data | class |
SANDBOX_FOLDER |
Path | Directory path for sandboxed script execution | class |
OUTPUT_DIR |
Path | Directory path for agent-generated output files | class |
DATABASE_URL |
str | Database connection URL, defaults to SQLite database in BASE_DIR | class |
OPENAI_API_KEY |
str | API key for OpenAI services, sourced from environment or default | class |
GEMINI_API_KEY |
str | API key for Google Gemini services, sourced from environment or default | class |
AZURE_OPENAI_ENDPOINT |
str | Azure OpenAI service endpoint URL, sourced from environment or default | class |
AZURE_OPENAI_API_KEY |
str | API key for Azure OpenAI services, sourced from environment or default | class |
AVAILABLE_MODELS |
dict | Dictionary mapping model IDs to their configuration including name, provider, and description | class |
DEFAULT_MODEL |
str | Default LLM model identifier to use ('gpt-4o') | class |
ANTHROPIC_API_KEY |
str | API key for Anthropic Claude services, sourced from environment or default | class |
DEFAULT_SIGNIFICANCE_LEVEL |
float | Default statistical significance level (alpha) for hypothesis tests (0.05) | class |
MAX_DATASET_ROWS |
int | Maximum number of rows allowed in a dataset (100,000) | class |
MAX_COLUMNS |
int | Maximum number of columns allowed in a dataset (500) | class |
DEFAULT_SAMPLE_SIZE |
int | Default sample size for large dataset sampling (1,000) | class |
SCRIPT_TIMEOUT |
int | Maximum execution time in seconds for Python scripts (300 seconds / 5 minutes) | class |
ALLOWED_IMPORTS |
list | List of Python module names allowed for import in generated scripts for security | class |
MSSQL_DRIVER |
str | ODBC driver name for SQL Server connections | class |
MSSQL_TIMEOUT |
int | Timeout in seconds for SQL Server queries (60 seconds) | class |
ALLOWED_EXTENSIONS |
set | Set of allowed file extensions for uploads (csv, xlsx, xls, txt, tsv) | class |
REPORT_TEMPLATE_DIR |
Path | Directory path for report templates | class |
AUTO_CLEANUP_ENABLED |
bool | Flag to enable automatic cleanup of old analyses and sessions | class |
KEEP_RECENT_ANALYSES |
int | Number of recent analyses to keep per session during cleanup (8) | class |
CLEANUP_OLD_SESSIONS_DAYS |
int | Number of days after which to clean up entire sessions (15) | class |
MAX_ANALYSES_PER_SESSION |
int | Maximum number of analyses allowed per session before forced cleanup (10) | class |
Dependencies
ospathlib
Required Imports
import os
from pathlib import Path
Usage Example
# Basic instantiation
from pathlib import Path
import os
config = Config()
# Access Flask settings
secret_key = config.SECRET_KEY
max_size = config.MAX_CONTENT_LENGTH
# Access directory paths
upload_path = config.UPLOAD_FOLDER
reports_path = config.REPORTS_FOLDER
# Access API keys
openai_key = config.OPENAI_API_KEY
anthropic_key = config.ANTHROPIC_API_KEY
# Access model configuration
available_models = config.AVAILABLE_MODELS
default_model = config.DEFAULT_MODEL
model_info = config.AVAILABLE_MODELS['gpt-4o']
# Access statistical settings
significance = config.DEFAULT_SIGNIFICANCE_LEVEL
max_rows = config.MAX_DATASET_ROWS
# Access execution settings
timeout = config.SCRIPT_TIMEOUT
allowed_imports = config.ALLOWED_IMPORTS
# Access cleanup settings
if config.AUTO_CLEANUP_ENABLED:
keep_recent = config.KEEP_RECENT_ANALYSES
cleanup_days = config.CLEANUP_OLD_SESSIONS_DAYS
Best Practices
- Instantiate Config once at application startup and reuse the instance throughout the application lifecycle
- Override environment variables in production to avoid using hardcoded API keys and secrets
- The class automatically creates directories on instantiation, so ensure the application has write permissions
- Use Config.AVAILABLE_MODELS to dynamically discover supported LLM models rather than hardcoding model names
- Check Config.ALLOWED_EXTENSIONS before processing uploaded files to ensure security
- Respect Config.SCRIPT_TIMEOUT when executing user-generated Python scripts to prevent infinite loops
- Use Config.ALLOWED_IMPORTS as a whitelist when validating Python code execution for security
- Monitor Config.MAX_ANALYSES_PER_SESSION and implement cleanup logic to prevent resource exhaustion
- Change SECRET_KEY in production environments for security
- All directory paths are Path objects, use them with Path operations or convert to strings as needed
- The class uses class variables, so all instances share the same configuration values
- API keys have default values for development but should be overridden via environment variables in production
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class Config_v1 89.8% similar
-
class Config_v3 67.6% similar
-
class SmartStatConfig 65.4% similar
-
class Config_v5 64.7% similar
-
class DevelopmentConfig 63.2% similar