cfpq_data.graphs.readwrite.txt#

Read (and write) a graph from (and to) TXT file.

Functions

graph_from_text(text)

Returns a graph from text.

graph_from_txt(path)

Returns a graph loaded from a TXT file.

graph_to_text(graph, *[, quoting])

Turns a graph into its text representation.

graph_to_txt(graph, path, *[, quoting])

Returns a path to the TXT file where the graph will be saved.

graph_from_text(text: Iterable[str]) MultiDiGraph[source]#

Returns a graph from text.

Parameters:

text (Iterable[str]) -- The text with which the graph will be created.

Examples

>>> from cfpq_data import *
>>> g = graph_from_text(["1 A 2"])
>>> g.number_of_nodes()
2
>>> g.number_of_edges()
1
Returns:

g -- Loaded graph.

Return type:

MultiDiGraph

graph_from_txt(path: Path | str) MultiDiGraph[source]#

Returns a graph loaded from a TXT file.

Parameters:

path (Union[Path, str]) -- The path to the TXT file with which the graph will be created.

Examples

>>> from cfpq_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
Returns:

g -- Loaded graph.

Return type:

MultiDiGraph

graph_to_text(graph: MultiDiGraph, *, quoting: bool = False) Iterator[str][source]#

Turns a graph into its text representation.

Parameters:
  • graph (MultiDiGraph) -- Graph to text.

  • quoting (bool) -- If true, quotes will be added.

Examples

>>> from cfpq_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']
Returns:

text -- Generator of graph edges.

Return type:

str

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:
  • graph (MultiDiGraph) -- Graph to save.

  • path (Union[Path, str]) -- The path to the file where the graph will be saved.

  • quoting (bool) -- If true, quotes will be added.

Examples

>>> from cfpq_data import *
>>> g = labeled_cycle_graph(42, label="a")
>>> path = graph_to_txt(g, "test.txt", quoting=False)
Returns:

path -- Path to a TXT file where the graph will be saved.

Return type:

Path