Skip to content

Commit 252dcce

Browse files
committed
chore: format code
1 parent 83b4869 commit 252dcce

File tree

1 file changed

+54
-24
lines changed

1 file changed

+54
-24
lines changed

src/ssspx/io.py

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,20 @@ def _iter_edges(G: Graph) -> Iterable[Tuple[int, int, float]]:
3434

3535
def _read_csv(path: Path) -> Tuple[int, EdgeList]:
3636
"""
37-
Reads a CSV file containing edge data and returns the number of nodes and the edge list.
37+
Reads a CSV file containing edge data and returns the number of nodes
38+
and the edge list.
3839
39-
Each line in the file should contain at least three columns: source node, target node, and edge weight.
40-
Lines starting with '#' or empty lines are ignored. Columns can be separated by commas or tabs.
40+
Each line in the file should contain at least three columns: source node,
41+
target node, and edge weight.
42+
Lines starting with '#' or empty lines are ignored. Columns can be
43+
separated by commas or tabs.
4144
4245
Args:
4346
path (Path): The path to the CSV file.
4447
4548
Returns:
46-
Tuple[int, EdgeList]: A tuple containing the number of nodes (max node id + 1) and a list of edges,
49+
Tuple[int, EdgeList]: A tuple containing the number of nodes
50+
(max node id + 1) and a list of edges,
4751
where each edge is represented as a tuple (u, v, w).
4852
4953
Raises:
@@ -76,7 +80,8 @@ def _write_csv(path: Path, G: Graph) -> None:
7680
"""
7781
Writes the edges of a graph to a CSV file.
7882
79-
Each row in the CSV file represents an edge in the graph, with columns for the source node, target node, and edge weight.
83+
Each row in the CSV file represents an edge in the graph, with columns for
84+
the source node, target node, and edge weight.
8085
8186
Args:
8287
path (Path): The file path where the CSV will be written.
@@ -92,7 +97,7 @@ def _write_csv(path: Path, G: Graph) -> None:
9297

9398
def _read_jsonl(path: Path) -> Tuple[int, EdgeList]:
9499
"""
95-
Reads a JSON Lines (JSONL) file containing graph edges and returns the
100+
Reads a JSON Lines (JSONL) file containing graph edges and returns the
96101
number of nodes and the edge list.
97102
98103
Each line in the file should be a JSON object with keys "u", "v", and "w",
@@ -154,14 +159,18 @@ def _read_mtx(path: Path) -> Tuple[int, EdgeList]:
154159
path (Path): Path to the Matrix Market file.
155160
156161
Returns:
157-
Tuple[int, EdgeList]: A tuple containing the number of nodes (n) and the edge list.
158-
The edge list is a list of tuples (u, v, w), where u and v are zero-based node indices,
159-
and w is the edge weight.
162+
Tuple[int, EdgeList]: (
163+
A tuple containing the number of nodes (n) and the edge list.
164+
The edge list is a list of tuples (u, v, w), where u and v
165+
are zero-based node indices, and w is the edge weight.
166+
)
160167
161168
Notes:
162169
- Lines starting with '%' are treated as comments and skipped.
163-
- Assumes the file contains at least three columns: source, target, and weight.
164-
- Node indices in the file are assumed to be 1-based and are converted to 0-based.
170+
- Assumes the file contains at least three columns: source, target,
171+
and weight.
172+
- Node indices in the file are assumed to be 1-based and are
173+
converted to 0-based.
165174
"""
166175
edges: EdgeList = []
167176
it = path.open("r", encoding="utf-8")
@@ -187,14 +196,17 @@ def _read_mtx(path: Path) -> Tuple[int, EdgeList]:
187196

