Save, load and migrate¶
A network can be kept in two ways: as a project file — one network in one
JSON file, to mail, to version or to open in the GUI — or in a SQLite
database that holds a catalogue of bus and branch types and many networks.
This guide uses both, migrates a database written by an earlier release and
shows what happens to files from an untrusted source. The script is
persistence.py; its output is at the
end of the page.
station = gi.BusType(name="station", system_type="Station", voltage_level=20.0,
impedance_formula="2*rho/(3*1.13*sqrt(100.0))")
substation = gi.BusType(name="substation", system_type="Substation", voltage_level=20.0,
impedance_formula="0.5")
cable = gi.BranchType(name="cable", grounding_conductor=True,
self_impedance_formula="(0.2 + j*2*pi*f*1.2e-3)*l",
mutual_impedance_formula="(j*2*pi*f*1.0e-3)*l")
net = gi.create_network(name="Umspannwerk Süd", frequencies=[50.0, 250.0])
gi.create_bus(name="UW", type=substation, specific_earth_resistance=100.0, network=net)
gi.create_bus(name="S", type=station, specific_earth_resistance=100.0, network=net)
gi.create_branch(name="K", type=cable, from_bus="UW", to_bus="S", length=3.0, network=net)
gi.create_source(name="infeed", bus="UW", values={50.0: 1000.0, 250.0: 50.0}, network=net)
gi.create_fault(name="F", bus="S", scalings={50.0: 1.0, 250.0: 1.0}, network=net)
gi.run_fault(net, "F")
The project file¶
gi.save_network_to_json(network, path) writes the network — elements, types,
frequencies and the stored results — as JSON, and
gi.load_network_from_json(path) reads it back.
- UTF-8 on every platform. Names with
Ω,ρorüare stored the same on Windows, macOS and Linux. A file that is not UTF-8 — only 0.6.0 and earlier on a non-UTF-8 system wrote such files — is read with the platform encoding and announced (PROJECT_FILE_LEGACY_ENCODING); saving it again writes UTF-8. - Atomic. The file is written to a temporary file next to the target and then moved over it, so a failure leaves an existing project file intact.
- Versioned. The file starts with
schema_version(the version of the file layout) andgroundinsight_version(the release that wrote it).
gi.save_network_to_json(net, "project.json") # UTF-8, atomic, with schema_version
header = list(json.loads(Path("project.json").read_text(encoding="utf-8")))[:2]
from_json = gi.load_network_from_json("project.json")
Network.model_dump_json() and gi.Network.model_validate_json(text) remain
available for a network embedded in your own file format; the project-file
functions add the header, the encoding rules and the atomic write.
What is promised for old and new files¶
- Files of every earlier release load. A project file without
schema_version(0.6.0 and earlier) is read unchanged. A database is migrated to the current schema when it is opened, after a copy of the original is kept as<name>.db.bak. Results stored before the earthing voltage and impedance were redefined at the fault bus load with those quantities empty and aninforecordLEGACY_EARTHING_DEFINITIONinResult.diagnostics; solve the fault again. - Files of a newer schema are refused with a
ValueErrorthat names both schema versions, instead of being read with whatever the newer release added silently dropped. Update groundinsight to open them.
newer = json.loads(Path("project.json").read_text(encoding="utf-8"))
newer["schema_version"] = 99 # as if a later release wrote it
Path("from_the_future.json").write_text(json.dumps(newer), encoding="utf-8")
try:
gi.load_network_from_json("from_the_future.json")
except ValueError as refused:
newer_refused = str(refused).split(" (written")[0]
The database¶
gi.Database — gi.Database(path) — opens (or creates) a SQLite file and offers the
operations as methods: save_bustype, load_bustypes, save_branchtype,
load_branchtypes, save_network (with its types and stored results),
load_network, list_networks, delete_network, and their counterparts for
types. It is a context manager; every operation runs in a short-lived session
of its own, and two Database objects on the same file share a lock, so a
GUI or a server may use them from several threads. gi.Database(":memory:")
gives a private in-memory database. The module-level helpers
(gi.start_dbsession, gi.save_network_to_db, gi.load_network_from_db, …)
work as before on a default Database; the old globals gi.session and
gi.db_session still resolve but are deprecated. Details:
Database API.
for old_file in ("library.db", "library.db.bak"): # start from a fresh file
Path(old_file).unlink(missing_ok=True)
with gi.Database("library.db") as db:
db.save_bustype(station) # a reusable type catalogue
db.save_branchtype(cable)
db.save_network(net) # with its types and stored results
with gi.Database("library.db") as db:
stored = db.list_networks()
from_database = db.load_network("Umspannwerk Süd")
Migrate a database¶
needs_migration — gi.needs_migration(path) — classifies a file: "none" (current),
"add_columns" (columns of a later release missing), "legacy_scoped"
(elements keyed by name alone, up to 0.4.0) or "pickle_frequencies"
(frequency lists stored with pickle, up to 0.6.0).
migrate_database — gi.migrate_database(path, backup=True) — converts the file in place after
copying it to .bak, and returns a gi.MigrationReport — migrated, kind,
backup_path, the rows written, the columns added, and in needs_attention
whether anything was filled in or left out that you should look at.
gi.Database(path) does the same on opening (migrate=True, the default);
with migrate=False the file stays untouched and its first use raises an
error that names migrate().
connection = sqlite3.connect("library.db") # simulate a file of an earlier release:
connection.execute("ALTER TABLE bus_types DROP COLUMN electrode_current_split")
connection.commit()
connection.close()
kind_before = gi.needs_migration("library.db") # "add_columns"
report = gi.migrate_database("library.db") # copies library.db to library.db.bak first
kind_after = gi.needs_migration("library.db") # "none"
with gi.Database("library.db") as db: # Database(...) migrates on opening by default
after_migration = db.load_network("Umspannwerk Süd")
The script simulates a file of an earlier release by removing a column that a later release added; a real file from 0.5.0 lacks exactly such columns.
Files from an untrusted source¶
A .db or .json file is shared like a document — mailed, checked into a
repository, downloaded. Two properties keep opening one from running code:
- The formula guard. Impedance formulas are parsed with SymPy, which evaluates Python. Every formula — typed, imported or loaded — passes the formula guard first: arithmetic over names and numbers, a fixed list of functions, and bounds on the size of numbers, exponents and terms; string literals, attribute access, other calls and dunder names are refused (Formula syntax).
- No pickle. Databases store frequency lists as JSON. Files of 0.6.0 and
earlier stored them with pickle; the migration converts them with an
unpickler that resolves nothing but
list,floatandint, and aborts on anything else.
The guard bounds the work per formula, not the size of a file: a file with
many types and elements still takes time to load. A server that opens files
from strangers should do so in a worker process with a time limit; a session
of the service layer also refuses files above its
max_buses and max_branches on opening.
untrusted = json.loads(Path("project.json").read_text(encoding="utf-8"))
untrusted["buses"]["S"]["type"]["impedance_formula"] = "__import__('os').getcwd()"
Path("untrusted.json").write_text(json.dumps(untrusted), encoding="utf-8")
try:
gi.load_network_from_json("untrusted.json")
except ValueError as refused: # a pydantic ValidationError is a ValueError
untrusted_refused = re.search(r"Impedance formula [^(]*", str(refused)).group(0).strip()
Output¶
project file header: ['schema_version', 'groundinsight_version']
JSON round trip identical: True
newer schema: ValueError: 'from_the_future.json' uses project-file schema version 99
database holds: ['Umspannwerk Süd']
database round trip identical: True
migration: add_columns -> none, backup library.db.bak, added {'bus_types': ['electrode_current_split']}
after migration identical: True
stored EPR (RMS): {'UW': 44.46, 'S': 524.57}
untrusted formula refused: Impedance formula must not contain string literals or other non-arithmetic tokens
"Identical" compares the complete JSON serialisation of the network — every element, type, frequency and stored result — before saving and after loading.