Coverage for mlos_bench/mlos_bench/os_environ.py: 55%
11 statements
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 01:18 +0000
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 01:18 +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``.
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"""
19import os
20import sys
22if sys.version_info >= (3, 10):
23 from typing import TypeAlias
24else:
25 from typing_extensions import TypeAlias
27if sys.version_info >= (3, 9):
28 # pylint: disable=protected-access,disable=unsubscriptable-object
29 EnvironType: TypeAlias = os._Environ[str]
30else:
31 EnvironType: TypeAlias = os._Environ # pylint: disable=protected-access
33# Handle case sensitivity differences between platforms.
34# https://stackoverflow.com/a/19023293
35if sys.platform == "win32":
36 import nt # type: ignore[import-not-found] # pylint: disable=import-error # (3.8)
38 environ: EnvironType = nt.environ
39 """A platform agnostic abstraction for the OS environment variables."""
40else:
41 environ: EnvironType = os.environ
42 """A platform agnostic abstraction for the OS environment variables."""
44__all__ = ["environ"]