def test_f_get_hash():
# Test generating a hash for a simple object
= {"key": "value"}
obj = 'h1_a7353f7cddce808de0032747a0b7be50'
expected_hash assert f_get_hash(obj) == expected_hash
def test_f_to_dict():
# Test converting a simple object to a dictionary
= {"key": "value"}
obj = {"key": "value"}
expected_dict assert f_to_dict(obj) == expected_dict
# Test converting a datetime object to a dictionary
= datetime.datetime(2020, 1, 1, 12, 0, 0)
obj = "2020-01-01T12:00:00"
expected_dict assert f_to_dict(obj) == expected_dict
def test_f_data_class_to_dict():
# Define a simple data class
@dataclasses.dataclass
class DataClass:
str
field1: int
field2:
# Test converting a data class object to a dictionary
= DataClass("value1", 123)
obj = {"field1": "value1", "field2": 123}
expected_dict
assert f_data_class_to_dict(obj) == expected_dict
@dataclasses.dataclass
class DataClass:
str
field1: int
field2: def test_f_from_dict():
# Define a simple data class
# Test converting a dictionary to a data class object
= {"__type__": "DataClass", "__module__": __name__, "field1": "value1", "field2": 123}
obj_dict = DataClass("value1", 123)
expected_obj assert f_from_dict(obj_dict) == expected_obj
def test_dict_to_obj():
# Define a simple class
class SimpleClass:
def __init__(self, field1, field2):
self.field1 = field1
self.field2 = field2
def __eq__(self, other):
# Compare the field values of the objects
return self.field1 == other.field1 and self.field2 == other.field2
# Test converting a dictionary to an object
= {"field1": "value1", "field2": 123}
obj_dict = SimpleClass("value1", 123)
expected_obj
# Convert the dictionary to an object
= f_dict_to_obj(obj_dict, SimpleClass)
obj
assert obj == expected_obj
Data Utils
dataclass convenience functions
f_data_class_to_dict
f_data_class_to_dict (thing:object)
Convert a dataclass object to a dictionary.
Parameters: - thing: the object to convert to a dictionary
Returns: - a dictionary representation of the object
f_to_dict
f_to_dict (item)
Convert the given item to a dictionary.
Parameters: - item: the object to convert to a dictionary
Returns: - a dictionary representation of the object
f_get_hash
f_get_hash (item:object, prefix:str='h1')
Generate a hash of the given item.
Parameters: - item: the object to generate a hash for - prefix: a prefix to include in the generated hash (default ‘h1_’)
Returns: - a bytes object containing the generated hash
originally inspired by https://death.andgravity.com/stable-hashing
f_from_dict
f_from_dict (item:Dict[str,Any])
Convert a dictionary to an object.
Parameters: - item: the dictionary to convert to an object
Returns: - an object representation of the dictionary
f_data_class_from_dict
f_data_class_from_dict (type_:type, data:Dict[str,Any])
Convert a dictionary to a dataclass object.
Parameters: - type_: the type of object to create - data: the dictionary to convert to an object
Returns: - an object of the specified type, initialized with the data from the dictionary
f_dict_to_obj
f_dict_to_obj (d:Dict[str,Any], cls:Type[~T])
Convert a dictionary to an object of the given class.
Parameters: - d: the dictionary to convert to an object - cls: the class to use for creating the object
Returns: - an object of the given class, initialized with the values from the dictionary
test
test_f_get_hash()
test_f_to_dict()
test_f_data_class_to_dict()
test_f_from_dict() test_dict_to_obj()