Parse GML graph from a string or iterable. Parameters ---------- lines : string or iterable of strings Data in GML format. label : string, optional If not None, the parsed nodes will be renamed according to node attributes indicated by `label`. Default value:
(lines, label="label", destringizer=None)
| 142 | |
| 143 | |
| 144 | def parse_gml(lines, label="label", destringizer=None): |
| 145 | """Parse GML graph from a string or iterable. |
| 146 | |
| 147 | Parameters |
| 148 | ---------- |
| 149 | lines : string or iterable of strings |
| 150 | Data in GML format. |
| 151 | |
| 152 | label : string, optional |
| 153 | If not None, the parsed nodes will be renamed according to node |
| 154 | attributes indicated by `label`. Default value: 'label'. |
| 155 | |
| 156 | destringizer : callable, optional |
| 157 | A `destringizer` that recovers values stored as strings in GML. If it |
| 158 | cannot convert a string to a value, a `ValueError` is raised. Default |
| 159 | value : None. |
| 160 | |
| 161 | Returns |
| 162 | ------- |
| 163 | G : EasyGraph graph |
| 164 | The parsed graph. |
| 165 | |
| 166 | Raises |
| 167 | ------ |
| 168 | EasyGraphError |
| 169 | If the input cannot be parsed. |
| 170 | |
| 171 | See Also |
| 172 | -------- |
| 173 | write_gml, read_gml |
| 174 | |
| 175 | Notes |
| 176 | ----- |
| 177 | This stores nested GML attributes as dictionaries in the EasyGraph graph, |
| 178 | node, and edge attribute structures. |
| 179 | |
| 180 | GML files are stored using a 7-bit ASCII encoding with any extended |
| 181 | ASCII characters (iso8859-1) appearing as HTML character entities. |
| 182 | Without specifying a `stringizer`/`destringizer`, the code is capable of |
| 183 | writing `int`/`float`/`str`/`dict`/`list` data as required by the GML |
| 184 | specification. For writing other data types, and for reading data other |
| 185 | than `str` you need to explicitly supply a `stringizer`/`destringizer`. |
| 186 | |
| 187 | For additional documentation on the GML file format, please see the |
| 188 | `GML url <https://web.archive.org/web/20190207140002/http://www.fim.uni-passau.de/index.php?id=17297&L=1>`_. |
| 189 | |
| 190 | See the module docstring :mod:`easygraph.readwrite.gml` for more details. |
| 191 | """ |
| 192 | |
| 193 | def decode_line(line): |
| 194 | if isinstance(line, bytes): |
| 195 | try: |
| 196 | line.decode("ascii") |
| 197 | except UnicodeDecodeError as err: |
| 198 | raise EasyGraphError("input is not ASCII-encoded") from err |
| 199 | if not isinstance(line, str): |
| 200 | line = str(line) |
| 201 | return line |
nothing calls this directly
no test coverage detected