From f9fd1e06bf3c19133e0b489713215a7bc0476da2 Mon Sep 17 00:00:00 2001 From: "nirmala.sharma" Date: Tue, 23 Apr 2024 17:50:32 +0545 Subject: [PATCH 1/2] Output Directory Generation not working When DirectoryName and FileName is passed as Output Argument --- school_center.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/school_center.py b/school_center.py index c68cdf7..d4e4aa7 100644 --- a/school_center.py +++ b/school_center.py @@ -153,9 +153,28 @@ def is_allocated(scode1: str, scode2:str) -> bool: 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 +# Nirmala, Date: 23'April'24: If Directory and fileName is provided as argument then this logic will handle and in case if it is +# not provided then it works in the same way as it was working previously in which default files is created inside results directory(i.e. default directory) +# set at the top. +if args.output != parser.get_default('output'): + # Tuple Unpacking to separate Directory and Filename + output_dir, output_filename = os.path.split(args.output) + if output_dir is not None: + OUTPUT_DIR = output_dir + + # Combine the directory and filename to ensure the file is created within the directory + output_file_path = os.path.join(OUTPUT_DIR, output_filename) +else: + output_file_path = os.path.join(OUTPUT_DIR, args.output) +intermediate_file_path = os.path.join(OUTPUT_DIR, 'school-center-distance.tsv') + +# Create Directory if specified Directory has not been created +if OUTPUT_DIR and not os.path.exists(OUTPUT_DIR): + os.makedirs(OUTPUT_DIR) + + 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} @@ -164,9 +183,9 @@ def is_allocated(scode1: str, scode2:str) -> bool: remaining = 0 # stores count of non allocated students allocations = {} # to track mutual allocations -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: +#create_dir(OUTPUT_DIR) # Create the output directory if not exists +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", "school_name", "school_lat", "school_long", "cscode", "center_name", "center_address", "center_capacity", "distance_km"]) From f4b8054df9af1c9461e35b1e4c5ee45145b45d53 Mon Sep 17 00:00:00 2001 From: sumanashrestha Date: Wed, 24 Apr 2024 23:31:12 +0545 Subject: [PATCH 2/2] accept folder name only for --output --- school_center.py | 48 +++++++++++++++--------------------------------- 1 file changed, 15 insertions(+), 33 deletions(-) diff --git a/school_center.py b/school_center.py index 53bf3a4..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,8 +177,8 @@ 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') @@ -196,28 +186,21 @@ def is_allocated(scode1: str, scode2: str) -> bool: args = parser.parse_args() random = random.Random(args.seed) #overwrites the random module to use seeded rng -OUTPUT_DIR = 'results/' # Default Directory - -# Nirmala, Date: 23'April'24: -# If Directory and fileName are provided as arguments, then this logic will handle them. -# In case they are not provided, it works in the same way as it was working previously, -# where the default file is created inside the results directory (i.e., default directory) -if args.output != parser.get_default('output'): - # Tuple Unpacking to separate Directory and Filename - output_dir, output_filename = os.path.split(args.output) - if output_dir is not None: - OUTPUT_DIR = output_dir - - # Combine the directory and filename to ensure the file is created within the directory - output_file_path = os.path.join(OUTPUT_DIR, output_filename) -else: - output_file_path = os.path.join(OUTPUT_DIR, args.output) -intermediate_file_path = os.path.join(OUTPUT_DIR, 'school-center-distance.tsv') - +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) +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) @@ -227,7 +210,6 @@ def is_allocated(scode1: str, scode2: str) -> bool: remaining = 0 # stores count of non allocated students allocations = {} # to track mutual allocations -#create_dir(OUTPUT_DIR) # Create the output directory if not exists 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")