#!/usr/bin python
"""
Author : Yinfei Yang
Mail : yangyin7 AT gmail DOT com
Date : 2010-11-12 Fri 04:52 PM
python version: 2.6.5
Usage 1: python convert.py inputFileName outputFileName
Usage 2: python convert.py dirName
Notice: Please ensure the file need to be converted with the suffix .prb
example of input file:
20 15
0 136 1 189 2 121 3 183 4 77 5 133 6 77 7 155 8 68 9 97 10 99 11 37 12 51 13 149 14 132
0 2 1 7 2 93 3 99 4 42 5 66 6 190 7 174 8 94 9 171 10 7 11 129 12 60 13 56 14 12
0 167 1 68 2 176 3 30 4 99 5 107 6 152 7 184 8 186 9 15 10 145 11 21 12 199 13 43 14 135
0 137 1 162 2 98 3 2 4 92 5 123 6 171 7 100 8 191 9 57 10 166 11 168 12 59 13 145 14 73
0 7 1 197 2 192 3 53 4 146 5 104 6 169 7 182 8 98 9 145 10 80 11 133 12 5 13 16 14 146
0 166 1 73 2 133 3 35 4 141 5 90 6 136 7 184 8 135 9 3 10 195 11 132 12 178 13 7 14 162
0 197 1 51 2 137 3 136 4 121 5 15 6 160 7 179 8 20 9 44 10 159 11 38 12 55 13 55 14 170
0 151 1 153 2 82 3 40 4 97 5 200 6 165 7 69 8 29 9 49 10 30 11 30 12 84 13 78 14 87
0 171 1 155 2 123 3 131 4 168 5 155 6 145 7 155 8 135 9 66 10 89 11 19 12 4 13 134 14 161
0 183 1 161 2 165 3 116 4 184 5 157 6 175 7 48 8 85 9 165 10 94 11 149 12 84 13 138 14 137
0 114 1 40 2 6 3 170 4 163 5 118 6 79 7 154 8 195 9 34 10 197 11 45 12 39 13 125 14 160
0 65 1 105 2 24 3 31 4 90 5 22 6 59 7 66 8 20 9 150 10 37 11 101 12 139 13 87 14 138
0 110 1 37 2 73 3 25 4 38 5 46 6 11 7 123 8 195 9 164 10 86 11 191 12 184 13 120 14 43
0 50 1 181 2 23 3 134 4 198 5 169 6 186 7 75 8 141 9 62 10 173 11 123 12 192 13 29 14 124
0 67 1 80 2 155 3 166 4 25 5 4 6 11 7 184 8 32 9 64 10 16 11 108 12 31 13 4 14 145
0 149 1 195 2 125 3 118 4 18 5 186 6 22 7 15 8 118 9 60 10 176 11 82 12 94 13 170 14 27
0 174 1 126 2 49 3 77 4 92 5 147 6 171 7 18 8 193 9 14 10 171 11 50 12 95 13 24 14 104
0 156 1 1 2 158 3 192 4 155 5 99 6 62 7 82 8 115 9 41 10 33 11 139 12 77 13 61 14 161
0 49 1 179 2 183 3 176 4 164 5 165 6 93 7 194 8 46 9 59 10 199 11 197 12 5 13 143 14 23
0 5 1 185 2 92 3 173 4 116 5 81 6 131 7 85 8 115 9 98 10 135 11 163 12 68 13 179 14 1
"""
import sys, re, os
DATA_START = "data([\n"
DATA_END = "])."
def convert(inputFile, outputFile):
print ''.join(['Start to convert ',inputFile,' ...'])
f = open(inputFile,'r')
f2 = open(outputFile,'w')
f2.write(DATA_START)
file_lines = f.readlines()
num = file_lines[0].replace('\n','').split(' ')
if len(num) != 2:
print 'Error 2:'
print 'prb file format error, please check it, exit.'
sys.exit(2)
row = int(num[0])
column = int(num[1])
count = row * column
current_job = 0
for line in file_lines[1:len(file_lines)]:
tasks = re.split('[ ]+',line.replace('\n','').strip())
task_number = len(tasks)/2
if(len(tasks) != column*2):
print 'Error 2:'
print 'prb file format error, please check it exit'
sys.exit(2)
for i in range(0,task_number):
current_job += 1
f2.write(''.join(['task( ' , str(current_job) , ', ' , tasks[2*i+1] , ', [']))
if current_job%column==0:
current_line = current_job/column
else:
current_line = current_job/column+1
for j in range(1, current_job - (current_line-1)*column):
f2.write(str((current_line-1)*column+j))
if(j != current_job-(current_line-1)*column-1):
f2.write(',')
f2.write(''.join(['], ', tasks[2*i], ')']))
if current_job < count:
f2.write(',')
f2.write('\n')
f2.write(DATA_END)
f.close()
f2.close()
print ''.join(['Convert succeed. Output file: ', outputFile])
def traverse(dirName):
filenames = os.listdir(dirName)
print ''.join(['traverse ',dirName])
for filename in filenames:
if 0 == cmp(filename[-4:len(filename)].lower(),'.prb'):
convert(filename, ''.join([filename[0:-4],'.pl']))
def usage():
print 'convert.py usage:'
print 'Usage 1: python convert.py inputFileName outputFileName'
print 'Usage 2: python convert.py dirName'
print ''
if __name__=='__main__':
if len(sys.argv) != 2 and len(sys.argv) != 3:
usage()
sys.exit(0)
if len(sys.argv) == 2:
if not os.path.isdir(sys.argv[1]):
print 'Error:'
print ''.join([sys.argv[1],'is not a directory, exit.'])
print ''
sys.exit(1)
else:
traverse(sys.argv[1])
if len(sys.argv) == 3:
filename = sys.argv[1]
if 0 == cmp(filename[-4:len(filename)].lower(),'.prb'):
convert(sys.argv[1],sys.argv[2])
else:
print 'Warning:'
print 'the input file is not with the suffix .prb'
print sys.exit(0)
No comments:
Post a Comment