1616class Student :
1717 """A class to represent the Student data."""
1818
19- def __init__ (self , csv_file : str ):
19+ def __init__ (self , csv_file : str = "data.csv" ):
2020 """
2121 Constructs all the necessary attributes for the Student object.
22- :param csv_file: path to the csv file
22+ :param csv_file: The name to the csv file. The default is
23+ data.csv
2324 :type csv_file: str
2425 """
2526 self .csv_file : str = csv_file
@@ -35,20 +36,24 @@ def load_data(self) -> list[dict[str, Any]]:
3536 students : list [dict [str , Any ]] = []
3637 file_path : str = os .path .join (
3738 os .path .dirname (os .path .dirname (os .path .abspath (__file__ ))),
38- self .csv_file .replace ("/" , "\\ " ))
39- with open (file_path , "r" , encoding = ENCODING ) as file :
40- reader = csv .DictReader (file )
41- student : dict [str , Any ]
42- for student in reader :
43- try :
44- age : int = int (student ["edad" ])
45- if age > 0 :
46- student ["edad" ] = age
47- students .append (student )
48- else :
49- logger .error (VALID_AGE , age )
50- raise ValidationError (VALID_AGE )
51- except ValueError as exc :
52- logger .error (NOT_NUMBER )
53- raise ValidationError (NOT_NUMBER ) from exc
39+ "data" , "raw" , self .csv_file )
40+ try :
41+ with open (file_path , "r" , encoding = ENCODING ) as file :
42+ reader = csv .DictReader (file )
43+ student : dict [str , Any ]
44+ for student in reader :
45+ try :
46+ age : int = int (student ["edad" ])
47+ if age > 0 :
48+ student ["edad" ] = age
49+ students .append (student )
50+ else :
51+ logger .error (VALID_AGE , age )
52+ raise ValidationError (VALID_AGE )
53+ except ValueError as exc :
54+ logger .error (NOT_NUMBER )
55+ raise ValidationError (NOT_NUMBER ) from exc
56+ except FileNotFoundError :
57+ logger .error ("Data file not found at %s. Please check the"
58+ " location of your data file." , file_path )
5459 return students
0 commit comments