🔍 Code Extractor

function deephash

Maturity: 41

Computes a hash value for any Python object by serializing it to JSON using a custom HashableJSON encoder and returning the hash of the resulting string.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
365 - 373
Complexity:
simple

Purpose

This function provides a way to generate hash values for complex Python objects that may not be directly hashable. It's useful for caching, comparison, and deduplication of objects. The hash is deterministic for the same object structure but is not guaranteed to be consistent across different Python versions, architectures, or platforms. Returns None if the object cannot be serialized to JSON.

Source Code

def deephash(obj):
    """
    Given an object, return a hash using HashableJSON. This hash is not
    architecture, Python version or platform independent.
    """
    try:
        return hash(json.dumps(obj, cls=HashableJSON, sort_keys=True))
    except Exception:
        return None

Parameters

Name Type Default Kind
obj - - positional_or_keyword

Parameter Details

obj: Any Python object to be hashed. Can be primitives (int, str, float), collections (list, dict, tuple), or custom objects that can be serialized by the HashableJSON encoder. Objects that cannot be JSON-serialized will cause the function to return None.

Return Value

Returns an integer hash value computed from the JSON representation of the input object, or None if the object cannot be serialized to JSON. The hash is generated using Python's built-in hash() function on the sorted JSON string representation. Note: This hash is not cryptographically secure and should not be used for security purposes.

Dependencies

  • json

Required Imports

import json

Usage Example

# Assuming HashableJSON is defined elsewhere in the module
# from your_module import deephash, HashableJSON

# Example with simple objects
obj1 = {'key': 'value', 'number': 42}
hash1 = deephash(obj1)
print(f"Hash of dict: {hash1}")

# Example with nested structures
obj2 = {'nested': {'list': [1, 2, 3], 'tuple': (4, 5, 6)}}
hash2 = deephash(obj2)
print(f"Hash of nested structure: {hash2}")

# Example with non-serializable object (returns None)
obj3 = lambda x: x + 1
hash3 = deephash(obj3)
print(f"Hash of lambda: {hash3}")  # Will print None

# Verify same objects produce same hash
obj4 = {'key': 'value', 'number': 42}
assert deephash(obj1) == deephash(obj4)

Best Practices

  • This hash is NOT suitable for cryptographic purposes or security-sensitive applications
  • The hash is not guaranteed to be stable across different Python versions, platforms, or architectures
  • Always check for None return value when hashing objects that might not be JSON-serializable
  • Use this for in-memory caching and comparison within the same runtime environment only
  • Ensure the HashableJSON encoder is properly defined to handle all object types you intend to hash
  • For cross-platform or persistent hashing needs, consider using hashlib with a stable serialization format
  • The sort_keys=True parameter ensures consistent ordering of dictionary keys for deterministic hashing

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class HashableJSON 69.0% similar

    A JSON encoder extension that generates hashable string representations for a wide variety of Python objects, including those not normally JSON-serializable like sets, numpy arrays, and pandas DataFrames.

    From: /tf/active/vicechatdev/patches/util.py
  • function safe_json_dumps 50.4% similar

    Safely serializes Python objects to JSON format, handling NaN values and datetime objects that would otherwise cause serialization errors.

    From: /tf/active/vicechatdev/full_smartstat/services.py
  • function safe_json_serialize 49.3% similar

    Recursively converts Python objects into JSON-serializable formats, with special handling for datetime objects and objects with to_dict() methods.

    From: /tf/active/vicechatdev/CDocs/utils/__init__.py
  • function clean_for_json_v3 48.5% similar

    Recursively converts Python objects (including NumPy and Pandas types) into JSON-serializable formats by handling special numeric types, NaN/Inf values, and nested data structures.

    From: /tf/active/vicechatdev/vice_ai/smartstat_scripts/f0a78968-1d2b-4fbe-a0c6-a372da2ce2a4/project_1/analysis.py
  • function clean_for_json_v12 48.5% similar

    Recursively sanitizes Python objects to make them JSON-serializable by converting non-serializable types (NumPy types, pandas objects, tuples, NaN/Inf values) into JSON-compatible formats.

    From: /tf/active/vicechatdev/vice_ai/smartstat_scripts/290a39ea-3ae0-4301-8e2f-9d5c3bf80e6e/project_3/analysis.py
← Back to Browse