The PywrNetwork class

class pywrparser.types.network.PywrNetwork(parser)

An abstract representation of a Pywr network.

classmethod from_file(filename, raise_on_parser_error=False, raise_on_parser_warning=False, ignore_warnings=False, allow_duplicate_edges=True, ruleset=None)

Returns either the valid PywrNetwork contained in the file denoted by the filename argument, or corresponding errors encountered during parsing.

Parameters
  • filename (str) – The filename of a file containing a JSON definition of a Pywr network.

  • raise_on_parser_error (bool) – Specifies whether parsing errors should be raised immediately as exceptions or collected in the errors return value.

  • raise_on_parser_warning (bool) – Specifies whether warnings encountered during parsing should be raised immediately as exceptions or collected in the warnings return value.

  • allow_duplicate_edges (bool) – Specifies whether duplicate edges are considered as errors or are permitted in a valid networks.

  • ruleset (str) – The key of a valid ruleset. This ruleset will then be applied during parsing.

Returns

network, errors, warnings (Tuple[PywrNetwork, Dict, Dict]) – in which either one of network or errors is not None. warnings may be present in either case.

classmethod from_json(json_src, raise_on_parser_error=False, raise_on_parser_warning=False, ignore_warnings=False, allow_duplicate_edges=True, ruleset=None)

Returns either the valid PywrNetwork represented by the JSON encoded string contained in the json_src argument, or corresponding errors encountered during parsing.

Parameters
  • json_src (str) – A string containing a JSON encoded representation of a Pywr network.

  • raise_on_parser_error (bool) – Specifies whether parsing errors should be raised immediately as exceptions or collected in the errors return value.

  • raise_on_parser_warning (bool) – Specifies whether warnings encountered during parsing should be raised immediately as exceptions or collected in the warnings return value.

  • allow_duplicate_edges (bool) – Specifies whether duplicate edges are considered as errors or are permitted in a valid networks.

  • ruleset (str) – The key of a valid ruleset. This ruleset will then be applied during parsing.

Returns

network, errors, warnings (Tuple[PywrNetwork, Dict, Dict]) – in which either one of network or errors is not None. warnings may be present in either case.

as_dict()
Returns

network (dict) – A dict representation of the PywrNetwork instance.

as_json()
Returns

network (str) – A JSON encoded representation of the PywrNetwork instance.

promote_inline_parameters()

Promotes inline parameter definitions to instances of PywrParameter.

Any values of attrs in a node which can be interpreted as an inline (i.e. dict) parameter definition are instantiated as parameters and the attr value replaced with the instance.

promote_inline_recorders()

Promotes inline recorder definitions to instances of PywrRecorder.

Any values of attrs in a node which can be interpreted as an inline (i.e. dict) recorder definition are instantiated as recorders and the attr value replaced with the instance.

attach_reference_parameters()

Promotes strings which reference parameters to instances of PywrParameter.

Any values of attrs in a node which resolve to a global parameter are replaced with the instance of that parameter.

attach_reference_recorders()

Promotes strings which reference recorders to instances of PywrRecorder.

Any values of attrs in a node which resolve to a global recorder are replaced with the instance of that recorder.

add_parameter_references(*, component='parameter')

Where a parameter name is in the format “__nodename__:attrname” it is interpreted as representing the attr “attrname” on node “nodename”. In this case, a reference to the global parameter is added to the node where this is not already present.

Partial application of the internal __add_component_references() function.

add_recorder_references(*, component='recorder')

Where a recorder name is in the format “__nodename__:attrname” it is interpreted as representing the attr “attrname” on node “nodename”. In this case, a reference to the global recorder is added to the node where this is not already present.

Partial application of the internal __add_component_references() function.

detach_parameters()

Removes any parameter instances from nodes and replaces these by a string referencing the name of the parameter.

detach_recorders()

Removes any recorder instances from nodes and replaces these by a string referencing the name of the recorder.

report()
Returns

report (dict) – Contains a key for each network component whose associated value is the number of instances of that component type.

verbose_report()
property title
property description
property duplicate_edges

Return a dict of “duplicate” edges, that is edges of length n comprised of the same n nodes in the same order which are defined more than once. This is permitted by Pywr but may indicate a malformed network in some enviroments.

url_references()

Return a dict of urls references by parameters, each mapping to a list of the parameters containing that reference.