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([x.__dict__ for x in self.set]), f)