Skip to content

Commit c1101dd

Browse files
authored
Changed output file location and added encoding='utf-8' on file write. (#8)
* REFACTOR: made output files to be created in separater folder * ADD: added gitignore to ignore the output directory * REFACTOR: added utf-8 mode to write files This was done because it was throwing error in windows
1 parent 294fb3c commit c1101dd

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,8 @@ venv_*/
9999
.vimrc
100100

101101
mypy.ini
102+
# Output Directories
103+
output
104+
result
105+
results
106+
outputs

school_center.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
OUTPUT_DIR = 'results/'
2+
13
PREF_DISTANCE_THRESHOLD = 2 # Preferred threshold distance in kilometers
24
ABS_DISTANCE_THRESHOLD = 7 # Absolute threshold distance in kilometers
35
MIN_STUDENT_IN_CENTER = 10 # minimum number of students from a school to be assigned to a center in normal circumstances
@@ -8,9 +10,18 @@
810
import csv
911
import random
1012
import argparse
13+
import os
1114
from typing import Dict, List
1215

1316

17+
def create_dir(dirPath:str):
18+
"""
19+
Create the given directory if it doesn't exists
20+
- Creates all the directories needed to resolve to the provided directory path
21+
"""
22+
if not os.path.exists(dirPath):
23+
os.makedirs(dirPath)
24+
1425
def haversine_distance(lat1, lon1, lat2, lon2):
1526
"""
1627
Calculate the great circle distance between two points
@@ -142,7 +153,9 @@ def is_allocated(scode1: str, scode2:str) -> bool:
142153
remaining = 0 # stores count of non allocated students
143154
allocations = {} # to track mutual allocations
144155

145-
with open('school-center-distance.tsv', 'w') as intermediate_file, open(args.output, 'w') as a_file:
156+
create_dir(OUTPUT_DIR) # Create the output directory if not exists
157+
with open('{}school-center-distance.tsv'.format(OUTPUT_DIR), 'w', encoding='utf-8') as intermediate_file, \
158+
open(OUTPUT_DIR + args.output, 'w', encoding='utf-8') as a_file:
146159
writer = csv.writer(intermediate_file, delimiter="\t")
147160
writer.writerow(["scode", "s_count", "school_name", "school_lat", "school_long", "cscode", "center_name", "center_address", "center_capacity", "distance_km"])
148161

0 commit comments

Comments
 (0)