Skip to content

Commit d2ec4d7

Browse files
authored
Fixed: output directory generation not working as expected when --output is passed a directory name instead of filename (#45)
* FIX: file not exist error when --output is passed a directory * FIX: merge conflicts * Fix: using output_filename variable as string * FIX: import error * FIX: error while --output provided directory only * FIX: typo and removed unwanted print statements
1 parent 49e1aec commit d2ec4d7

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

school_center.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from utils.custom_logger import configure_logging
22
from typing import Dict, List
3-
import sys
4-
import os
3+
from os import sys, path, makedirs
54
import argparse
65
import logging
76
import random
@@ -14,19 +13,13 @@
1413
MIN_STUDENT_IN_CENTER = 10 # Min. no of students from a school to be assigned to a center in normal circumstances
1514
STRETCH_CAPACITY_FACTOR = 0.02 # How much can center capacity be streched if need arises
1615
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'
1718

1819
configure_logging()
1920
logger = 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

3124
def haversine_distance(lat1, lon1, lat2, lon2):
3225
"""
@@ -214,7 +207,7 @@ def is_allocated(scode1: str, scode2: str) -> bool:
214207
parser.add_argument('prefs_tsv', default='prefs.tsv',
215208
help="Tab separated (TSV) file containing preference scores")
216209
parser.add_argument(
217-
'-o', '--output', default='school-center.tsv', help='Output file')
210+
'-o', '--output', default = DEFAULT_OUTPUT_FILENAME, help='Output file')
218211
parser.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:
231224
remaining = 0 # stores count of non allocated students
232225
allocations = {} # 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

Comments
 (0)