diff --git a/school_center.py b/school_center.py index 271e2f0..a494636 100644 --- a/school_center.py +++ b/school_center.py @@ -17,16 +17,6 @@ configure_logging() logger = logging.getLogger(__name__) - -def create_dir(dirPath: str): - """ - Create the given directory if it doesn't exists - - Creates all the directories needed to resolve to the provided directory path - """ - if not os.path.exists(dirPath): - os.makedirs(dirPath) - - def haversine_distance(lat1, lon1, lat2, lon2): """ Calculate the great circle distance between two points @@ -187,16 +177,31 @@ def is_allocated(scode1: str, scode2: str) -> bool: help="Tab separated (TSV) file containing center details") parser.add_argument('prefs_tsv', default='prefs.tsv', help="Tab separated (TSV) file containing preference scores") -parser.add_argument( - '-o', '--output', default='school-center.tsv', help='Output file') +parser.add_argument('-o', '--output', default='school-center.tsv', + help='Output file') parser.add_argument('-s', '--seed', action='store', metavar='SEEDVALUE', default=None, type=float, help='Initialization seed for Random Number Generator') args = parser.parse_args() - random = random.Random(args.seed) #overwrites the random module to use seeded rng +output_dir, output_filename = os.path.split(args.output) + +if len(output_dir) == 0: + output_dir = 'results/' # Default Directory + +if len(output_filename) == 0: + output_filename = parser.get_default('output') + +# Create Directory if specified Directory has not been created +if output_dir and not os.path.exists(output_dir): + os.makedirs(output_dir) + +output_file_path = os.path.join(output_dir, output_filename) + +intermediate_file_path = os.path.join(output_dir, 'school-center-distance.tsv') + schools = sorted(read_tsv(args.schools_tsv), key= school_sort_key) centers = read_tsv(args.centers_tsv) centers_remaining_cap = {c['cscode']: int(c['capacity']) for c in centers} @@ -205,10 +210,8 @@ def is_allocated(scode1: str, scode2: str) -> bool: remaining = 0 # stores count of non allocated students allocations = {} # to track mutual allocations -OUTPUT_DIR = 'results/' -create_dir(OUTPUT_DIR) # Create the output directory if not exists -with open('{}school-center-distance.tsv'.format(OUTPUT_DIR), 'w', encoding='utf-8') as intermediate_file, \ - open(OUTPUT_DIR + args.output, 'w', encoding='utf-8') as a_file: +with open(intermediate_file_path, 'w', encoding='utf-8') as intermediate_file, \ +open(output_file_path, 'w', encoding='utf-8') as a_file: writer = csv.writer(intermediate_file, delimiter="\t") writer.writerow(["scode", "s_count",