1. import os
  2. def truncate_runtime_data(data, environment):
  3. """
  4. Truncates runtime environment specific data for staging environments.
  5. Args:
  6. data (dict): The data dictionary to truncate.
  7. environment (str): The current environment (e.g., "staging").
  8. Returns:
  9. dict: The truncated data dictionary.
  10. """
  11. if environment == "staging":
  12. # Truncate environment-specific data. Example: remove sensitive keys.
  13. if "api_key" in data:
  14. del data["api_key"]
  15. if "secret_key" in data:
  16. del data["secret_key"]
  17. if "database_password" in data:
  18. del data["database_password"]
  19. #Truncate other environment specific values
  20. for key in list(data.keys()): # Iterate over copy to allow deletion
  21. if key.startswith("staging_"):
  22. del data[key]
  23. return data
  24. if __name__ == '__main__':
  25. #Example Usage
  26. test_data = {
  27. "api_key": "staging_api_key",
  28. "secret_key": "staging_secret",
  29. "database_password": "staging_password",
  30. "name": "Test Data",
  31. "staging_setting": "value"
  32. }
  33. current_environment = os.environ.get("ENVIRONMENT", "production") #Default to production if not set
  34. truncated_data = truncate_runtime_data(test_data, current_environment)
  35. print(truncated_data)

Add your comment