Coverage for mlos_bench/mlos_bench/os_environ.py: 73%

11 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-21 01:50 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5""" 

6Simple platform agnostic abstraction for the OS environment variables. Meant as a 

7replacement for :external:py:data:`os.environ` vs ``nt.environ``. 

8 

9Example 

10------- 

11>>> # Import the environ object. 

12>>> from mlos_bench.os_environ import environ 

13>>> # Set an environment variable. 

14>>> environ["FOO"] = "bar" 

15>>> # Get an environment variable. 

16>>> pwd = environ.get("PWD") 

17""" 

18 

19import os 

20import sys 

21from typing import TypeAlias 

22 

23# pylint: disable=protected-access,disable=unsubscriptable-object 

24EnvironType: TypeAlias = os._Environ[str] 

25 

26# Handle case sensitivity differences between platforms. 

27# https://stackoverflow.com/a/19023293 

28if sys.platform == "win32": 

29 import nt # type: ignore[import-not-found] # pylint: disable=import-error # (3.8) 

30 

31 environ: EnvironType = nt.environ 

32 """A platform agnostic abstraction for the OS environment variables.""" 

33else: 

34 environ: EnvironType = os.environ 

35 """A platform agnostic abstraction for the OS environment variables.""" 

36 

37__all__ = ["environ"]