Skip to content

Instantly share code, notes, and snippets.

@nailsonlinux
Forked from highsmallxu/validate_descriptor.py
Created December 3, 2022 19:06
Show Gist options
  • Save nailsonlinux/6379c01453f99d8500883adbe137bcb9 to your computer and use it in GitHub Desktop.
Save nailsonlinux/6379c01453f99d8500883adbe137bcb9 to your computer and use it in GitHub Desktop.
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", "xiaoxugao@gmail.com", 27)
xiaoxu = Citizen("id1", "xiaoxu1234567890123456789", "highsmallxu@gmail.com", 27)
# ValueError: Name cannot exceed 20 characters.
xiaoxu = Citizen("id1", "xiaoxu gao", "highsmallxu@gmail.c", 27)
# ValueError: It's not an email address.
xiaoxu = Citizen("id1", "xiaoxu gao", "highsmallxu@gmail.com", -27)
# ValueError: Age cannot be negative.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment