Loading Network Files
This guide walks through loading existing electrical network files using PyPtP. By the end, you'll understand how to read GNF and VNF files, access network elements, and save modified networks.
Full Example
View the complete code: 01_load_network.py
Prerequisites
This guide assumes familiarity with PyPtP's network types. See Overview for details on balanced vs unbalanced modeling.
Enable Logging
configure_logging(level="INFO")PyPtP is silent by default. This one-liner enables console output at INFO level, which is sufficient for most scripts.
More Options
See Logging Configuration for file logging, custom formats, and multiple handlers.
Load a Network File
# Load unbalanced network (GNF/Gaia format)
lv_network = NetworkLV.from_file("path/to/network.gnf")
logger.info("Unbalanced network: {} nodes, {} cables", len(lv_network.nodes), len(lv_network.cables))The from_file() class method handles all parsing internally:
- Reads the file contents
- Migrates legacy file versions if needed (e.g., older GNF/VNF formats)
- Parses the section-based format
- Registers elements on the network instance
Loading by File Format
# Unbalanced model (GNF/Gaia)
lv_network = NetworkLV.from_file("path/to/network.gnf")
# Balanced model (VNF/Vision)
mv_network = NetworkMV.from_file("path/to/network.vnf")Both file formats use identical section-based structure under the hood:
[SECTION_NAME]
#General GUID:'{...}' Name:'ElementName' Property:Value
#Presentation Sheet:'{...}' X:100 Y:200 Symbol:11
[]PyPtP abstracts this complexity. You simply call from_file() with the appropriate class.
Access Network Elements
for node in list(mv_network.nodes.values())[:5]:
logger.info("Node: {} at {}kV", node.general.name, node.general.unom)Element Collections
Network elements are stored in GUID-indexed dictionaries for O(1) lookup:
# Access by GUID
node = mv_network.nodes["{GUID-STRING}"]
# Iterate all elements
for guid, cable in lv_network.cables.items():
print(f"{cable.general.name}: {cable.general.length}m")For a complete list of available elements, see:
- LV Elements Reference (GNF/Gaia)
- MV Elements Reference (VNF/Vision)
Element Structure
Each element follows a consistent pattern with nested dataclasses:
node = mv_network.nodes[some_guid]
# General properties (name, GUID, voltage level)
print(node.general.name)
print(node.general.unom) # Nominal voltage in kV
# Presentation data (visual representation on sheets)
for presentation in node.presentations:
print(f"Sheet: {presentation.sheet}, Position: ({presentation.x}, {presentation.y})")Save the Network
lv_network.save("output.gnf")
mv_network.save("output.vnf")The save() method serializes the network back to the native format, preserving all element properties, presentations, and relationships.
Complete Example
"""Load and inspect existing network files."""
from pyptp import NetworkLV, NetworkMV, configure_logging
from pyptp.ptp_log import logger
# Enable logging to see output
configure_logging(level="INFO")
# Load unbalanced network (GNF/Gaia format)
lv_network = NetworkLV.from_file("path/to/network.gnf")
logger.info("Unbalanced network: {} nodes, {} cables", len(lv_network.nodes), len(lv_network.cables))
# Load balanced network (VNF/Vision format)
mv_network = NetworkMV.from_file("path/to/network.vnf")
logger.info("Balanced network: {} nodes, {} lines", len(mv_network.nodes), len(mv_network.lines))
# Access network elements
for node in list(mv_network.nodes.values())[:5]:
logger.info("Node: {} at {}kV", node.general.name, node.general.unom)
# Save modified network
lv_network.save("output.gnf")
mv_network.save("output.vnf")