Repository: Project-MONAI/MONAI
File: tests/test_utils.py (Python)

Code before:
from monai.utils.misc import MONAIEnvVars
from monai.utils.module import compute_capabilities_after, pytorch_after
from monai.utils.tf32 import detect_default_tf32
from monai.utils.type_conversion import convert_data_type

nib, _ = optional_import("nibabel")
http_error, has_req = optional_import("requests", name="HTTPError")
file_url_error, has_gdown = optional_import("gdown.exceptions", name="FileURLRetrievalError")
hf_http_error, has_hf_hub = optional_import("huggingface_hub.errors", name="HfHubHTTPError")
hf_local_entry_error, _has_hf_local = optional_import("huggingface_hub.errors", name="LocalEntryNotFoundError")


quick_test_var = "QUICKTEST"
_tf32_enabled = None
_test_data_config: dict = {}

MODULE_PATH = Path(__file__).resolve().parents[1]

DOWNLOAD_EXCEPTS: tuple[type, ...] = (ContentTooShortError, HTTPError, ConnectionError)
if has_req:
    DOWNLOAD_EXCEPTS += (http_error,)
if has_gdown:
    DOWNLOAD_EXCEPTS += (file_url_error,)
if has_hf_hub:
    DOWNLOAD_EXCEPTS += (hf_http_error,)
if _has_hf_local:
    DOWNLOAD_EXCEPTS += (hf_local_entry_error,)

DOWNLOAD_FAIL_MSGS = (
    "unexpected EOF",  # incomplete download
    "network issue",
    "gdown dependency",  # gdown not installed
    "md5 check",
    "limit",  # HTTP Error 503: Egress is over the account limit
    "authenticate",
    "timed out",  # urlopen error [Errno 110] Connection timed out
    "HTTPError",  # HTTPError: 429 Client Error: Too Many Requests for huggingface hub
)


def testing_data_config(*keys):
    """get _test_data_config[keys0][keys1]...[keysN]"""
    if not _test_data_config:
        with open(f"{MODULE_PATH}/tests/testing_data/data_config.json") as c:
            _config = json.load(c)
            for k, v in _config.items():
                _test_data_config[k] = v
    return reduce(operator.getitem, keys, _test_data_config)


def get_testing_algo_template_path():

Review comment (suggestion):
```suggestion
if has_hf_hub:
    DOWNLOAD_EXCEPTS += (hf_http_error, hf_local_entry_error)

```
Unless there's a case where one exception may be missing and the other present we can do this.

Code after:
from monai.utils.misc import MONAIEnvVars
from monai.utils.module import compute_capabilities_after, pytorch_after
from monai.utils.tf32 import detect_default_tf32
from monai.utils.type_conversion import convert_data_type

nib, _ = optional_import("nibabel")
http_error, has_req = optional_import("requests", name="HTTPError")
file_url_error, has_gdown = optional_import("gdown.exceptions", name="FileURLRetrievalError")
hf_http_error, has_hf_hub = optional_import("huggingface_hub.errors", name="HfHubHTTPError")
hf_local_entry_error, _has_hf_local = optional_import("huggingface_hub.errors", name="LocalEntryNotFoundError")


quick_test_var = "QUICKTEST"
_tf32_enabled = None
_test_data_config: dict = {}

MODULE_PATH = Path(__file__).resolve().parents[1]

DOWNLOAD_EXCEPTS: tuple[type, ...] = (ContentTooShortError, HTTPError, ConnectionError)
if has_req:
    DOWNLOAD_EXCEPTS += (http_error,)
if has_gdown:
    DOWNLOAD_EXCEPTS += (file_url_error,)
if has_hf_hub:
    DOWNLOAD_EXCEPTS += (hf_http_error, hf_local_entry_error)

DOWNLOAD_FAIL_MSGS = (
    "unexpected EOF",  # incomplete download
    "network issue",
    "gdown dependency",  # gdown not installed
    "md5 check",
    "limit",  # HTTP Error 503: Egress is over the account limit
    "authenticate",
    "timed out",  # urlopen error [Errno 110] Connection timed out
    "HTTPError",  # HTTPError: 429 Client Error: Too Many Requests for huggingface hub
)


def testing_data_config(*keys):
    """get _test_data_config[keys0][keys1]...[keysN]"""
    if not _test_data_config:
        with open(f"{MODULE_PATH}/tests/testing_data/data_config.json") as c:
            _config = json.load(c)
            for k, v in _config.items():
                _test_data_config[k] = v
    return reduce(operator.getitem, keys, _test_data_config)


def get_testing_algo_template_path():
    """
    a local folder to the testing algorithm template or a url to the compressed template file.