parse_json
This module consists of a single class, JSONParser
.
- class pype_schema.parse_json.JSONParser(path)[source]
A parser to convert a JSON file into a Network object
- Parameters:
path (str) – path to the JSON file to load
- Variables:
- add_virtual_tags(verbose=False)[source]
Adds all virtual tags in an object NOTE: assumes the objects tags have already been added
- Parameters:
obj (Node or Connection) – object to add virtual tags to
verbose (bool) – If True will print informative statements while adding virtual tags. Default is False
- collect_virtual_tags(config, obj_id=None, virtual_tags=None)[source]
Recursively collects all virtual tags in a network’s dictionary representation (i.e. config)
- Parameters:
config (dict) – Dictionary representation of the network
obj_id (str) – Optional “id” of config whose virtual tags are being collected (if None will use “ParentNetwork”)
virtual_tags (dict) – dictionary to store virtual tags in
- Returns:
dictionary of all VirtualTag in network
- Return type:
- static conn_to_dict(conn_obj)[source]
Converts a Connection object to a dictionary that can be parsed into JSON format
- Parameters:
conn_obj (Connection) – object to be converted into a dictionary
- Returns:
conn_obj in dictionary form
- Return type:
- create_connection(connection_id, node_obj, verbose=False)[source]
Converts a dictionary into a Connection object
- Parameters:
connection_id (str) – the string id for the Connection
node_id (str) – the string id for the Node which holds this connection. If None the default ID, ‘ParentNetwork’ is used
verbose (bool) – Whether to print informative messages for debugging. Default is False
- Returns:
a Python object with all the values from key connection_id
- Return type:
- create_node(node_id, verbose=False)[source]
Converts a dictionary into a Node object
- Parameters:
node_id (str) – the string id for the Node
verbose (bool) – Whether to print informative messages for debugging. Default is False
- Returns:
a Python object with all the values from key node_id
- Return type:
- static dosing_to_dict(dosing_dict)[source]
Converts the dosing rate or area dictionary from PyPES to JSON format.
- Parameters:
dosing_dict (dict) –
- dictionary of the form {
DosingTypeA
: float or`pint.Quantity`DosingTypeB
: float or pint.Quantity
}
- Returns:
- Dictionary in JSON-readable format. I.e., {
DosingTypeA
: {value
: floatunits
: str
}
DosingTypeB
: {value
: floatunits
: str
}
}
- Return type:
- extend_node(new_network, target_node_id, connections_path, inplace=False, verbose=False)[source]
Incoporates subnetwork (i.e. the new_network) into a node in a existing network (i.e. the old_network) modifying it in place and returning the modified network
- Parameters:
new_network (str or pype_schema.Network) – JSON file path or Network objet to merge with self
target_node_id (str) – ID of the node to expend, must be in the old_network
connections_path (str) – JSON file path to the connections connecting new_network to old_network.
inplace (bool) – Whether to modify self in place or leave original object unmodified. False by default
verbose (bool) – Whether to print informative messages for debugging. Default is False
- Raises:
- Returns:
Modified network object
- Return type:
- static get_tag_contents(tag_id, tag_info, obj)[source]
Parse tag ID and dictionary of information into Tag object
- Parameters:
tag_id (str) – name of the tag
tag_info (dict) –
- dictionary of the form {
type
: TagTypeunits
: strcontents
: strsource_unit_id
: int or strdest_unit_id
: int or strtotalized
: bool
}
obj (Node or Connection) – object that this tag is associated with, which is used to gather relevant metadata
- Returns:
the contents of this tag
- Return type:
- Raises:
ValueError – If contents are ambiguously defined in JSON. E.g., contents not defined in tag and parent object has a list of contents
- initialize_network(verbose=False)[source]
Converts a dictionary into a Network object
- Parameters:
verbose (bool) – If True will print informative statements while initializing the network
- Returns:
a Python object with all the values from the JSON file stored hierarchically
- Return type:
- merge_network(old_network, inplace=False)[source]
Incorporates nodes/connections (i.e. the new_network) into a network (i.e. old_newtwork) modifying it in place and returning the modified network
- Parameters:
old_network (str or pype_schema.Network) – JSON file path or Network objet to merge with self
- Raises:
TypeError: – When user does not provide a valid path or Network object for old_network
- Returns:
Modified network object
- Return type:
- static min_max_design_to_dict(obj, attribute)[source]
Converts the flow rate tuple of a Node or Connection into a dictionary object
- Parameters:
obj (Node or Connection) – object to get attribute of and convert it to a dictionary
attribute (str) – Either “flow_rate”, “gen_capacity”, or “pressure”
- Returns:
- flow rate in the form {
min
: float or intmax
: float or intdesign
: float or intunits
: str
}
- Return type:
- static node_to_dict(node_obj)[source]
Converts a Node object to a dictionary that can be parsed into JSON format
- static parse_dosing_rate(dosing_dict)[source]
Converts a dictionary into a dictionary of a different format
- Parameters:
heating_vals (dict) –
dictionary of the form {
DosingType
: {rate
: floatunits
: str}
}
- Returns:
Keys are of DosingType or str and values of pint.Quantity or float Given as a float if no units are specified
- Return type:
- static parse_min_max_design(min_max_design)[source]
Converts a dictionary into a tuple of flow rates
- Parameters:
min_max_design (dict) –
- dictionary of the form {
min
: floatmax
: floatdesign
: floatunits
: str
}
- Returns:
(min, max, and average) with the given Pint units as a tuple. If no units given, then returns a tuple of floats.
- Return type:
(pint.Quantity, pint.Quantity, pint.Quantity) or (float, float, float)
- static parse_tag(tag_id, tag_info, obj)[source]
Parse tag ID and dictionary of information into Tag object
- Parameters:
tag_id (str) – name of the tag
tag_info (dict) –
- dictionary of the form {
type
: TagTypeunits
: strcontents
: strsource_unit_id
: int or strdest_unit_id
: int or strtotalized
: bool
}
obj (Node or Connection) – object that this tag is associated with, which is used to gather relevant metadata
- Returns:
a Python object with the given ID and the values from tag_info
- Return type:
- static parse_unit_val_dict(unit_dict)[source]
Converts a dictionary into a Pint Quantity
- Parameters:
unit_dict (dict) –
- dictionary of the form {
value
:float
units
: str
}
- Returns:
Dictionary as a Pint Quantity (or float if no units are specified)
- Return type:
pint.Quantity or float
- static parse_virtual_tag(tag_id, tag_info, obj, parent_network=None)[source]
Parse tag ID and dictionary information into VirtualTag object
- Parameters:
tag_id (str) – name of the tag
tag_info (dict) –
- dictionary of the form {
tags
: dict of Tagoperations
: strtype
: TagTypecontents
: str
}
obj (Node or Connection) – parent object that contains all constituent tags, which is used to gather the tag list for combining data correctly
parent_network (None, Network) – Optional network object the tag is a part of If None will assume obj is parent network and all tags are in obj.tags
parent_network (None, Network) – Optional network object the tag is a part of If None will assume obj is the parent network and all tags are in obj.tags
- Returns:
a Python object with the given ID and the values from tag_info
- Return type:
- static prefix_children(target_node, prefix)[source]
Renames the children nodes and connections of the target_node by prepending their ID with prefix.
- Parameters:
target_node (node.Network) – Network object to rename the children of with a prefix
prefix (str) – String to prepend to the existing node and connection objects
- Returns:
New network object with
- Return type:
- static tag_to_dict(tag_obj)[source]
Converts a Tag or VirtualTag object to a dictionary that can be parsed into JSON format
- static to_json(network, file_path=None, indent=4, verbose=False)[source]
Converts a Network object to a JSON file
- Parameters:
network (node.Network) – Network object to export to JSON
file_path (str) – path to write the configuration in JSON format. Default is None, meaning that no file will be written
indent (int) – number of spaces to indent the JSON file. Default is 4
verbose (bool) – Whether to print informative messages for debugging. Default is False
- Raises:
TypeError – If network is not of type Network
- Returns:
json in dictionary format
- Return type: