diff options
author | Jeroen Bergmans <bergmansj@users.noreply.github.com> | 2021-06-07 11:36:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-07 12:36:23 +0300 |
commit | 251fa09289ee1e105295101a6a58f674eeb0fd92 (patch) | |
tree | e383ec12476fe3e3faca49f575d1f6293804a72a /networkx | |
parent | b173f2bb323c798c979a2963e8a05d67c9c1f3d7 (diff) | |
download | networkx-251fa09289ee1e105295101a6a58f674eeb0fd92.tar.gz |
Deserializing custom default properties graph ml (#4872)
* Add test case for handling default node attributes from graphML
* Add decode conversion to key defaults analogous to decode_data_elements
Diffstat (limited to 'networkx')
-rw-r--r-- | networkx/readwrite/graphml.py | 11 | ||||
-rw-r--r-- | networkx/readwrite/tests/test_graphml.py | 39 |
2 files changed, 48 insertions, 2 deletions
diff --git a/networkx/readwrite/graphml.py b/networkx/readwrite/graphml.py index 1ecaebdd..1a463b54 100644 --- a/networkx/readwrite/graphml.py +++ b/networkx/readwrite/graphml.py @@ -962,8 +962,15 @@ class GraphMLReader(GraphML): "type": self.python_type[attr_type], "for": k.get("for"), } - # check for "default" subelement of key element + # check for "default" sub-element of key element default = k.find(f"{{{self.NS_GRAPHML}}}default") if default is not None: - graphml_key_defaults[attr_id] = default.text + # Handle default values identically to data element values + python_type = graphml_keys[attr_id]["type"] + if python_type == bool: + graphml_key_defaults[attr_id] = self.convert_bool[ + default.text.lower() + ] + else: + graphml_key_defaults[attr_id] = python_type(default.text) return graphml_keys, graphml_key_defaults diff --git a/networkx/readwrite/tests/test_graphml.py b/networkx/readwrite/tests/test_graphml.py index fbbc1953..beb62265 100644 --- a/networkx/readwrite/tests/test_graphml.py +++ b/networkx/readwrite/tests/test_graphml.py @@ -119,6 +119,40 @@ class BaseGraphML: cls.attribute_graph.add_edge("n5", "n4", id="e6", weight=1.1) cls.attribute_fh = io.BytesIO(cls.attribute_data.encode("UTF-8")) + cls.node_attribute_default_data = """<?xml version="1.0" encoding="UTF-8"?> + <graphml xmlns="http://graphml.graphdrawing.org/xmlns" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns + http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> + <key id="d0" for="node" attr.name="boolean_attribute" attr.type="boolean"><default>false</default></key> + <key id="d1" for="node" attr.name="int_attribute" attr.type="int"><default>0</default></key> + <key id="d2" for="node" attr.name="long_attribute" attr.type="long"><default>0</default></key> + <key id="d3" for="node" attr.name="float_attribute" attr.type="float"><default>0.0</default></key> + <key id="d4" for="node" attr.name="double_attribute" attr.type="double"><default>0.0</default></key> + <key id="d5" for="node" attr.name="string_attribute" attr.type="string"><default>Foo</default></key> + <graph id="G" edgedefault="directed"> + <node id="n0"/> + <node id="n1"/> + <edge id="e0" source="n0" target="n1"/> + </graph> + </graphml> + """ + cls.node_attribute_default_graph = nx.DiGraph(id="G") + cls.node_attribute_default_graph.graph["node_default"] = { + "boolean_attribute": False, + "int_attribute": 0, + "long_attribute": 0, + "float_attribute": 0.0, + "double_attribute": 0.0, + "string_attribute": "Foo", + } + cls.node_attribute_default_graph.add_node("n0") + cls.node_attribute_default_graph.add_node("n1") + cls.node_attribute_default_graph.add_edge("n0", "n1", id="e0") + cls.node_attribute_default_fh = io.BytesIO( + cls.node_attribute_default_data.encode("UTF-8") + ) + cls.attribute_named_key_ids_data = """<?xml version='1.0' encoding='utf-8'?> <graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" @@ -351,6 +385,11 @@ class TestReadGraphML(BaseGraphML): for a, b in zip(ge, he): assert a == b + def test_node_default_attribute_graphml(self): + G = self.node_attribute_default_graph + H = nx.read_graphml(self.node_attribute_default_fh) + assert G.graph["node_default"] == H.graph["node_default"] + def test_directed_edge_in_undirected(self): s = """<?xml version="1.0" encoding="UTF-8"?> <graphml xmlns="http://graphml.graphdrawing.org/xmlns" |