1import re
2
3class PhoneNumber:
4 def __init__(self, number):
5 self.number = re.sub(r"^[\+1]|[\.\s\-\(\)]", "", number)
6 self.number = self.validate(self.number)
7 self.area_code = self.number[:3]
8 self.exchange_code = self.number[3:6]
9 self.suscriber_num= self.number[6:]
10
11 def validate(self, number):
12 length = len(number)
13
14 if length < 10 or length > 11:
15 raise ValueError("Wrong Number Length")
16
17 if length == 11:
18 if number[0] != '1':
19 raise ValueError("Incorrect country code")
20 else:
21 number = number[1:]
22
23 if re.findall("[A-z]", number):
24 raise ValueError("Incorrect Number: alpha inside")
25
26 if int(number[0]) <= 1 or int(number[3]) <= 1:
27 raise ValueError("Incorrect Number")
28
29 return number
30
31 def pretty(self):
32 return f"({self.area_code})-{self.exchange_code}-{self.suscriber_num}"
33
34
35# print(PhoneNumber("(223) 456-7890").number)
36# print(PhoneNumber("(223) 456-7890").area_code)
37# print(PhoneNumber("(223) 45a-7890").number)