Last active
December 3, 2022 19:06
-
-
Save highsmallxu/43f7154d0633688f07b3150b8d6850ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
class Name: | |
def __get__(self, obj, objtype=None): | |
return self.value | |
def __set__(self, obj, value): | |
if len(value) > 20: | |
raise ValueError("Name cannot exceed 20 characters.") | |
self.value = value | |
class Email: | |
def __get__(self, obj, objtype=None): | |
return self.value | |
def __set__(self, obj, value): | |
regex = "^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$" | |
if not re.match(regex, value): | |
raise ValueError("It's not an email address.") | |
self.value = value | |
class Age: | |
def __get__(self, obj, objtype=None): | |
return self.value | |
def __set__(self, obj, value): | |
if value < 0: | |
raise ValueError("Age cannot be negative.") | |
self.value = value | |
class Citizen: | |
name = Name() | |
email = Email() | |
age = Age() | |
def __init__(self, id, name, email, age): | |
self.id = id | |
self.name = name | |
self.email = email | |
self.age = age | |
xiaoxu = Citizen("id1", "xiaoxu gao", "[email protected]", 27) | |
xiaoxu = Citizen("id1", "xiaoxu1234567890123456789", "[email protected]", 27) | |
# ValueError: Name cannot exceed 20 characters. | |
xiaoxu = Citizen("id1", "xiaoxu gao", "[email protected]", 27) | |
# ValueError: It's not an email address. | |
xiaoxu = Citizen("id1", "xiaoxu gao", "[email protected]", -27) | |
# ValueError: Age cannot be negative. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment