function deephash
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.
/tf/active/vicechatdev/patches/util.py
365 - 373
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
-
function safe_json_dumps 50.4% similar
-
function safe_json_serialize 49.3% similar
-
function clean_for_json_v3 48.5% similar
-
function clean_for_json_v12 48.5% similar