~Python3~基础学习已完成,~Asyncio~的~WebApp~已搭建通过;本仓库用于存放整理后的~python3~学习心得;
Orm作用只是对象-关系映射,把关系数据库的一张表映射为一个类,一行映射为一个对象 方便编写代码,不用直接操作SQL语句。 Orm框架必须要动态定义,因为我们实际需要的类,都需要根据表结构进行定义; 而python为我们提供了动态创建类的方法,即为 metaclass
即,建立orm到使用metaclass的正确思考逻辑为(个人理解): 为了简化代码编写,需要将数据库映射为具体的对象; 为了简化映射关系的建立过程,对映射过程进行了抽象,根据抽象的方法,定义orm框架 (我们难道不可以不建Model,直接对每张表做一遍映射?但是无疑这么做复杂更多) ORM框架需要能够动态定义类,刚好python可以用metaclass动态定义类
metaclass做了这些事: 建立一个基类Model,任何继承自此类的具体子类,都将自动得到字段与属性的映射关系, 并拥有增删改查等方法
metaclass用于增强类的功能,通过自定义创建类的方法~__new__~,改变类的行为 如此例中定义的ModelMetaclass,当基于Model生成子类时,子类将根据ModelMetaclass定 义的~__new__~,创建~__mapping__~、~__table__~等属性,动态创建各种使用的Model
email的正则表达式:
email_regex = re.compile(r'''(
^
([a-zA-Z0-9._%+_]+) # username
(@) # symbol
([a-zA-Z0-9-.]+) # domain name
(\.[a-zA-Z]{2,4}) # 不能带空格 切记
$
)
''', re.VERBOSE)
首先,后端拿到的初始密码即已经通过前端进行摘要,数据在由客户端传递至服务端时,用户密码已经经过了sha1摘要
服务端获取到用户注册时传递的passwd后,拼接用户 uid
:
passwd
,再次进行摘要,将摘要结果计入数据库中
用户登陆时,服务端接收到用户传递的passwd,查找用户,取出uid,再次拼接为 uid
:
passwd
,进行摘要后与数据库密钥相比对,相同即登陆完成