Utility Functions
NodeEncoder
¶
Bases: JSONEncoder
Custom JSON encoder for serializing CRIPT nodes to JSON.
This encoder is used to convert CRIPT nodes into JSON format while handling unique identifiers (UUIDs) and condensed representations to avoid redundancy in the JSON output. It also allows suppressing specific attributes from being included in the serialized JSON.
Attributes:
Name | Type | Description |
---|---|---|
handled_ids |
Set[str]
|
A set to store the UIDs of nodes that have been processed during serialization. |
known_uuid |
Set[str]
|
A set to store the UUIDs of nodes that have been previously encountered in the JSON. |
condense_to_uuid |
Dict[str, Set[str]]
|
A set to store the node types that should be condensed to UUID edges in the JSON. |
suppress_attributes |
Optional[Dict[str, Set[str]]]
|
A dictionary that allows suppressing specific attributes for nodes with the corresponding UUIDs. |
Methods:
Name | Description |
---|---|
```python |
|
default |
Convert CRIPT nodes and other objects to their JSON representation.¶ |
``` |
|
```python |
|
_apply_modifications |
|
``` |
|
Source code in src/cript/nodes/util/__init__.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
|
default(obj)
¶
Convert CRIPT nodes and other objects to their JSON representation.
This method is called during JSON serialization. It customizes the serialization process for CRIPT nodes and handles unique identifiers (UUIDs) to avoid redundant data in the JSON output. It also allows for attribute suppression for specific nodes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
Any
|
The object to be serialized to JSON. |
required |
Returns:
Type | Description |
---|---|
dict
|
The JSON representation of the input object, which can be a string, a dictionary, or any other JSON-serializable type. |
Raises:
Type | Description |
---|---|
CRIPTJsonDeserializationError
|
If there is an issue with the JSON deserialization process for CRIPT nodes. |
Notes
- If the input object is a UUID, it is converted to a string representation and returned.
- If the input object is a CRIPT node (an instance of
BaseNode
), it is serialized into a dictionary representation. The node is first checked for uniqueness based on its UID (unique identifier), and if it has already been serialized, it is represented as a UUID edge only. If not, the node's attributes are added to the dictionary representation, and any default attribute values are removed to reduce redundancy in the JSON output. - The method
_apply_modifications()
is called to check if further modifications are needed before considering the dictionary representation done. This includes condensing certain node types to UUID edges and suppressing specific attributes for nodes.
Source code in src/cript/nodes/util/__init__.py
add_orphaned_nodes_to_project(project, active_experiment, max_iteration=-1)
¶
Helper function that adds all orphaned material nodes of the project graph to the
project.materials
attribute.
Material additions only is permissible with active_experiment is None
.
This function also adds all orphaned data, process, computation and computational process nodes
of the project graph to the active_experiment
.
This functions call project.validate
and might raise Exceptions from there.
Source code in src/cript/nodes/util/__init__.py
load_nodes_from_json(nodes_json)
¶
User facing function, that return a node and all its children from a json string input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes_json |
Union[str, Dict]
|
JSON string representation of a CRIPT node |
required |
Examples:
>>> import cript
>>> # Get updated project from API
>>> my_paginator = api.search(
... node_type=cript.Project,
... search_mode=cript.SearchModes.EXACT_NAME,
... value_to_search="my project name",
... )
>>> # Take specific Project you want from paginator
>>> my_project_from_api_dict: dict = my_paginator.current_page_results[0]
>>> # Deserialize your Project dict into a Project node
>>> my_project_node_from_api = cript.load_nodes_from_json(
... nodes_json=my_project_from_api_dict
... )
Raises:
Type | Description |
---|---|
CRIPTJsonNodeError
|
If there is an issue with the JSON of the node field. |
CRIPTJsonDeserializationError
|
If there is an error during deserialization of a specific node. |
CRIPTDeserializationUIDError
|
If there is an issue with the UID used for deserialization, like circular references. |
Notes
This function uses a custom _NodeDecoderHook
to convert JSON nodes into Python objects.
The _NodeDecoderHook
class is responsible for handling the deserialization of nodes
and caching objects with shared UIDs to avoid redundant deserialization.
The function is intended for deserializing CRIPT nodes and should not be used for generic JSON.
Returns:
Type | Description |
---|---|
Union[CRIPT Node, List[CRIPT Node]]
|
Typically returns a single CRIPT node, but if given a list of nodes, then it will serialize them and return a list of CRIPT nodes |