import os
def truncate_runtime_data(data, environment):
"""
Truncates runtime environment specific data for staging environments.
Args:
data (dict): The data dictionary to truncate.
environment (str): The current environment (e.g., "staging").
Returns:
dict: The truncated data dictionary.
"""
if environment == "staging":
# Truncate environment-specific data. Example: remove sensitive keys.
if "api_key" in data:
del data["api_key"]
if "secret_key" in data:
del data["secret_key"]
if "database_password" in data:
del data["database_password"]
#Truncate other environment specific values
for key in list(data.keys()): # Iterate over copy to allow deletion
if key.startswith("staging_"):
del data[key]
return data
if __name__ == '__main__':
#Example Usage
test_data = {
"api_key": "staging_api_key",
"secret_key": "staging_secret",
"database_password": "staging_password",
"name": "Test Data",
"staging_setting": "value"
}
current_environment = os.environ.get("ENVIRONMENT", "production") #Default to production if not set
truncated_data = truncate_runtime_data(test_data, current_environment)
print(truncated_data)
Add your comment