Skip to content

Commit

Permalink
Add GML handling for bool and tuple types (#2513)
Browse files Browse the repository at this point in the history
* Add GML handling for bool and tuple types

Fixes: #1973 #2118 #1985

* Fix pypy test by using `!=` instead of `is not`
  • Loading branch information
dschult committed Jul 15, 2017
1 parent fda9281 commit 0a947aa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
21 changes: 16 additions & 5 deletions networkx/readwrite/gml.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,12 @@ def literal_stringizer(value):
"""
def stringize(value):
if isinstance(value, (int, long, bool)) or value is None:
buf.write(str(value))
if value is True: # GML uses 1/0 for boolean values.
buf.write(str(1))
elif value is False:
buf.write(str(0))
else:
buf.write(str(value))
elif isinstance(value, unicode):
text = repr(value)
if text[0] != 'u':
Expand Down Expand Up @@ -653,9 +658,14 @@ def stringize(key, value, ignored_keys, indent, in_list=False):
if not isinstance(key, str):
key = str(key)
if key not in ignored_keys:
if isinstance(value, (int, long)):
if isinstance(value, (int, long, bool)):
if key == 'label':
yield indent + key + ' "' + str(value) + '"'
elif value is True:
# python bool is an instance of int
yield indent + key + ' 1'
elif value is False:
yield indent + key + ' 0'
else:
yield indent + key + ' ' + str(value)
elif isinstance(value, float):
Expand All @@ -677,10 +687,11 @@ def stringize(key, value, ignored_keys, indent, in_list=False):
for line in stringize(key, value, (), next_indent):
yield line
yield indent + ']'
elif isinstance(value, list) and value and not in_list:
elif isinstance(value, (list, tuple)) and key != 'label' \
and value and not in_list:
next_indent = indent + ' '
for value in value:
for line in stringize(key, value, (), next_indent, True):
for val in value:
for line in stringize(key, val, (), next_indent, True):
yield line
else:
if stringizer:
Expand Down
4 changes: 2 additions & 2 deletions networkx/readwrite/tests/test_gml.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ def test_graph_types(self):
assert_equal(gml, '\n'.join(nx.generate_gml(G)))

def test_data_types(self):
data = [10 ** 20, -2e33, "'", '"&&&""',
[{(b'\xfd',): '\x7f', unichr(0x4444): (1, 2)}]]
data = [True, False, 10 ** 20, -2e33, "'", '"&&&""',
[{(b'\xfd',): '\x7f', unichr(0x4444): (1, 2)}, (2, "3")]]
try:
data.append(unichr(0x14444)) # fails under IronPython
except ValueError:
Expand Down

0 comments on commit 0a947aa

Please sign in to comment.