diff --git a/school_center.py b/school_center.py index a03e859..9ee7297 100644 --- a/school_center.py +++ b/school_center.py @@ -198,6 +198,22 @@ def is_allocated(scode1: str, scode2: str) -> bool: """ return allocations.get(scode1, {}).get(scode2) is not None +def validate_data(schools, centers, prefs): + # Ensure all necessary fields are present and valid + for school in schools: + if not all(k in school for k in ('scode', 'name-address', 'lat', 'long', 'count')): + logger.error(f"Invalid school data: {school}") + sys.exit(1) + for center in centers: + if not all(k in center for k in ('cscode', 'name', 'address', 'lat', 'long', 'capacity')): + logger.error(f"Invalid center data: {center}") + sys.exit(1) + # Validate preference scores similarly + for scode, center_prefs in prefs.items(): + for cscode, pref in center_prefs.items(): + if not isinstance(pref, int): + logger.error(f"Invalid preference data: scode={scode}, cscode={cscode}, pref={pref}") + sys.exit(1) parser = argparse.ArgumentParser( prog='center randomizer', @@ -223,10 +239,11 @@ def is_allocated(scode1: str, scode2: str) -> bool: centers_remaining_cap = {c['cscode']: int(c['capacity']) for c in centers} prefs = read_prefs(args.prefs_tsv) +validate_data(schools, centers, prefs) #Validate Data Here + remaining = 0 # stores count of non allocated students allocations = {} # to track mutual allocations - def get_output_dir(): dirname = path.dirname(args.output) if(dirname): @@ -336,4 +353,4 @@ def get_output_filename(): 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}") \ No newline at end of file