From 3d7951dc02d50c739b798f6ec26f6f245a1a4e2c Mon Sep 17 00:00:00 2001 From: Bouwe Andela Date: Tue, 2 Jul 2024 15:03:55 +0200 Subject: [PATCH] More widely applicable _to_frozen function --- esmvalcore/_recipe/from_datasets.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/esmvalcore/_recipe/from_datasets.py b/esmvalcore/_recipe/from_datasets.py index 8bd33fd5e9..3fbbce8908 100644 --- a/esmvalcore/_recipe/from_datasets.py +++ b/esmvalcore/_recipe/from_datasets.py @@ -4,9 +4,10 @@ import itertools import logging import re +from collections.abc import Iterable, Mapping, Sequence from functools import partial from pathlib import Path -from typing import TYPE_CHECKING, Any, Dict, Iterable, Mapping, Sequence +from typing import TYPE_CHECKING, Any, Dict from nested_lookup import nested_delete @@ -99,10 +100,12 @@ def _move_datasets_up(recipe: Recipe) -> Recipe: def _to_frozen(item): """Return a frozen and sorted copy of nested dicts and lists.""" - if isinstance(item, list): - return tuple(sorted(_to_frozen(elem) for elem in item)) - if isinstance(item, dict): + if isinstance(item, str): + return item + if isinstance(item, Mapping): return tuple(sorted((k, _to_frozen(v)) for k, v in item.items())) + if isinstance(item, Iterable): + return tuple(sorted(_to_frozen(elem) for elem in item)) return item