11from utils .custom_logger import configure_logging
22from typing import Dict , List
3- import sys
4- import os
3+ from os import sys , path , makedirs
54import argparse
65import logging
76import random
1413MIN_STUDENT_IN_CENTER = 10 # Min. no of students from a school to be assigned to a center in normal circumstances
1514STRETCH_CAPACITY_FACTOR = 0.02 # How much can center capacity be streched if need arises
1615PREF_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'
1718
1819configure_logging ()
1920logger = logging .getLogger (__name__ )
2021
2122
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-
3023
3124def haversine_distance (lat1 , lon1 , lat2 , lon2 ):
3225 """
@@ -214,7 +207,7 @@ def is_allocated(scode1: str, scode2: str) -> bool:
214207parser .add_argument ('prefs_tsv' , default = 'prefs.tsv' ,
215208 help = "Tab separated (TSV) file containing preference scores" )
216209parser .add_argument (
217- '-o' , '--output' , default = 'school-center.tsv' , help = 'Output file' )
210+ '-o' , '--output' , default = DEFAULT_OUTPUT_FILENAME , help = 'Output file' )
218211parser .add_argument ('-s' , '--seed' , action = 'store' , metavar = 'SEEDVALUE' ,
219212 default = None , type = float ,
220213 help = 'Initialization seed for Random Number Generator' )
@@ -231,10 +224,28 @@ def is_allocated(scode1: str, scode2: str) -> bool:
231224remaining = 0 # stores count of non allocated students
232225allocations = {} # to track mutual allocations
233226
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 :
238249 writer = csv .writer (intermediate_file , delimiter = "\t " )
239250 writer .writerow (["scode" ,
240251 "s_count" ,
0 commit comments