-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathexercise-1.py
32 lines (24 loc) · 1.08 KB
/
exercise-1.py
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
# encoding: utf-8
"""
Single Responsibility Principle to jedna z zasad SOLID. Mówi ona, że każda klasa
powinna mieć tylko jeden obszar odpowiedzialności i tylko jeden powód do zmiany.
Poniżej przedstawiono klasę Person, która reprezentuje pojedynczą osobę - jej
imię, nazwisko oraz adres mailowy. Jest to przykład złamania tej zasady,
ponieważ w środku niej znajduje się walidacja adresu email.
Jako ćwiczenie, spróbuj zrefaktoryzować ten kod tak, aby sama walidacja znalazła
się w osobnej klasie reprezentującej adres email.
"""
import re
import unittest
EMAIL_PATTERN = re.compile(r'^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$')
class Person:
def __init__(self, first_name, last_name, email):
assert isinstance(first_name, str)
assert isinstance(last_name, str)
assert isinstance(email, str)
self.first_name = first_name
self.last_name = last_name
if EMAIL_PATTERN.match(email) is None:
raise ValueError('Invalid email')
else:
self.email = email