This commit is contained in:
emile 2019-11-04 22:47:15 +01:00
commit c8f16d8f9c
4 changed files with 122 additions and 0 deletions

33
main.py Normal file
View File

@ -0,0 +1,33 @@
print("""
____ _____ ____ _____ _ ___ _____ _ _____ ___ ____
| _ \| ____/ ___|| ____| | |_ _|__ / / \|_ _/ _ \| _ \
| |_) | _| \___ \| _| | | | | / / / _ \ | || | | | |_) |
| __/| |___ ___) | |___| |___ | | / /_ / ___ \| || |_| | _ <
|_| |_____|____/|_____|_____|___/____/_/ \_|_| \___/|_| \_\ 2000!
dodaj - dodaj osobę
list - listuj osoby
list pesele
pesel - sprawdź pesel osoby
""")
from pesel import PeselStorage
storage = PeselStorage()
while True:
def enter():
name = input("$ Wpisz imię: ")
pesel = input("$ Wpisz pesel: ")
try:
storage.append(name,pesel)
except AttributeError:
print(" ----- Pesel niepoprawny! Spróbuj ponownie ;( ------")
def _list():
print("\n".join(storage.get_all_persons()))
def remove():
storage.remove_all_names()
_list()
def get_pesel():
print(storage.get_pesel_of(input("$ Kogo?: ")))
def list_pesele():
print("\n".join([": ".join([x.name, str(x.pesel)]) for x in storage.set]))
{"dodaj":enter, "list":_list, "pesel":get_pesel, "list pesele":list_pesele}[input("$ ")]()

58
pesel.py Normal file
View File

@ -0,0 +1,58 @@
from typing import Union, List
from dataclasses import dataclass
from functools import *
import json
class PeselToolkit:
@staticmethod
def check_pesel_validity(pesel:Union[int,str])->bool:
pesel =str(pesel)
if len(pesel)<11: return False
def get_checksum(pesel:str)->bool:
weights = [9,7,3,1,9,7,3,1,9,7,0]
checksum = sum([weights[i] * int(n) for i,n in enumerate(pesel)])%10
return checksum
if get_checksum(pesel) != int(pesel[-1]): return False
def check_dates(pesel:str)->bool:
year = int(pesel[:2])
month = int(pesel[2:4])
day = int(pesel[4:6])
if not (month>0 and (month<=12 or (month>20 and month<=32) or (month>40 and month<=52) or (month>60 and month<=72) or (month>80 and month<=92))):
return False
if day>31 or day<1: return False
return True
if not check_dates(pesel): return False
return True
@dataclass()
class Person:
name:str
pesel:str
def __hash__(self):
if (self.pesel[0]=='0'):return int("999999"+self.pesel)
return int(self.pesel)
class PeselStorage:
def __init__(self):
self.set = set()
def append(self,name:str, pesel:Union[int,str]):
if not PeselToolkit.check_pesel_validity(str(pesel)):
raise AttributeError("pesel is invalid")
self.set.add(Person(name,str(pesel)))
def get_pesel_of(self, name:str)->int:
#lepiej pytonicznie [x for x in self.set if x.name==name][0]
return list(filter(lambda d: d.name == name, self.set))[0].pesel
def get_all_persons(self):
return list(map(lambda d: d.name, self.set))
def remove_all_names(self):
#self.set = set([Person("", x.pesel) for x in self.set])
self.set = map(lambda d: Person("",d.pesel),self.set)
def dump(self, filename:str):
with open(filename, "w+") as f:
json.dump(list(self.set), f)

10
readme.md Normal file
View File

@ -0,0 +1,10 @@
# Włączanie testów:
`python3 tests.py`
# Włączanie programu
`python3 main.py`
Jest lista, jest set, są strumienie (filter, sum, map i pythonowe comprahensions). Jest zwracanie struktury. Są testy.

21
tests.py Normal file
View File

@ -0,0 +1,21 @@
import unittest
from pesel import PeselStorage, PeselToolkit
class TestPeselToolkit(unittest.TestCase):
def test_toolkit(self):
pesele = ["81100216357","80072909146","02070803628","90060804786" ]
for x in pesele:
self.assertTrue(PeselToolkit.check_pesel_validity(x))
invalid = ["02070803627","81100216350","80072909143","02070803624","90060804781","81950216357","80075609146","0207080362444","90060804782"]
for x in invalid:
self.assertFalse(PeselToolkit.check_pesel_validity(x))
class TestPeselStorage(unittest.TestCase):
def test_storage(self):
s = PeselStorage()
s.append("janek", "80072909146")
s.append("janek2", "02070803628")
self.assertListEqual(s.get_all_persons(), ["janek", "janek2"])
self.assertRaises(AttributeError, s.append, "zly", "02070803627")
self.assertEqual(s.get_pesel_of("janek"),"80072909146" )
if __name__=="__main__":
unittest.main()