Created
April 18, 2019 00:43
-
-
Save dvnoble/d4c49b840d68535a8843c0492797c739 to your computer and use it in GitHub Desktop.
edges2dot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" This script converts a graph formatted as an edge list | |
to a dot file. | |
""" | |
import argparse | |
parser = argparse.ArgumentParser(description='Converts an edge list file to a respective graphviz dot file.') | |
parser.add_argument('--directed', action='store_true', default=False, | |
help='indicates that the result should be a digraph (default: False)') | |
parser.add_argument('--labeled', action='store_true', default=False, | |
help='prints labels on edges (default: False)') | |
parser.add_argument('infile', type=argparse.FileType('r'), | |
help='the input file name.') | |
parser.add_argument('outfile', type=argparse.FileType('w'), | |
help='the output file name.') | |
def main(): | |
args = parser.parse_args() | |
if (args.directed): | |
head = 'digraph G {' | |
edge_symbol = '->' | |
else: | |
head = 'graph G { ' | |
edge_symbol = '--' | |
print(head,end='\n',file=args.outfile) | |
print('\tnode [shape=circle]', end='\n', file=args.outfile) | |
for line in args.infile: | |
s, d, w = line[:-1].split(',') | |
if (args.labeled): | |
print("\t{0} {1} {2} [label={3}];".format(s, edge_symbol, d, w), | |
end='\n', file=args.outfile) | |
else: | |
print("\t{0} {1} {2};".format(s, edge_symbol, d), | |
end='\n', file=args.outfile) | |
print('}', file=args.outfile) | |
args.infile.close() | |
args.outfile.close() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment