Spaces:
Runtime error
Runtime error
File size: 778 Bytes
bb5dde5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
from types import MappingProxyType
from collections.abc import Iterable, Mapping, Sequence, Set
from typing import Any
def to_deep_immutable(obj: Any):
"""Recursively convert mutable containers to immutable equivalents."""
# Handle mappings (dict-like).
if isinstance(obj, Mapping):
return MappingProxyType({to_deep_immutable(key): to_deep_immutable(value) for key, value in obj.items()})
# Handle sets.
if isinstance(obj, Set):
return frozenset(to_deep_immutable(item) for item in obj)
# Handle sequences (list/tuple-like).
if isinstance(obj, (Iterable, Sequence)) and not isinstance(obj, (str, bytes)):
return tuple(to_deep_immutable(item) for item in obj)
# Return anything else as-is.
return obj
|