classModelMetaclass(type):def__new__(cls, name, bases, attrs):
mappings =dict()for k, v in attrs.items():ifisinstance(v,tuple):
mappings[k]= v
for k in mappings.keys():
attrs.pop(k)
attrs['__mappings__']= mappings
attrs['__table__']= name
returntype.__new__(cls, name, bases, attrs)classUser(metaclass=ModelMetaclass):
uid =('uid',"int unsigned")
name =('username',"varchar(30)")
email =('email',"varchar(30)")
password =('password',"varchar(30)")def__init__(self,**kwargs):for name, value in kwargs.items():setattr(self, name, value)defsave(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')
u.save()