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:
  • path (str) – path to the JSON file to load

  • config (dict) – dictionary with the contents the JSON file

  • network_obj (Network) – Python representation of the JSON file

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:

dict

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:

dict

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:

Connection

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:

Node

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: float

units: str

}

DosingTypeB: {

value: float

units: str

}

}

Return type:

dict

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:
  • TypeError – When user does not provide a valid path or Network object for old_network or new_network

  • KeyError – When target_node_id is not in the old_network or any node in connections_path is not in the new_network or old_network

Returns:

Modified network object

Return type:

pype_schema.node.Network

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: TagType

    units: str

    contents: str

    source_unit_id: int or str

    dest_unit_id: int or str

    totalized: 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:

ContentsType

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:

Network

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:

pype_schema.node.Network

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 int

max: float or int

design: float or int

units: str

}

Return type:

dict

static node_to_dict(node_obj)[source]

Converts a Node object to a dictionary that can be parsed into JSON format

Parameters:

node_obj (Node) – object to be converted into a dictionary

Raises:

TypeError – if node_obj is not a subclass of Node

Returns:

node_obj in dictionary form

Return type:

dict

parse_contents(id)[source]

Converts a dictionary into a tuple of input and output contents

Parameters:

id (str) – ID of the node to get the contents for

Returns:

(input_contents, output_contents)

Return type:

(str, str)

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: float

units: 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:

dict

static parse_heating_values(heating_vals)[source]

Converts a dictionary into a tuple of flow rates

Parameters:

heating_vals (dict) –

dictionary of the form {

lower: float

higher: float

units: str

}

Returns:

(lower, higher) heating values as a tuple, with units applied. Given as a float if no units are specified

Return type:

(pint.Quantity, pint.Quantity) or (float, float)

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: float

max: float

design: float

units: 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: TagType

    units: str

    contents: str

    source_unit_id: int or str

    dest_unit_id: int or str

    totalized: 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:

Tag

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 Tag

    operations: str

    type: TagType

    contents: 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:

VirtualTag

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:

node.Network

static tag_to_dict(tag_obj)[source]

Converts a Tag or VirtualTag object to a dictionary that can be parsed into JSON format

Parameters:

tag_obj (Tag or VirtualTag) – object to be converted into a dictionary

Raises:

TypeError – If tag_obj is not of type Tag or VirtualTag

Returns:

tag_obj in dictionary form

Return type:

dict

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:

dict

static unit_val_to_dict(attribute)[source]
Converts the given attribute to dictionary of the form {

value: float

units: str

}

Parameters:

attribute (pint.Quantity or float) – Attribute to represent as a dictionary

Returns:

Dictionary with keys value and units

Return type:

dict