1
1
from utils .custom_logger import configure_logging
2
2
from typing import Dict , List
3
- import sys
4
- import os
3
+ from os import sys , path , makedirs
5
4
import argparse
6
5
import logging
7
6
import random
14
13
MIN_STUDENT_IN_CENTER = 10 # Min. no of students from a school to be assigned to a center in normal circumstances
15
14
STRETCH_CAPACITY_FACTOR = 0.02 # How much can center capacity be streched if need arises
16
15
PREF_CUTOFF = - 4 # Do not allocate students with pref score less than cutoff
16
+ DEFAULT_OUTPUT_DIR = 'results' # Default directory to create output files if --output not provided
17
+ DEFAULT_OUTPUT_FILENAME = 'school-center.tsv'
17
18
18
19
configure_logging ()
19
20
logger = logging .getLogger (__name__ )
20
21
21
22
22
- def create_dir (dirPath : str ):
23
- """
24
- Create the given directory if it doesn't exists
25
- - Creates all the directories needed to resolve to the provided directory path
26
- """
27
- if not os .path .exists (dirPath ):
28
- os .makedirs (dirPath )
29
-
30
23
31
24
def haversine_distance (lat1 , lon1 , lat2 , lon2 ):
32
25
"""
@@ -214,7 +207,7 @@ def is_allocated(scode1: str, scode2: str) -> bool:
214
207
parser .add_argument ('prefs_tsv' , default = 'prefs.tsv' ,
215
208
help = "Tab separated (TSV) file containing preference scores" )
216
209
parser .add_argument (
217
- '-o' , '--output' , default = 'school-center.tsv' , help = 'Output file' )
210
+ '-o' , '--output' , default = DEFAULT_OUTPUT_FILENAME , help = 'Output file' )
218
211
parser .add_argument ('-s' , '--seed' , action = 'store' , metavar = 'SEEDVALUE' ,
219
212
default = None , type = float ,
220
213
help = 'Initialization seed for Random Number Generator' )
@@ -231,10 +224,28 @@ def is_allocated(scode1: str, scode2: str) -> bool:
231
224
remaining = 0 # stores count of non allocated students
232
225
allocations = {} # to track mutual allocations
233
226
234
- OUTPUT_DIR = 'results/'
235
- create_dir (OUTPUT_DIR ) # Create the output directory if not exists
236
- with open ('{}school-center-distance.tsv' .format (OUTPUT_DIR ), 'w' , encoding = 'utf-8' ) as intermediate_file , \
237
- open (OUTPUT_DIR + args .output , 'w' , encoding = 'utf-8' ) as a_file :
227
+
228
+ def get_output_dir ():
229
+ dirname = path .dirname (args .output )
230
+ if (dirname ):
231
+ return dirname
232
+ else :
233
+ return DEFAULT_OUTPUT_DIR
234
+
235
+ def get_output_filename ():
236
+ basename = path .basename (args .output )
237
+ if (basename ):
238
+ return basename
239
+ else :
240
+ return DEFAULT_OUTOUT_FILENAME
241
+
242
+
243
+ output_dirname = get_output_dir ()
244
+ output_filename = get_output_filename ()
245
+ makedirs (output_dirname , exist_ok = True ) # Create the output directory if not exists
246
+
247
+ with open (path .join (output_dirname , "school-center-distance.tsv" ), 'w' , encoding = 'utf-8' ) as intermediate_file , \
248
+ open (path .join (output_dirname , output_filename ), 'w' , encoding = 'utf-8' ) as a_file :
238
249
writer = csv .writer (intermediate_file , delimiter = "\t " )
239
250
writer .writerow (["scode" ,
240
251
"s_count" ,
0 commit comments