美文网首页
Python 描述器学习

Python 描述器学习

作者: 陈忠俊 | 来源:发表于2020-08-13 18:03 被阅读0次

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

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 "<stdin>", line 1, in <module>
  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 "<stdin>", line 1, in <module>
  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 "<stdin>", line 1, in <module>
  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 "<stdin>", line 1, in <module>
  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 描述器学习

    想写一个关于密码检查的方法: 测试: 只包含数字,且长度小于8 包含数字和小写字母,长度大于8 包含大写小写字母及...

  • Python 描述器

    描述器 定义了__get__和__set__方法的描述器称为数据描述器只定义了__get__的描述器称为非数据描述...

  • python 描述器

    描述器定义 ① 实现描述符协议 实现 __get__(), __set__(), __delete__() 方法 ...

  • Python描述器

    引入描述器 以stackoverflow上关于描述器(descriptor )的疑问开篇。 以上代码实现了温度的摄...

  • python--装饰器--测试程序运行次数以及程序运行时间

    python的装饰器应用极其广泛,在python--学习的章节有所描述,在这里写两个常用的装饰器。 测试程序运行次...

  • Python 描述器解析

    语法简析 一般来说,描述器(descriptor)是一个有”绑定行为”的对象属性(object attribute...

  • python描述器(Descriptor)

    描述器协议 描述器协议包括以下3个方法: object.__get__(self, instance, owner...

  • 9.描述器

    目录:1.描述器的表现2.描述器定义3.属性的访问顺序4.Python中的描述器5.新增方法 1. 描述器的表现 ...

  • Python 黑魔法 --- 描述器(descriptor)

    Python 黑魔法---描述器(descriptor) Python黑魔法,前面已经介绍了两个魔法,装饰器和迭代...

  • Python面向对象 - 描述器

    描述器 什么是描述器?描述器是干什么用的? 现在有一个Person类来表示人。其中有两个属性,体重weight和身...

网友评论

      本文标题:Python 描述器学习

      本文链接:https://www.haomeiwen.com/subject/codidktx.html