def pesel(input_file_path): root, ext = os.path.splitext(input_file_path) output_file_path = root + "-birthdays" + ext invalid_list = [] with open(input_file_path, 'r') as input_file, open(output_file_path, 'w') as output_file: for line_number, pesel_line in enumerate(input_file, 1): pesel_line = pesel_line.rstrip() try: pesel = PESEL().from_string(pesel_line) except ValueError: invalid_list.append([line_number, pesel_line]) continue processed_line = "{pesel} : {birthday}".format( **{ "pesel": pesel_line, "birthday": pesel.to_date("%d-%m-%Y") }) print(processed_line, file=output_file) retval = { "processed": line_number, "valid": line_number - len(invalid_list), "invalid": len(invalid_list), "invalid_list": invalid_list, } return retval
def test_20th_century_one_digit_month(self): p = PESEL().from_string("20022045678") self.assertEqual(p.to_date("%Y-%m-%d"), "1920-02-20" )
def test_lower_boundary_valid(self): self.assertTrue(PESEL().from_date(year=1800, month=1, day=1))
def test_leap_year_invalid(self): with self.assertRaises(ValueError): PESEL().from_date(year=2013, month=2, day=29)
def test_invalid_month_below(self): with self.assertRaises(ValueError): PESEL().from_date(year=2016, month=0, day=30)
def test_invalid_year_below(self): with self.assertRaises(ValueError): PESEL().from_date(year=1799, month=12, day=31)
def test_23rd_century(self): self.assertTrue(PESEL().from_string("16711012342"))
def test_21st_century(self): self.assertTrue(PESEL().from_string("16311012340"))
def test_23rd_century_one_digit_month(self): p = PESEL().from_string("00653011554") self.assertEqual(p.to_date("%Y-%m-%d"), "2200-05-30" )
def test_22nd_century_two_digit_month(self): p = PESEL().from_string("61513035794") self.assertEqual(p.to_date("%Y-%m-%d"), "2161-11-30" )
def test_space(self): with self.assertRaises(ValueError): PESEL().from_string("78a c859694")
def test_22nd_century_one_digit_month(self): p = PESEL().from_string("11443095179") self.assertEqual(p.to_date("%Y-%m-%d"), "2111-04-30" )
def test_21st_century_two_digit_month(self): p = PESEL().from_string("20323079310") self.assertEqual(p.to_date("%Y-%m-%d"), "2020-12-30" )
def test_21st_century_one_digit_month(self): p = PESEL().from_string("06233084266") self.assertEqual(p.to_date("%Y-%m-%d"), "2006-03-30" )
def test_20th_century_two_digit_month(self): p = PESEL().from_string("99123085265") self.assertEqual(p.to_date("%Y-%m-%d"), "1999-12-30" )
def test_19th_century(self): self.assertTrue(PESEL().from_string("16911012348"))
def test_20th_century(self): self.assertTrue(PESEL().from_string("16111012344"))
def test_23rd_century_two_digit_month(self): p = PESEL().from_string("99722984464") self.assertEqual(p.to_date("%Y-%m-%d"), "2299-12-29" )
def test_22nd_century(self): self.assertTrue(PESEL().from_string("16511012346"))
def test_special_char(self): with self.assertRaises(ValueError): PESEL().from_string("7-_=+*/<>82")
def test_short_string(self): with self.assertRaises(ValueError): PESEL().from_string("780524")
def test_zero_month(self): with self.assertRaises(ValueError): PESEL().from_string("49000501580")
def test_invalid_year_above(self): with self.assertRaises(ValueError): PESEL().from_date(year=2300, month=1, day=1)
def test_zero_day(self): with self.assertRaises(ValueError): PESEL().from_string("49040001580")
def test_invalid_day_above(self): with self.assertRaises(ValueError): PESEL().from_date(year=2016, month=11, day=32)
def test_invalid_month(self): with self.assertRaises(ValueError): PESEL().from_string("49140501580")
def test_leap_year_valid(self): self.assertTrue(PESEL().from_date(year=2012, month=2, day=29))
def test_invalid_day(self): with self.assertRaises(ValueError): PESEL().from_string("49043501580")
def test_upper_boundary_valid(self): self.assertTrue(PESEL().from_date(year=2299, month=12, day=31))
def test_invalid_control_number(self): with self.assertRaises(ValueError): PESEL().from_string("49040501587")