Exemple #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
 def parse_data(self):
     result = dict()
     titles = self.file[0]
     row_num = 1
     for row in self.file:
         counter = 0
         if row is not self.file[0]:
             result_row = dict()
             for cell in row:
                 result_row[titles[counter]] = cell
                 if isinstance(cell, datetime):
                     cell = Validator.xlsx_date(cell)
                 result_row[titles[counter]] = cell
                 counter = counter + 1
             result[row_num] = result_row
             row_num = row_num + 1
     self.file = result
Exemple #3
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
 def validate_data(self):
     validate = Validator()
     self.file = validate.save_dict(self.file)
Exemple #5
0
 def setUp(self):
     self.shell = Shell()
     validator.a = Validator()
Exemple #6
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)
Exemple #7
0
 def setUp(self):
     self.validator = Validator()
     validator.a = Validator()
     self.maxDiff = None
Exemple #8
0
 def test_date_object(self):
     expected = "05-05-1988"
     data = datetime.datetime(1988, 5, 5)
     result = Validator.xlsx_date(data)
     self.assertEqual(expected, result)
Exemple #9
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)