Utilities

Python Object JSON Tool pyobjson.utils module.

Attributes:
  • __author__ (str) –

    Python package template author.

  • __email__ (str) –

    Python package template author email.

Functions:

Name Description
clean_data_dict

Recursive utility method to un-type custom class type objects for serialization.

derive_custom_class_key

Utility method to derive a key for a custom class representing the fully qualified name of that class.

derive_typed_key_value_pairs

Utility method to derive both keys and Python object types from specially formatted dictionary keys and make

clean_data_dict

clean_data_dict(custom_class_instance, pyobjson_base_custom_subclasses)

Recursive utility method to un-type custom class type objects for serialization.

Parameters:
  • custom_class_instance (Any) –

    Custom Python class instance to be serialized.

  • pyobjson_base_custom_subclasses (list[Type]) –

    List of custom Python class subclasses.

Returns:
  • Dict[str, Any]

    dict[str, Any]: Dictionary that extracts serializable data from custom objects.

Source code in src/pyobjson/utils.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def clean_data_dict(custom_class_instance: Any, pyobjson_base_custom_subclasses: List[Type]) -> Dict[str, Any]:
    """Recursive utility method to un-type custom class type objects for serialization.

    Args:
        custom_class_instance (Any): Custom Python class instance to be serialized.
        pyobjson_base_custom_subclasses (list[Type]): List of custom Python class subclasses.

    Returns:
        dict[str, Any]: Dictionary that extracts serializable data from custom objects.

    """
    clean_dict = {}
    for k, v in vars(custom_class_instance).items():
        clean_dict[k] = (
            clean_data_dict(v, pyobjson_base_custom_subclasses)
            if type(v) in pyobjson_base_custom_subclasses
            else v
        )
    return clean_dict

derive_custom_class_key

derive_custom_class_key(custom_class)

Utility method to derive a key for a custom class representing the fully qualified name of that class.

Parameters:
  • custom_class (Type) –

    The custom class object for which to derive a key.

Returns:
  • str( str ) –

    The fully qualified name of the class in lowercase (e.g. module.submodule.class).

Source code in src/pyobjson/utils.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def derive_custom_class_key(custom_class: Type) -> str:
    """Utility method to derive a key for a custom class representing the fully qualified name of that class.

    Args:
        custom_class (Type): The custom class object for which to derive a key.

    Returns:
        str: The fully qualified name of the class in lowercase (e.g. module.submodule.class).

    """
    # avoid including module if no module exists or the class is in the Python builtins
    if (cls_module := getattr(custom_class, "__module__", None)) and cls_module != "builtins":
        return f"{cls_module.lower()}.{custom_class.__qualname__.lower()}"
    else:
        return custom_class.__qualname__.lower()

derive_typed_key_value_pairs

derive_typed_key_value_pairs(json_dict)

Utility method to derive both keys and Python object types from specially formatted dictionary keys and make their respective values into Python objects of those types.

Parameters:
  • json_dict (Dict[str, Any]) –

    JSON dictionary that may contain keys in the format type.key_name (e.g. path.root_directory) with corresponding string values representing Python objects of that type.

Returns:
  • Dict[str, Any]

    dict[str, Any]: Dictionary with both keys and Python object values derived from specially formatted JSON dictionary keys.

Source code in src/pyobjson/utils.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def derive_typed_key_value_pairs(json_dict: Dict[str, Any]) -> Dict[str, Any]:
    """Utility method to derive both keys and Python object types from specially formatted dictionary keys and make
    their respective values into Python objects of those types.

    Args:
        json_dict (Dict[str, Any]): JSON dictionary that may contain keys in the format type.key_name (e.g.
            path.root_directory) with corresponding string values representing Python objects of that type.

    Returns:
        dict[str, Any]: Dictionary with both keys and Python object values derived from specially formatted JSON
            dictionary keys.

    """
    derived_key_value_pairs = {}
    for key, value in json_dict.items():
        if key.count(".") == 1:
            type_str, key = key.split(".")

            if type_str == "path":  # handle posix paths
                value = Path(value)
            elif type_str == "callable":  # TODO: handle proper serialization and reconstruction of functions
                value = None

            derived_key_value_pairs[key] = value
        else:
            # add key-value pair without modification if key is not formatted with a single "." to indicate a value type
            derived_key_value_pairs[key] = value

    return derived_key_value_pairs