Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
add hashing util
Browse files Browse the repository at this point in the history
  • Loading branch information
kevingrismore committed Feb 2, 2024
1 parent d2b0f47 commit 2bca343
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions prefect_kubernetes/utilities.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" Utilities for working with the Python Kubernetes API. """

import socket
import sys
from pathlib import Path
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 2bca343

Please sign in to comment.