Skip to content

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.

0

Import the Required Modules

python
from pyptp import NetworkLV, NetworkMV, configure_logging
from pyptp.ptp_log import logger

What's being imported?

  • NetworkMV: Balanced network model for VNF files (Vision format)
  • NetworkLV: Unbalanced network model for GNF files (Gaia format)
  • configure_logging: Enables PyPtP's logging output (silent by default)
  • logger: The Loguru logger instance for consistent output
0

Enable Logging

python
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.

0

Load a Network File

python
# 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:

  1. Reads the file contents
  2. Migrates legacy file versions if needed (e.g., older GNF/VNF formats)
  3. Parses the section-based format
  4. Registers elements on the network instance

Loading by File Format

python
# 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:

text
[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.

0

Access Network Elements

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

python
# 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:

Element Structure

Each element follows a consistent pattern with nested dataclasses:

python
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})")
0

Save the Network

python
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

python
"""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")

Next Steps