import hashlib
import inspect
[docs]
def get_class_hash(cls):
"""
Generates a unique hash value for a given class.
This function uses the built-in `hashlib` and `inspect` modules to create a hash value based on the class' methods
and properties. The members of the class are first sorted to ensure a consistent order, and then the hash object is
updated with each member's name and signature.
Parameters:
- cls (type): The class object to calculate the hash for.
Returns:
- str: The generated hexadecimal hash value.
"""
hash_obj = hashlib.sha256()
# Get the list of methods and properties of the class
members = inspect.getmembers(cls, predicate=inspect.isfunction)
members += inspect.getmembers(cls, predicate=inspect.isdatadescriptor)
# Sort members to ensure consistent order
members.sort()
# Update the hash with each member's name and signature
for name, member in members:
hash_obj.update(name.encode('utf-8'))
if inspect.isfunction(member):
sig = inspect.signature(member)
hash_obj.update(str(sig).encode('utf-8'))
# Return the hexadecimal digest of the hash
return hash_obj.hexdigest()