pythonmvc简单框架设计_一个简单额Python MVC框架(3)

MVC中的M已经介绍完了,C和V都是用户代码,先看几个工具类:

A)全局变量类,用户定义全局变量和一个用于产生普通对象的类型:

gl_A=1

gl_B='a'

class Obj:

def __init__(self):

self.Obj=self如果需要,大家可以定义自己的全局性变量。

B)OsHelper.用于动态加载模块,获取类型

import imp

import inspect

import uuid

#加载控制模块和类,读取文件,获取GUID

class OsHelper:

@staticmethod

def LoadModule(controllerName):

return imp.load_source(controllerName,controllerName+'.py')

@staticmethod

def LoadClass(module,classname,ignorecase=True):

if ignorecase :

theMembers = inspect.getmembers(module)

for theM in theMembers:

if theM[0].upper()==classname.upper():

return theM[1]

else:

return getattr(module,classname)

@staticmethod

def GetGuid():

return uuid.uuid1().__str__().replace('-','')

@staticmethod

def ReadFile(filepath,AEncoding='utf-8'):

if AEncoding==None:

AEncoding='utf-8'

theFile = open(file=filepath,encoding=AEncoding)

try:

return theFile.read()

finally:

theFile.close()C)HtmlHelper模块,主要提供html处理的类。这里提供了一个自动根据表单域给实体赋值的方法和Cookie处理的类。大家可以根据自己的需要增加自己的方法。

from os import environ

import cgi, cgitb

from ListAttr import ObjOpt

class HtmlTools:

#自动根据表单域给对象赋值

@staticmethod

def TryUpdate(obj,form):

theProperties = ObjOpt.GetPropertyNames(obj)

theObjName = ObjOpt.GetClassName(type(obj))

theRet={}

if theProperties != None:

for theP in theProperties:

try:

theVal =form.getvalue(theP)

if theVal!=None:

setattr(obj,theP,theVal)

else:

theVal =form.getvalue(theObjName+'.'+theP)

if theVal!=None:

setattr(obj,theP,theVal)

if theVal!=None:

theRet[theP] = theVal

except Exception:

return theRet

return theRet

#获取Cookie信息,字典方式返回.

@staticmethod

def GetCookies(Environ):

theRet={}

theCookieStr=Environ.get('HTTP_COOKIE')

if theCookieStr!=None:

for cookie in theCookieStr.split(";"):

(key, value ) = cookie.split('=')

theRet[key]=value

return theRet

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值