#!/usr/bin/python # # edges.py # # Duplicates a list of directed edges to obtain an undirected graph. # # Copyright (c) 2006 Marco Colombo # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # See http://www.gnu.org/licenses/gpl.txt for a copy of the license. # import sys, string, re, fileinput # # main # def main(argv = None): # change argv if we are not running from the interactive prompt if argv is None: argv = sys.argv # check if we have the correct number of arguments if len(argv) != 2: print "Usage: edges.py " return 1 try: # go through the input file for line in fileinput.input(): # skip lines that are empty or begin with "#" if re.match("^\s*$|^#", line): continue # split the line fr, to, length = string.split(line) try: # print the existing arc print "%3d %3d %f" % (int(fr), int(to), float(length)) # print the reversed arc print "%3d %3d %f" % (int(to), int(fr), float(length)) except ValueError: pass except IOError: print "Could not open file '" + fileinput.filename() + "'." if __name__ == "__main__": sys.exit(main())