十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
function="luckywin."+case_name+"."+def_name
目前创新互联已为上千家的企业提供了网站建设、域名、雅安服务器托管、网站托管、服务器租用、企业网站设计、翁源网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
function=eval(function)
function(player_id,test_url)
function是个字符串,function(player_id,test_url) 这样写肯定不是字符串,怎么能用eval呢,直接eval function返回函数名,然后调用函数
import inspect
from demo import demo
#传入函数,改变函数内部变量a,从a=1改变成a=2
def cfunc(func):
#当前位置的全部局部变量
lc=locals()
#获得函数的内容
func_code=inspect.getsource(func)
#使用replace改变函数内部的变量
func_code=func_code.replace('a=1','a=2')
#动态创建函数
exec(func_code)
#获得函数并返回
res_func=lc[func.__name__]
return res_func
demo=cfunc(demo)
demo()
class Node(object):
def __init__(self, name):
self._name = name
self._value = None
self._from = None
self._next = []
def setValue(self, value):
self._value = value
def setNext(self, node):
self._next.append(node)
def setFrom(self, node):
self._from = node
def getValue(self):
return self._value
def getNext(self):
return self._next
def getName(self):
return self._name
def getFrom(self):
return self._from
class BFtree():
def __init__(self, dList):
self._dList = dList
def bfValue(self, start):
cur = start
if cur.getNext() is not None:
for node in cur.getNext():
path = [cur.getName(), node.getName()]
path.sort()
path = ''.join(path)
value = cur.getValue() + self._dList[path]
if node.getValue() is None or value node.getValue():
node.setValue(value)
node.setFrom(cur)
self.bfValue(node)
def move(self, start, end):
print 'From: ', start.getName(), ' to: ', end.getName()
start.setValue(0)
self.bfValue(start)
trace = [end.getName()]
cur = end
while cur.getFrom() is not None:
cur = cur.getFrom()
trace.append(cur.getName())
trace = reversed(trace)
print 'The path is ', ' '.join(trace),' and the value is ', end.getValue()
#builidng node
a = Node('A')
b = Node('B')
c = Node('C')
d = Node('D')
e = Node('E')
f = Node('F')
g = Node('G')
h = Node('H')
#build tree
a.setNext(b)
a.setNext(c)
a.setNext(d)
b.setNext(a)
b.setNext(g)
c.setNext(a)
c.setNext(g)
c.setNext(e)
d.setNext(a)
d.setNext(e)
e.setNext(c)
e.setNext(b)
e.setNext(f)
f.setNext(e)
g.setNext(b)
g.setNext(c)
g.setNext(h)
h.setNext(g)
#build distance list
dList = dict();
dList['AB'] = 1
dList['AC'] = 2
dList['AD'] = 3
dList['BG'] = 1
dList['BE'] = 6
dList['CE'] = 2
dList['CG'] = 5
dList['DE'] = 4
dList['EF'] = 3
dList['GH'] = 4
#build BFtree
tree = BFtree(dList)
tree.move(a,h)
tree.move(a,f)
tree.move(a,e)
代码略长....
给你这样一个例子吧,这个例子里面有动态增加类的函数。
声明一个类,类初始化的时候读取配置文件,根据配置列表加载特定目录下的模块下的函数,函数和模块同名,将此函数动态加载为类的成员函数。
代码如下所示:
class WinBAS(Bas):
def __init__(self):
self.__baslist = {}
self.__Init_Modules()
pass
def __Init_Modules(self):
import modplugs
for m in modplugs.__moduleset__:
mh = __import__('modules.' + m)# + '.' + m)
ma = getattr(mh, m)# + '.' + m)
ma = getattr(ma, m)
setattr(self.__class__, m, ma)
modplugs.py是模块配置文件如下:
__moduleset__ = [
'BAS_GetUserList',
]
然后建立目录modules下面建立一个空的__init__.py文件,把目录变为一个包,在modules目录下建立真正的BAS_GetUserList实现:BAS_GetUserList文件中有个BAS_GetUserList函数如下:
def BAS_GetUserList(self, strs):
return [0, strs]
这样WinBAS类就可以动态加入了BAS_GetUserList函数。