Coverage for strongcoca/env.py: 69%
14 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-10-26 18:44 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-10-26 18:44 +0000
1from os import getenv
4def strongcoca_getenv(variable) -> str:
5 """ Get value of environment variable, or default.
7 Possible environment variables are:
9 - `STRONGCOCA_MAX_SOLVE_MEM` - Value in units of MiB. In the linear solve operation for the
10 PolarizabilityCalculator, try to limit memory of intermediate matrices below this value.
12 Parameters
13 ----------
14 variable
15 Name of variable, without the ``'STRONGCOCA'`` prefix.
17 Returns
18 -------
19 Value of variable as string.
20 """
22 defaults = {'MAX_SOLVE_MEM': '80',
23 }
25 if variable not in defaults.keys(): 25 ↛ 26line 25 didn't jump to line 26 because the condition on line 25 was never true
26 allowed = '\n'.join([f' STRONGCOCA_{var}' for var in defaults.keys()])
27 raise ValueError(f'Environment variable STRONGCOCA_{variable} is unknown. '
28 f'Allowed variables are:\n{allowed}')
30 return getenv(f'STRONGCOCA_{variable}', defaults[variable])
33def get_float(variable) -> float:
34 strvalue = strongcoca_getenv(variable)
35 try:
36 value = float(strvalue)
37 except ValueError:
38 raise ValueError(f'Expected environment variable STRONGCOCA_{variable} to '
39 f'be castable to float. Value is {strvalue!r}.')
41 return value