示例#1
0
 def read(self, filename):
     """
     Return dictionary with key => value pairs
     :param filename is the file where the values exist
     >>> f = FileTypeXLSX()
     >>> f.read("Saves/doctest.xlsx")
     {1: {'ID': 'G262', 'Gender': 'Female', 'Age': 12, 'Sales': 215, 'BMI': 'Normal', 'Salary': 23, 'Birthday': '24-05-1993'}, 2: {'ID': 'A233', 'Gender': 'Chihuahua', 'Age': 22, 'Sales': 245, 'BMI': 'normal', 'Salary': 23, 'Birthday': '24-06-1995'}, 3: {'ID': 'A262', 'Gender': 'M', 'Age': 24, 'Sales': 845, 'BMI': 'Normal', 'Salary': 23, 'Birthday': '24-05-1993'}, 4: {'ID': 'A233', 'Gender': 'Female', 'Age': 62, 'Sales': 245, 'BMI': 'normal', 'Salary': 23, 'Birthday': '24-06-1995'}}
     Adding Row 1
     {'ID': 'G262', 'Gender': 'F', 'Age': '12', 'Sales': '215', 'BMI': 'Normal', 'Salary': '23', 'Birthday': '24-05-1993'}
     Error at entry: 2
     Adding Row 3
     {'ID': 'A262', 'Gender': 'M', 'Age': '24', 'Sales': '845', 'BMI': 'Normal', 'Salary': '23', 'Birthday': '24-05-1993'}
     Adding Row 4
     {'ID': 'A233', 'Gender': 'F', 'Age': '62', 'Sales': '245', 'BMI': 'Normal', 'Salary': '23', 'Birthday': '24-06-1995'}
     result
     {1: {'ID': 'G262', 'Gender': 'F', 'Age': '12', 'Sales': '215', 'BMI': 'Normal', 'Salary': '23', 'Birthday': '24-05-1993'}, 3: {'ID': 'A262', 'Gender': 'M', 'Age': '24', 'Sales': '845', 'BMI': 'Normal', 'Salary': '23', 'Birthday': '24-05-1993'}, 4: {'ID': 'A233', 'Gender': 'F', 'Age': '62', 'Sales': '245', 'BMI': 'Normal', 'Salary': '23', 'Birthday': '24-06-1995'}}
         """
     data = dict()
     empno = 0
     keys = []
     a_row = 0
     try:
         workbook = load_workbook(filename)
         first_sheet = workbook.sheetnames[0]
         worksheet = workbook[first_sheet]
         for row in worksheet.iter_rows():
             record = dict()
             row_num = 0
             for cell in row:
                 a_row = cell.row
                 if 1 == a_row:
                     keys.append(cell.value)
                 else:
                     valid = cell.value
                     if isinstance(cell.value, datetime):
                         valid = Validator.xlsx_date(cell.value)
                     record[keys[row_num]] = valid
                 row_num += 1
             if a_row > 1:
                 data[empno] = record
             empno += 1
         # print(data)
         result = Validator.save_dict(data)
         validator.a = None
     except PermissionError:
         result = dict()
         print("Sorry, you don't have enough permissions to access this file")
     print("result")
     return result
示例#2
0
 def read(self, filename):
     """
     Return dictionary with key => value pairs
     :param filename is the file where the values exist
     >>> read("Saves/data.csv")
     """
     # try:
     data = dict()
     empno = 0
     with open(filename) as f:
         reader = CSVDictReader(f)
         for row in reader:
             record = dict()
             for key in row:
                 record[key] = row.get(key)
             data[empno] = record
             empno += 1
         # print(data)
     # James' changes (13/03)
     result = Validator.save_dict(data)
     print(result)
     return result
