From 2bca343843e95afaaf64c04377c2193290c45851 Mon Sep 17 00:00:00 2001 From: Kevin Cameron Grismore Date: Thu, 1 Feb 2024 21:57:20 -0600 Subject: [PATCH] add hashing util --- prefect_kubernetes/utilities.py | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/prefect_kubernetes/utilities.py b/prefect_kubernetes/utilities.py index 1bb4835..bd2bcca 100644 --- a/prefect_kubernetes/utilities.py +++ b/prefect_kubernetes/utilities.py @@ -1,4 +1,5 @@ """ Utilities for working with the Python Kubernetes API. """ + import socket import sys from pathlib import Path @@ -7,6 +8,7 @@ from kubernetes.client import ApiClient from kubernetes.client import models as k8s_models from prefect.infrastructure.kubernetes import KubernetesJob, KubernetesManifest +from prefect.utilities.collections import visit_collection from slugify import slugify # Note: `dict(str, str)` is the Kubernetes API convention for @@ -46,6 +48,38 @@ def enable_socket_keep_alive(client: ApiClient) -> None: ] = socket_options +def hash_collection(collection) -> int: + """Use visit_collection to transform and hash a collection. + + Args: + collection (Any): The collection to hash. + + Returns: + int: The hash of the transformed collection. + + Example: + ```python + from prefect_aws.utilities import hash_collection + + hash_collection({"a": 1, "b": 2}) + ``` + + """ + + def make_hashable(item): + """Make an item hashable by converting it to a tuple.""" + if isinstance(item, dict): + return tuple(sorted((k, make_hashable(v)) for k, v in item.items())) + elif isinstance(item, list): + return tuple(make_hashable(v) for v in item) + return item + + hashable_collection = visit_collection( + collection, visit_fn=make_hashable, return_data=True + ) + return hash(hashable_collection) + + def convert_manifest_to_model( manifest: Union[Path, str, KubernetesManifest], v1_model_name: str ) -> V1KubernetesModel: