Python 描述器学习

想写一个关于密码检查的方法:

class Typed:
    # 大写字母
    uppercase = re.compile(r'[A-Z]+')
    # 小写字母
    lowercase = re.compile(r'[a-z]+')
    # 阿拉伯数字
    numbers   = re.compile(r'[0-9]+')
    # 特殊字符
    special_c = re.compile(r'[^\w]+')

    def __init__(self, passwd):
        self.password = passwd

    def __get__(self, instance, cls):
        if instance is None:
            return self
        return instance.__dict__[self.password]
    def __set__(self, instance, passwd):
        if len(passwd) < 8 or len(passwd) > 24:
            raise TypeError("Your password length must be less equal 24 and greater eqaul 8")
        else:
            if self.uppercase.findall(passwd) and self.lowercase.findall(passwd) and self.numbers.findall(passwd) and self.special_c.findall(passwd):
                instance.__dict__[self.password] = passwd
            else:
                 raise TypeError("Your password must contain upper/lower case, numbers and special characters")
    def __delete__(self, instance):
        del instance.__dict__[self.password]

class new_pass:
    password = Typed('password')
    def __init__(self, password):
        self.password = password

测试:

  1. 只包含数字,且长度小于8
>>> mypass = new_pass('12345')
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Users\Zhongjun CHEN\Desktop\descriptor.py", line 30, in __init__
    self.password = password
  File "C:\Users\Zhongjun CHEN\Desktop\descriptor.py", line 18, in __set__
    raise TypeError("Your password length must be less equal 24 and greater eqaul 8")
TypeError: Your password length must be less equal 24 and greater eqaul 8
  1. 包含数字和小写字母,长度大于8
>>> mypass = new_pass('12345667adfd')
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Users\Zhongjun CHEN\Desktop\descriptor.py", line 30, in __init__
    self.password = password
  File "C:\Users\Zhongjun CHEN\Desktop\descriptor.py", line 23, in __set__
    raise TypeError("Your password must contain upper/lower case, numbers and special characters")
TypeError: Your password must contain upper/lower case, numbers and special characters
  1. 包含大写小写字母及数字
>>> mypass = new_pass('123AbcdAA')
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Users\Zhongjun CHEN\Desktop\descriptor.py", line 30, in __init__
    self.password = password
  File "C:\Users\Zhongjun CHEN\Desktop\descriptor.py", line 23, in __set__
    raise TypeError("Your password must contain upper/lower case, numbers and special characters")
TypeError: Your password must contain upper/lower case, numbers and special characters
  1. 包含数字大写字母,特殊字符
>>> mypass = new_pass('123QWE,.')
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Users\Zhongjun CHEN\Desktop\descriptor.py", line 30, in __init__
    self.password = password
  File "C:\Users\Zhongjun CHEN\Desktop\descriptor.py", line 23, in __set__
    raise TypeError("Your password must contain upper/lower case, numbers and special characters")
TypeError: Your password must contain upper/lower case, numbers and special characters
  1. 合法的密码
>>> mypass = new_pass('123QWEasd!')
>>> mypass.password
'123QWEasd!'
>>>

你可能感兴趣的:(Python 描述器学习)