flpq_data.graphs.readwrite.txt#
Read (and write) a graph from (and to) TXT file.
Functions
|
Returns a graph from text. |
|
Returns a graph loaded from a TXT file. |
|
Turns a graph into its text representation. |
|
Returns a path to the TXT file where the graph will be saved. |
|
Yields the edges of a text representation as a stream. |
|
Yields the edges of a TXT file as a stream. |
|
Writes an edge stream to a TXT file. |
- graph_from_text(text: Iterable[str]) MultiDiGraph[source]#
Returns a graph from text.
- Parameters:
- textIterable[str]
The text with which the graph will be created.
- Returns:
- gMultiDiGraph
Loaded graph.
Examples
>>> from flpq_data import * >>> g = graph_from_text(["1 A 2"]) >>> g.number_of_nodes() 2 >>> g.number_of_edges() 1
- graph_from_txt(path: Path | str) MultiDiGraph[source]#
Returns a graph loaded from a TXT file.
- Parameters:
- pathUnion[Path, str]
The path to the TXT file with which the graph will be created.
- Returns:
- gMultiDiGraph
Loaded graph.
Examples
>>> from flpq_data import * >>> g_1 = graph_from_text(["1 A 2"]) >>> p = graph_to_txt(g_1, "test.txt") >>> g = graph_from_txt(p) >>> g.number_of_nodes() 2 >>> g.number_of_edges() 1
- graph_to_text(graph: MultiDiGraph, *, quoting: bool = False) Iterator[str][source]#
Turns a graph into its text representation.
One line per edge; the label is taken from the edge’s
labelattribute (the same convention asgraph_to_mtx_dir).- Parameters:
- graphMultiDiGraph
Graph to text.
- quotingbool
If true, quotes will be added.
- Returns:
- textstr
Generator of graph edges.
Examples
>>> from flpq_data import * >>> g = labeled_cycle_graph(2) >>> list(graph_to_text(g, quoting=True)) ["'0' 'a' '1'", "'1' 'a' '0'"] >>> list(graph_to_text(g, quoting=False)) ['0 a 1', '1 a 0']
- graph_to_txt(graph: MultiDiGraph, path: Path | str, *, quoting: bool = False) Path[source]#
Returns a path to the TXT file where the graph will be saved.
- Parameters:
- graphMultiDiGraph
Graph to save.
- path: Union[Path, str]
The path to the file where the graph will be saved.
- quotingbool
If true, quotes will be added.
- Returns:
- pathPath
Path to a TXT file where the graph will be saved.
Examples
>>> from flpq_data import * >>> g = labeled_cycle_graph(42, label="a") >>> path = graph_to_txt(g, "test.txt", quoting=False)
- iter_edges_from_text(text: Iterable[str]) Iterator[Tuple[str, str, str]][source]#
Yields the edges of a text representation as a stream.
Each line is
FROM LABEL TO— three whitespace-separated tokens; quoting is allowed, as in shell syntax.- Parameters:
- textIterable[str]
The lines with which the edges will be created.
- Returns:
- edgesIterator[Tuple[str, str, str]]
The
(u, label, v)edge tuples in line order.
- Raises:
- ValueError
If a line does not match the
FROM LABEL TOformat.
Examples
>>> list(iter_edges_from_text(["1 A 2", "'3' 'b_5' '4'"])) [('1', 'A', '2'), ('3', 'b_5', '4')]
- iter_edges_from_txt(path: Path | str) Iterator[Tuple[str, str, str]][source]#
Yields the edges of a TXT file as a stream.
The file holds one
FROM LABEL TOedge per line (seeiter_edges_from_text()).- Parameters:
- pathUnion[Path, str]
The path to the TXT file with which the edges will be created.
- Returns:
- edgesIterator[Tuple[str, str, str]]
The
(u, label, v)edge tuples in line order.
Examples
>>> import pathlib, tempfile >>> p = pathlib.Path(tempfile.mkdtemp()) / "g.txt" >>> _ = p.write_text("1 A 2\n3 b_5 4\n") >>> list(iter_edges_from_txt(p)) [('1', 'A', '2'), ('3', 'b_5', '4')]
- txt_from_edges(edges: Iterable[Tuple[str, str, str]], path: Path | str, *, quoting: bool = False) Path[source]#
Writes an edge stream to a TXT file.
One
FROM LABEL TOline per edge (the format ofiter_edges_from_text()); the memory stays O(1) in the number of edges.- Parameters:
- edgesIterable[Tuple[str, str, str]]
The
(u, label, v)edge tuples to write.- pathUnion[Path, str]
The path to the TXT file where the edges will be saved.
- quotingbool
If true, quotes will be added.
- Returns:
- pathPath
Path to the TXT file where the edges will be saved.
Examples
>>> import pathlib, tempfile >>> p = pathlib.Path(tempfile.mkdtemp()) / "g.txt" >>> _ = txt_from_edges([("1", "A", "2")], p) >>> p.read_text() '1 A 2\n'