-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeselParser.py
More file actions
105 lines (79 loc) · 2.34 KB
/
PeselParser.py
File metadata and controls
105 lines (79 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# counters
total = correct = male = female = 0
invalidLength = invalidDigit = invalidDate = invalidChecksum = 0
PESEL_LENGTH = 11
PESEL_WEIGHTS = (1, 3, 7, 9, 1, 3, 7, 9, 1, 3)
def isLeapYear(yyyy):
if yyyy % 4 == 0 and not yyyy % 100 == 0 or yyyy % 400 == 0:
return True
else:
return False
def checkSumCorrect(pesel):
sourceCheckSum = int(pesel[10])
sum = 0
for i in range(len(PESEL_WEIGHTS)):
sum += (PESEL_WEIGHTS[i] * int(pesel[i]))
computedCheckSum = (10 - (sum % 10)) % 10
return sourceCheckSum == computedCheckSum
def dateIsValid(pesel):
yyyy = int(pesel[0] + pesel[1])
mm = int(pesel[2] + pesel[3]) % 20
dd = int(pesel[4] + pesel[5])
centuryCheck = int(pesel[2] + pesel[3])
daysInMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if mm <= 0 or mm > 12:
return False
if dd <= 0 or dd > 31:
return False
# To find exact year
if mm == centuryCheck:
yyyy += 1900
elif mm == centuryCheck - 80:
yyyy += 1800
elif mm == centuryCheck - 20:
yyyy += 2000
elif mm == centuryCheck - 40:
yyyy += 2100
elif mm == centuryCheck - 60:
yyyy += 2200
if yyyy not in range(1800, 2300):
return False
# Check if days correspond to months
if dd > daysInMonths[mm - 1]:
if mm == 2 and dd <= 29 and isLeapYear(yyyy):
return True
else:
return False
return True
def isFemale(pesel):
return int(pesel[9]) % 2 == 0
file = open("1e3.dat", 'r')
for PESEL in file:
PESEL = PESEL.strip()
total += 1
# verify length
if len(PESEL) != 11:
invalidLength += 1
continue
if PESEL.isdigit() is not True:
invalidDigit += 1
continue
if dateIsValid(PESEL) is not True:
invalidDate += 1
continue
if checkSumCorrect(PESEL) is not True:
invalidChecksum += 1
continue
if isFemale(PESEL):
female += 1
correct += 1
else:
male += 1
correct += 1
file.close()
print("Correct: Female, Male ")
print(correct, female, male, "\n")
print("Invalid: Lengths, Digits, Dates, Checksum ")
print(invalidLength, invalidDigit, invalidDate, invalidChecksum, "\n")
print("Total : ")
print(invalidLength + invalidDigit + invalidDate + invalidChecksum + correct, "\n")