188197
def _write_mtx(path: Path, G: Graph) -> None:
189198
"""
190-
Writes the given graph `G` to a Matrix Market (.mtx) file at the specified path.
199+
Writes the given graph `G` to a Matrix Market (.mtx) file at the specified
200+
path.
191201
192-
The output file will contain the graph's adjacency matrix in coordinate format,
193-
where each line represents an edge with its source node, target node, and weight.
194-
Node indices are written as 1-based (Matrix Market convention).
202+
The output file will contain the graph's adjacency matrix in coordinate
203+
format, where each line represents an edge with its source node, target
204+
node, and weight. Node indices are written as 1-based (Matrix Market
205+
convention).
195206
196207
Args:
197-
path (Path): The file path where the Matrix Market file will be written.
208+
path (Path): The file path where the Matrix Market file will be
209+
written.
198210
G (Graph): The graph object containing nodes and weighted edges.
199211
200212
Returns:
@@ -215,9 +227,10 @@ def _read_graphml(path: Path) -> Tuple[int, EdgeList]:
215227
Args:
216228
path (Path): Path to the GraphML file.
217229
Returns:
218-
Tuple[int, EdgeList]: A tuple containing the number of nodes (as max node id + 1)
219-
and a list of edges, where each edge is represented as a tuple (u, v, w) with
220-
integer node IDs and float weights.
230+
Tuple[int, EdgeList]: A tuple containing the number of nodes
231+
(as max node id + 1) and a list of edges, where each edge is
232+
represented as a tuple (u, v, w) with integer node IDs and
233+
float weights.
221234
Raises:
222235
GraphFormatError: If no edges are parsed from the file.
223236
"""
@@ -252,7 +265,21 @@ def _read_graphml(path: Path) -> Tuple[int, EdgeList]:
252265

253266

254267
def _write_graphml(path: Path, G: Graph) -> None:
255-
"""Write ``G`` as a GraphML edges file."""
268+
"""
269+
Writes the given graph `G` to a GraphML file at the specified `path`.
270+
271+
Args:
272+
path (Path): The file path where the GraphML output will be written.
273+
G (Graph): The graph object to serialize, expected to have an
274+
attribute `n` for the number of nodes.
275+
276+
Notes:
277+
- The function assumes the graph is directed.
278+
- Each node is assigned an ID in the format "n{i}".
279+
- Edges are written with source, target, and weight attributes.
280+
- The helper function `_iter_edges(G)` should yield tuples of (u, v, w)
281+
for each edge.
282+
"""
256283
lines: List[str] = []
257284
lines.append('<?xml version="1.0" encoding="UTF-8"?>')
258285
lines.append('<graphml xmlns="http://graphml.graphdrawing.org/xmlns">')
@@ -289,8 +316,9 @@ def _detect_format(path: Path) -> Optional[str]:
289316
path (Path): The path to the file whose format is to be detected.
290317
291318
Returns:
292-
Optional[str]: The detected format as a string ("csv", "jsonl", "mtx", "graphml"),
293-
or None if the format is not recognized.
319+
Optional[str]: The detected format as a string
320+
("csv", "jsonl", "mtx", "graphml"), or None if the format is not
321+
recognized.
294322
"""
295323
ext = path.suffix.lower()
296324
if ext in {".csv", ".tsv"}:
@@ -310,7 +338,8 @@ def read_graph(path: str, fmt: Optional[str] = None) -> Graph:
310338
311339
Args:
312340
path (str): The path to the graph file.
313-
fmt (Optional[str], optional): The format of the graph file. If None, the format is auto-detected.
341+
fmt (Optional[str], optional): The format of the graph file. If None,
342+
the format is auto-detected.
314343
315344
Returns:
316345
Graph: The graph object constructed from the file.
@@ -333,7 +362,8 @@ def write_graph(G: Graph, path: str, fmt: Optional[str] = None) -> None:
333362
Parameters:
334363
G (Graph): The graph object to be written.
335364
path (str): The file path where the graph will be saved.
336-
fmt (Optional[str], optional): The format to use for writing the graph. If None, the format is auto-detected from the file extension.
365+
fmt (Optional[str], optional): The format to use for writing the graph.
366+
If None, the format is auto-detected from the file extension.
337367
338368
Raises:
339369
GraphFormatError: If the format is unknown or unsupported.

0 commit comments

Comments
 (0)