示例#3
0
 def test_invalid_key_gen(self):
     """
     Tests validating data containing an invalid key for Gender
     """
     expected = {
         1: {
             'ID': 'A233',
             'Gender': 'M',
             'Age': '22',
             'Sales': '245',
             'BMI': 'Normal',
             'Salary': '23',
             'Birthday': '24-06-1995'
         },
         2: {
             'ID': 'A244',
             'Gender': 'M',
             'Age': '30',
             'Sales': '666',
             'BMI': 'Underweight',
             'Salary': '23',
             'Birthday': '05-05-1988'
         },
         3: {
             'ID': 'A253',
             'Gender': 'M',
             'Age': '35',
             'Sales': '456',
             'BMI': 'Obesity',
             'Salary': '23',
             'Birthday': '01-08-1983'
         },
         4: {
             'ID': 'A262',
             'Gender': 'M',
             'Age': '24',
             'Sales': '999',
             'BMI': 'Normal',
             'Salary': '23',
             'Birthday': '24-05-1993'
         }
     }
     data = {
         1: {
             'ID': 'A233',
             'Gender': 'M',
             'Age': '22',
             'Sales': '245',
             'BMI': 'Normal',
             'Salary': '23',
             'Birthday': '24-06-1995'
         },
         2: {
             'ID': 'A244',
             'Gender': 'M',
             'Age': '30',
             'Sales': '666',
             'BMI': 'Underweight',
             'Salary': '23',
             'Birthday': '05-05-1988'
         },
         3: {
             'ID': 'A253',
             'Gender': 'M',
             'Age': '35',
             'Sales': '456',
             'BMI': 'Obesity',
             'Salary': '23',
             'Birthday': '01-08-1983'
         },
         4: {
             'ID': 'A262',
             'Gender': 'M',
             'Age': '24',
             'Sales': '999',
             'BMI': 'Normal',
             'Salary': '23',
             'Birthday': '24-05-1993'
         },
         5: {
             'ID': 'A233',
             'Ge1nder': 'F',
             'Age': '62',
             'Sales': '245',
             'BMI': 'Normal',
             'Salary': '23',
             'Birthday': '24-06-1995'
         }
     }
     result = Validator.save_dict(data)
     self.assertDictEqual(expected, result)
 def validate_data(self):
     validate = Validator()
     self.file = validate.save_dict(self.file)
示例#5
0
 def test_gender_type_error(self):
     expected = {
         1: {
             'ID': 'A233',
             'Gender': 'M',
             'Age': '22',
             'Sales': '245',
             'BMI': 'Normal',
             'Salary': '23',
             'Birthday': '24-06-1995'
         },
         2: {
             'ID': 'A244',
             'Gender': 'M',
             'Age': '30',
             'Sales': '666',
             'BMI': 'Underweight',
             'Salary': '23',
             'Birthday': '05-05-1988'
         },
         3: {
             'ID': 'A253',
             'Gender': 'M',
             'Age': '35',
             'Sales': '456',
             'BMI': 'Obesity',
             'Salary': '23',
             'Birthday': '01-08-1983'
         },
         4: {
             'ID': 'A262',
             'Gender': 'M',
             'Age': '24',
             'Sales': '999',
             'BMI': 'Normal',
             'Salary': '23',
             'Birthday': '24-05-1993'
         }
     }
     data = {
         1: {
             'ID': 'A233',
             'Gender': 'M',
             'Age': '22',
             'Sales': '245',
             'BMI': 'Normal',
             'Salary': '23',
             'Birthday': '24-06-1995'
         },
         2: {
             'ID': 'A244',
             'Gender': 'M',
             'Age': '30',
             'Sales': '666',
             'BMI': 'Underweight',
             'Salary': '23',
             'Birthday': '05-05-1988'
         },
         3: {
             'ID': 'A253',
             'Gender': 'male',
             'Age': '35',
             'Sales': '456',
             'BMI': 'Obesity',
             'Salary': '23',
             'Birthday': '01-08-1983'
         },
         4: {
             'ID': 'A262',
             'Gender': 'M',
             'Age': '24',
             'Sales': '999',
             'BMI': 'Normal',
             'Salary': '23',
             'Birthday': '24-05-1993'
         },
         5: {
             'ID': 'A253',
             'Gender': dict(),
             'Age': '62',
             'Sales': '245',
             'BMI': 'normal',
             'Salary': '23',
             'Birthday': '24-06-1995'
         }
     }
     result = Validator.save_dict(data)
     self.assertDictEqual(expected, result)