>>> from networkx import *
>>> import os,tempfile

Graph6
======

>>> data="""DF{"""

>>> G=parse_graph6(data)

>>> print sorted(G.nodes())
[0, 1, 2, 3, 4]

>>> print [e for e in sorted(G.edges())]
[(0, 3), (0, 4), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]


>>> (fd,fname)=tempfile.mkstemp()
>>> fh=open(fname,'w')
>>> fh.write(data)
>>> fh.close()
>>> Gin=read_graph6(fname)
>>> sorted(G.nodes())==sorted(Gin.nodes())
True
>>> sorted(G.edges())==sorted(Gin.edges())
True
>>> os.close(fd)
>>> os.unlink(fname)

Read many graphs into list

>>> data="""DF{\nD`{\nDqK\nD~{\n"""

>>> (fd,fname)=tempfile.mkstemp()
>>> fh=open(fname,'w')
>>> fh.write(data)
>>> fh.close()
>>> glist=read_graph6_list(fname)
>>> print len(glist)
4
>>> for G in glist:
...     print G.nodes()
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]

>>> os.close(fd)
>>> os.unlink(fname)

Sparse6
=======

>>> data=""":Q___eDcdFcDeFcE`GaJ`IaHbKNbLM"""

>>> G=parse_sparse6(data)

>>> print sorted(G.nodes())
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]

>>> print [e for e in sorted(G.edges())]
[(0, 1), (0, 2), (0, 3), (1, 12), (1, 14), (2, 13), (2, 15), (3, 16), (3, 17), (4, 7), (4, 9), (4, 11), (5, 6), (5, 8), (5, 9), (6, 10), (6, 11), (7, 8), (7, 10), (8, 12), (9, 15), (10, 14), (11, 13), (12, 16), (13, 17), (14, 17), (15, 16)]


>>> (fd,fname)=tempfile.mkstemp()
>>> fh=open(fname,'w')
>>> fh.write(data)
>>> fh.close()
>>> Gin=read_sparse6(fname)
>>> sorted(G.nodes())==sorted(Gin.nodes())
True
>>> sorted(G.edges())==sorted(Gin.edges())
True
>>> os.close(fd)
>>> os.unlink(fname)

Read many graphs into list

>>> data=""":Q___eDcdFcDeFcE`GaJ`IaHbKNbLM\n:Q___dCfDEdcEgcbEGbFIaJ`JaHN`IM"""

>>> (fd,fname)=tempfile.mkstemp()
>>> fh=open(fname,'w')
>>> fh.write(data)
>>> fh.close()
>>> glist=read_sparse6_list(fname)
>>> print len(glist)
2
>>> for G in glist:
...     print G.nodes()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]

>>> os.close(fd)
>>> os.unlink(fname)


