Git Product home page Git Product logo

zero_blog's Introduction

个人博客

概览

~Python3~基础学习已完成,~Asyncio~的~WebApp~已搭建通过;本仓库用于存放整理后的~python3~学习心得;

要点记录

ORM及metaclass用法

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 ,进行摘要后与数据库密钥相比对,相同即登陆完成

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.