元类和orm
1. 什么是元类
在Python中一切皆对象,类也是一个对象,实例对象由类创建出来的,类是由元类创建出来的。简而言之,用来创建类的类就叫元类(metaclass)。 函数type其实就是一个元类,type就是Python在背后用来创建所有类的元类。
2. globals()的作用
globals()函数返回的是一个字典,里面保存的是所有当前位置的全部全局变量。
3.内建模块
Python2中为__builtin__
Python3中为builtins
4. type()动态创建类
type(‘类名’, (父类,) ,{属性})
type可以接受一个类的描述作为参数,然后返回一个类。(要知道,根据传入参数的不同,同一个函数拥有两种完全不同的用法是一件很傻的事情,但这在Python中是为了保持向后兼容性)
type可以像这样工作:
type(类名, 由父类名称组成的元组(针对继承的情况,可以为空),包含属性的字典(名称和值))
5. 什么是ORM?
ORM 即Object Relational Mapping,全称对象关系映射。为了简化SQL操作,把数据库的操作封装到类里面,把表名映射成类,把字段映射为属性,把行(数据)映射为实例。通过这样一一对应的映射关系,我们便可以避免直接操作数据库,而只要调用相应的方法即可。
通过元类简单实现ORM中的insert功能
class ModelMetaclass(type): def __new__(cls, name, bases, attrs): mappings = dict() # 判断是否需要保存 for k, v in attrs.items(): # 判断是否是指定的StringField或者IntegerField的实例对象 if isinstance(v, tuple): print('Found mapping: %s ==> %s' % (k, v)) mappings[k] = v # 删除这些已经在字典中存储的属性 for k in mappings.keys(): attrs.pop(k) # 将之前的uid/name/email/password以及对应的对象引用、类名字 attrs['__mappings__'] = mappings # 保存属性和列的映射关系 attrs['__table__'] = name # 假设表名和类名一致 return type.__new__(cls, name, bases, attrs) class User(metaclass=ModelMetaclass): uid = ('uid', "int unsigned") name = ('username', "varchar(30)") email = ('email', "varchar(30)") password = ('password', "varchar(30)") # 当指定元类之后,以上的类属性将不在类中,而是在__mappings__属性指定的字典中存储 # 以上User类中有 # __mappings__ = { # "uid": ('uid', "int unsigned") # "name": ('username', "varchar(30)") # "email": ('email', "varchar(30)") # "password": ('password', "varchar(30)") # } # __table__ = "User" def __init__(self, **kwargs): for name, value in kwargs.items(): setattr(self, name, value) def save(self): fields = [] args = [] for k, v in self.__mappings__.items(): fields.append(v[0]) args.append(getattr(self, k, None)) sql = 'insert into %s (%s) values (%s)' % (self.__table__, ','.join(fields), ','.join([str(i) for i in args])) print('SQL: %s' % sql) u = User(uid=12345, name='Michael', email='test@orm.org', password='my-pwd') # print(u.__dict__) u.save()
效果
Found mapping: password ==> ('password', 'varchar(30)') Found mapping: email ==> ('email', 'varchar(30)') Found mapping: uid ==> ('uid', 'int unsigned') Found mapping: name ==> ('username', 'varchar(30)') SQL: insert into User (uid,password,username,email) values (12345,my-pwd,Michael,test@orm.org)
完善对数据类型的检测
class ModelMetaclass(type): def __new__(cls, name, bases, attrs): mappings = dict() # 判断是否需要保存 for k, v in attrs.items(): # 判断是否是指定的StringField或者IntegerField的实例对象 if isinstance(v, tuple): print('Found mapping: %s ==> %s' % (k, v)) mappings[k] = v # 删除这些已经在字典中存储的属性 for k in mappings.keys(): attrs.pop(k) # 将之前的uid/name/email/password以及对应的对象引用、类名字 attrs['__mappings__'] = mappings # 保存属性和列的映射关系 attrs['__table__'] = name # 假设表名和类名一致 return type.__new__(cls, name, bases, attrs) class User(metaclass=ModelMetaclass): uid = ('uid', "int unsigned") name = ('username', "varchar(30)") email = ('email', "varchar(30)") password = ('password', "varchar(30)") # 当指定元类之后,以上的类属性将不在类中,而是在__mappings__属性指定的字典中存储 # 以上User类中有 # __mappings__ = { # "uid": ('uid', "int unsigned") # "name": ('username', "varchar(30)") # "email": ('email', "varchar(30)") # "password": ('password', "varchar(30)") # } # __table__ = "User" def __init__(self, **kwargs): for name, value in kwargs.items(): setattr(self, name, value) def save(self): fields = [] args = [] for k, v in self.__mappings__.items(): fields.append(v[0]) args.append(getattr(self, k, None)) args_temp = list() for temp in args: # 判断入如果是数字类型 if isinstance(temp, int): args_temp.append(str(temp)) elif isinstance(temp, str): args_temp.append("""'%s'""" % temp) sql = 'insert into %s (%s) values (%s)' % (self.__table__, ','.join(fields), ','.join(args_temp)) print('SQL: %s' % sql) u = User(uid=12345, name='Michael', email='test@orm.org', password='my-pwd') # print(u.__dict__) u.save()
结果
Found mapping: uid ==> ('uid', 'int unsigned') Found mapping: password ==> ('password', 'varchar(30)') Found mapping: name ==> ('username', 'varchar(30)') Found mapping: email ==> ('email', 'varchar(30)') SQL: insert into User (email,uid,password,username) values ('test@orm.org',12345,'my-pwd','Michael')
抽取到基类中
class ModelMetaclass(type): def __new__(cls, name, bases, attrs): mappings = dict() # 判断是否需要保存 for k, v in attrs.items(): # 判断是否是指定的StringField或者IntegerField的实例对象 if isinstance(v, tuple): print('Found mapping: %s ==> %s' % (k, v)) mappings[k] = v # 删除这些已经在字典中存储的属性 for k in mappings.keys(): attrs.pop(k) # 将之前的uid/name/email/password以及对应的对象引用、类名字 attrs['__mappings__'] = mappings # 保存属性和列的映射关系 attrs['__table__'] = name # 假设表名和类名一致 return type.__new__(cls, name, bases, attrs) class Model(object, metaclass=ModelMetaclass): def __init__(self, **kwargs): for name, value in kwargs.items(): setattr(self, name, value) def save(self): fields = [] args = [] for k, v in self.__mappings__.items(): fields.append(v[0]) args.append(getattr(self, k, None)) args_temp = list() for temp in args: # 判断入如果是数字类型 if isinstance(temp, int): args_temp.append(str(temp)) elif isinstance(temp, str): args_temp.append("""'%s'""" % temp) sql = 'insert into %s (%s) values (%s)' % (self.__table__, ','.join(fields), ','.join(args_temp)) print('SQL: %s' % sql) class User(Model): uid = ('uid', "int unsigned") name = ('username', "varchar(30)") email = ('email', "varchar(30)") password = ('password', "varchar(30)") u = User(uid=12345, name='Michael', email='test@orm.org', password='my-pwd') # print(u.__dict__) u.save()
6.setattr()和getattr()的用法
setattr(对象, 属性, 属性值) 给对象中的属性设置值
getattr(对象, 属性[, 默认值]) 从对象中读取属性的值,可以设置默认值,如果不存在则读取默认值。