Environment Functions¶
System environment detection with intelligent caching for performance optimization.
environment
¶
Environment and system information utilities.
Functions:
-
environment–Obtain details of the hardware and software environment.
environment
¶
environment(cache_dir: Optional[str] = None) -> Dict[str, Any]
Obtain details of the hardware and software environment.
For efficiency, this is obtained from a cached file "environment.json" if one modified in the last 24 hours is available, otherwise the OS is queried and a new cache file created.
Parameters:
-
(cache_dir¶Optional[str], default:None) –Optional cache directory. If None, uses standard user cache directory for the platform.
Returns:
-
Dict[str, Any]–Environment information including os, cpu, python, ram.
Usage Examples¶
Basic Environment Detection¶
from causaliq_core.utils import environment
# Use default cache location
env = environment()
print(f"Running on {env['os']} with {env['ram']}GB RAM")
print(f"CPU: {env['cpu']}")
print(f"Python: {env['python']}")
Custom Cache Directory¶
from causaliq_core.utils import environment
# Use custom cache directory
env = environment(cache_dir="/tmp/my_cache")
print(f"OS: {env['os']}")
Returned Information¶
The environment function returns a dictionary with the following keys:
'os': Operating system name and version'cpu': CPU brand/model information'python': Python version string'ram': Total system RAM in GB (rounded to nearest GB)
Caching Behavior¶
- Cache files are stored as
environment.jsonin the cache directory - Cache is refreshed if older than 24 hours
- Uses platform-appropriate cache locations (e.g.,
~/.cacheon Linux,~/Library/Cacheson macOS) - Gracefully handles cache corruption or permission errors