Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation functions modified to check for the presence of required columns in the input files without enforcing a specific order #49

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
54 changes: 53 additions & 1 deletion school_center.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,58 @@ def is_allocated(scode1: str, scode2:str) -> bool:
return allocations[scode1].get(scode2) != None
else:
return False

# validating the format of the school input file
def validate_school_file(file_path: str) -> bool:

if not os.path.exists(file_path):
print(f"School file '{file_path}' not found.")
return False

# open the file and read the header
with open(file_path, 'r', newline='', encoding='utf-8') as file:
header = file.readline().strip().split('\t')
required_columns = ['scode', 'count', 'name-address', 'lat', 'long']
if not all(col in header for col in required_columns):
print(f"Invalid school file format. Required columns: {required_columns}")
return False

return True

# validating the format of the center input file
def validate_center_file(file_path: str) -> bool:
# check if file exists
if not os.path.exists(file_path):
print(f"Center file '{file_path}' not found.")
return False
# open files and read header
with open(file_path, 'r', newline='', encoding='utf-8') as file:
header = file.readline().strip().split('\t')
required_columns = ['cscode', 'capacity', 'name', 'address', 'lat', 'long']
# compare the header with required columns
if not all(col in header for col in required_columns):
print(f"Invalid center file format. Required columns: {required_columns}")
return False

return True

# validating the format of the preference input file
def validate_preference_file(file_path: str) -> bool:
# check if the file exists
if not os.path.exists(file_path):
print(f"Preference file '{file_path}' not found.")
return False

# open the file and read the header
with open(file_path, 'r', newline='', encoding='utf-8') as file:
header = file.readline().strip().split('\t')
required_columns = ['scode', 'cscode', 'pref', 'reason']
# compare the header with required columns
if not all(col in header for col in required_columns):
print(f"Invalid preference file format. Required columns: {required_columns}")
return False

return True

parser = argparse.ArgumentParser(
prog='center randomizer',
Expand Down Expand Up @@ -214,4 +266,4 @@ def is_allocated(scode1: str, scode2:str) -> bool:
logger.info("Remaining capacity at each center (remaining_capacity cscode):")
logger.info(sorted([(v,k) for k, v in centers_remaining_cap.items() if v != 0]))
logger.info(f"Total remaining capacity across all centers: {sum({k:v for k, v in centers_remaining_cap.items() if v != 0}.values())}")
logger.info(f"Students not assigned: {remaining}")
logger.info(f"Students not assigned: {remaining}")