三人行必有我师,手写三行必有所悟

当前位置:首页>Python>三人行必有我师,手写三行必有所悟

三人行必有我师,手写三行必有所悟

时间:2015-07-05 00:25来源:网络整理 作者:KKWL 点击:
因为买了本机器学习的书是python的,所以学习了一下python,就用python做了一个简单的web开发的mvc小框架。整个框架分为实体层,数据访问层,业务基类层,Web请求转发层,控制层和视图

因为买了本机器学习的书是python的,所以学习了一下python,就用python做了一个简单的web开发的mvc小框架。整个框架分为实体层,数据访问层,业务基类层,Web请求转发层,控制层和视图层。当然也包括一些辅助性工具类。下面一一介绍,希望对跟我一样初学python的有点帮助:
1)实体层
      由实体基类和用户实体类组成。实体类可以由数据库自动生成,提供了一个DbTools,可以自动生成规则的实体。下面是实体基类:
     import threading #用户错误类,用于抛出自定义的异常 class CustomError(RuntimeError): def __init__(self,args): self.args=args #实体的基类. class EntityB: def __init__(self): self.CurrFields=[] #根据属性名获取属性值 def GetValueByName(self,FieldName): if hasattr(self,FieldName): return getattr(self,FieldName) return None #根据属性名设置属性值 def SetValueByName(self,FieldName,Value): if hasattr(self,FieldName): return setattr(self,FieldName,Value) #定义了该属性,对象可枚举. def __getitem__(self,key): if type(key)==type('abc'): return self.GetValueByName(key) if type(key)==type(1): theFld = self.CurrFields[key] return self.GetValueByName(theFld) return None #设置属性值,key可以是索引,也可以是属性名. def __setitem__(self,key,value): if type(key)==type('abc'): self.SetValueByName(key,value) if type(key)==type(1): theFld = self.CurrFields[key] self.SetValueByName(theFld,value) #获取实体的表名. def GetTableName(self): theType = type(self) if hasattr(theType,'TableName'): return getattr(theType,'TableName') return '' #获取关键字段名 def GetKeyField(self): theType = type(self) if hasattr(theType,'KeyField'): return getattr(theType,'KeyField') return '' #获取字段名集合 def GetFields(self): theType = type(self) if hasattr(theType,'Fields'): return getattr(theType,'Fields') return [] InsertCondition = threading.Condition() InsertLockSign = False DeleteCondition = threading.Condition() DeleteLockSign = False UpdateAllCondition = threading.Condition() UpdateAllLockSign = False InsertSqlName='__InsertSql' UpdateAllSqlName='__UpdateSql' DelByPKSqlName='__DelByPKSql' DefaultSelectSQL='__DefaultSelectSql' #根据属性名获取类型的属性值。 def _GetClassValue(self,AttrName): theSelfType = type(self) theValue ='' if hasattr(theSelfType,AttrName): theValue=getattr(theSelfType,AttrName) return theValue #根据属性名设置类型的属性值. def _SetClassValue(self,AttrName,value): theSelfType = type(self) theValue ='' if hasattr(theSelfType,AttrName): setattr(theSelfType,AttrName,value) #获取字段参数 def GetFieldParams(self): return self._GetClassValue('FieldParams') #获取插入的SQL def GetInsertSQL(self): theSQL =self._GetClassValue(EntityB.InsertSqlName) if (theSQL==None or theSQL==''): EntityB.InsertCondition.acquire() try: if EntityB.InsertLockSign: EntityB.InsertCondition.wait() InsertLockSign = True theSQL =self._GetClassValue(EntityB.InsertSqlName) if (theSQL==None or theSQL==''): theTableName=self.GetTableName() theFields=self.GetFields() if theTableName=='' or theFields==[]: raise CustomError('表名或字段为空!') theSQL='INSERT INTO '+ theTableName theFlds='' theVals='' theFldParams = self.GetFieldParams() for theF in theFields: if theFlds=='': theFlds += theF theVals += theFldParams[theF]['DSFmt'] else: theFlds += ','+theF theVals +=','+theFldParams[theF]['DSFmt'] theSQL+='('+theFlds+') values('+theVals+')' self._SetClassValue(EntityB.InsertSqlName,theSQL) return theSQL finally: InsertLockSign=False EntityB.InsertCondition.notify() EntityB.InsertCondition.release() else: return theSQL #获取根据主键删除SQL def GetDelByPKSQL(self): theSQL =self._GetClassValue(EntityB.DelByPKSqlName) if (theSQL==None or theSQL==''): EntityB.DeleteCondition.acquire() try: if EntityB.DeleteLockSign: EntityB.DeleteCondition.wait() DeleteLockSign = True theSQL =self._GetClassValue(EntityB.DelByPKSqlName) if (theSQL==None or theSQL==''): theTableName=self.GetTableName() theKeyField=self.GetKeyField() if theTableName=='' or theKeyField=='': raise CustomError('表名或主键为空!') theFldParams = self.GetFieldParams() theSQL='DELETE FROM '+ theTableName+' WHERE '+theKeyField+'='+theFldParams[theKeyField]['DSFmt'] self._SetClassValue(EntityB.DelByPKSqlName,theSQL) return theSQL finally: DeleteLockSign=False EntityB.DeleteCondition.notify() EntityB.DeleteCondition.release() else: return theSQL #获取更新所有字段的SQL语句(根据主键更新) def GetUpdateAllSQL(self): theSQL =self._GetClassValue(EntityB.UpdateAllSqlName) if (theSQL==None or theSQL==''): EntityB.UpdateAllCondition.acquire() try: if EntityB.UpdateAllLockSign: EntityB.UpdateAllCondition.wait() UpdateAllLockSign = True theSQL =self._GetClassValue(EntityB.UpdateAllSqlName) if (theSQL==None or theSQL==''): theTableName=self.GetTableName() theFields=self.GetFields() theKeyField=self.GetKeyField() if theTableName=='' or theFields==[] or theKeyField=='': raise CustomError('表名、主键或字段为空!') theSQL='UPDATE '+ theTableName +' SET ' theFlds='' theFldParams = self.GetFieldParams() for theF in theFields: if(theF != theKeyField): if theFlds=='': theFlds += theF+'= '+theFldParams[theF]['DSFmt'] else: theFlds += ','+theF+'= '+theFldParams[theF]['DSFmt'] theSQL+= theFlds +' WHERE '+theKeyField+'='+theFldParams[theKeyField] self._SetClassValue(EntityB.UpdateAllSqlName,theSQL) return theSQL finally: UpdateAllLockSign=False EntityB.UpdateAllCondition.notify() EntityB.UpdateAllCondition.release() else: return theSQL #获取缺省的查询SQL def GetDefaultSelectSQL(self): theTableName=self.GetTableName() return 'SELECT * FROM ' + theTableName + ' WHERE 1=1' #def __delitem__(self)

------分隔线----------------------------