I/O with JSON files

Krang has facilities to store all the relevant information they contain in structured JSON files. The essential structure of such file is this:

krangpower JSON v0.1.6

root
├──"ckt_name": <string>
├──"elements":
│   ├──"type1.name1":
│   │    ├──"type": <string>
│   │    ├──"name": <string>
│   │    ├──"properties": {<prop_name>: <value>}
│   │    ├──("units"): {<prop_name>: <unit_string>}
│   │    ├──("depends"): {<prop_name>: <name_string>}
│   │    ├──("topological"): [<bus_name_string>]
│   │    └──("hash"): <string> (*)
│   └──"type2.name2":
│       '.....
├──"settings":
│   ├──"values": {<setting_name>: <value>}
│   └──"units": {<setting_name>: <unit_string>}
└──"buscoords": {<bus_name_string>: [x y]}

# NOTES:
#  keys in parentheses are not necessarily present
#  (*) "hash" is used only for comparisons; when editing a json externally, it can be omitted

Full examples are available under the package’s /test folder. The methods here illustrated are documented in the Krang reference page too.

Saving

Krang can output such a json file through the Krang.save_json() method.

Krang.save_json(path=None, indent=2)

Saves a complete JSON description of the circuit and its objects. If a valid path string is passed, a file will be created, and None will be returned; if None is passed as path, a Text buffered Stream (io.StringIO) with the exact same information as the file will be returned instead.

Loading

Krang exposes a Krang.from_json(path) class method. It returns a Krang in the state syntesised by the JSON.

classmethod Krang.from_json(path, override_log=False)

Loads circuit data from a json structured like the ones returned by Krang.save_json. Declaration precedence due to dependency between objects is automatically taken care of.

Note

Inside the box, from_json(path) instantiates an empty Krang, adds all the components in the right order of dependency and finally returns it.