是否有可能确定参数需要什么功能 - [英] Is it possible to determine what a function needs for parameters -
问题描述
大家好,
以下是一个基本的线程程序。我的基本想法是我有一个
函数需要使用数据队列运行。早在我指定
时我的函数只需要接受基本参数(没有
postional * args或* kwargs)但是现在我正在重写它而我想要
接受这些。无论如何,确定给定函数需要什么参数
并格式化适当的参数。我的问题是我觉得我的Kludge部分只是 - 一个kludge。
虽然它适用于大多数情况但它并不适用于所有人(尝试在function4上运行这个
,例如...)。我想我有多个
if语句的问题,我认为它应该只是看看接受了什么参数
并适当地格式化它们。想法?
如果我真的搞砸了 - 请帮我回到正确的
轨道。我很感激所有的帮助,很高兴向专家学习
。
随机导入
导入线程
导入队列
类WorkerB(threading.Thread):
def __init __(自我,* args,** kwargs):
self.id = kwargs.get(''id'',0)
self.requestQ = kwargs.get(''requestQ '',无)
self.function = kwargs.get(''function'',None)
threading.Thread .__ init __(self,name = self .__ class__ .__ name__
+"。" + str(self.id))
def run(self):
1:
input = self.requestQ.get()
如果输入为None:break
#我如何看看这个函数并确定它需要什么才能将它作为输入应用?
#--------启动Kludge --- -------
tu = dic = False
if isinstance(输入,元组):
尝试:
if isinstance(输入[0],元组):tu = True
elif isinstance(输入[0],列表):tu = True
除了:传递
尝试:
if isinstance(输入[1],dict):dic = True
除外:传递
如果tu和dic:
print" - 找到元组和列表
result = self.function(* input [0],** input [1])
elif tu而不是dic:
print" -Tuple发现
result = self.function(* input [0])
elif isinstance(输入,列表):
print" ; -list only found
result = self.function(* input)
else:
print" -Unknown"
result = self.function(输入)
#--------结束Kludge -------- -
def function1(arg1):
print arg1
def function2(* a):
print" args",
def function3(* a,** kw):
print" args" ,一个
打印kwargs,kw
def function4(arg1,* a,** kw):
print arg1
print" args",a
print" kwargs",kw
def main():
lod = 2
myQ = Queue.Queue()
#一个基本的例子
打印" \\\
==示例1"
for x in range(lod):myQ.put(random.random())
myQ.put(None)
a = WorkerB(requestQ = myQ,function = function1).start()
#扔一些args
打印\ n ==示例2
for x in range(lod):myQ.put([" a,b,c])
myQ.put(无)
a = WorkerB(requestQ = myQ,function = function2).start()
#扔掉它args和kwargs
print" \\\
== Example 3"
for x in range(lod):myQ.put((1,2,3 ),
{" input":random.random()," loglevel":10}))
myQ.put(无)
a = WorkerB(requestQ = myQ,function = function3).start()
#扔掉args和kwargs
print" \\ \\ n ==示例4什么都不做!!
for x in range(lod):myQ.put((" alpha",(1,2,3),
{" input":random.random()," loglevel":10}))
myQ.put(无)
a = WorkerB( requestQ = myQ,function = function4).start()
if __name__ ==''__ main__'':
main()
Hi all,
Below is a basic threading program. The basic I idea is that I have a
function which needs to be run using a queue of data. Early on I
specified my function needed to only accept basic parameters ( no
postional *args or *kwargs ) but now I am re-writing it and I want to
accept these. Is there anyway to determine what parameters are needed
by a given function and format the arguments given appropriately. My
problem is that I feel my "Kludge"section is just that - a kludge.
While it works for most cases it won''t work for all (Try running this
on function4 for example...). I guess I have a problem with multiple
if statements and I think it should just look to see what parameters
are accepted and format them appropriately. Thoughts?
If I am really screwing this up - please help me get back on the right
track. I appreciate all of the help I get and it''s great to learn from
the experts.
import random
import threading
import Queue
class WorkerB(threading.Thread):
def __init__(self, *args, **kwargs):
self.id = kwargs.get(''id'', 0)
self.requestQ = kwargs.get(''requestQ'', None)
self.function = kwargs.get(''function'', None)
threading.Thread.__init__(self, name=self.__class__.__name__
+"."+str(self.id))
def run(self):
while 1:
input = self.requestQ.get()
if input is None: break
# How do I look at the function and determine what it
requires then apply that as an input?
# -------- Start Kludge ----------
tu=dic=False
if isinstance(input, tuple):
try:
if isinstance(input[0], tuple): tu = True
elif isinstance(input[0], list): tu=True
except: pass
try:
if isinstance(input[1], dict):dic = True
except: pass
if tu and dic:
print " -Tuple and list found"
result = self.function(*input[0], **input[1])
elif tu and not dic:
print " -Tuple found"
result = self.function(*input[0])
elif isinstance(input, list):
print " -list only found"
result = self.function(*input)
else:
print " -Unknown"
result = self.function(input)
# -------- End Kludge ----------
def function1(arg1):
print arg1
def function2(*a ):
print "args", a
def function3(*a, **kw):
print "args", a
print "kwargs", kw
def function4(arg1, *a, **kw):
print arg1
print "args", a
print "kwargs", kw
def main():
lod = 2
myQ=Queue.Queue()
# A basic example
print "\n== Example 1"
for x in range(lod):myQ.put( random.random() )
myQ.put(None)
a=WorkerB(requestQ=myQ, function=function1).start()
# Throw at is some args
print "\n== Example 2"
for x in range(lod):myQ.put(["a","b","c"])
myQ.put(None)
a=WorkerB(requestQ=myQ, function=function2).start()
# Throw at it both args and kwargs
print "\n== Example 3"
for x in range(lod):myQ.put(((1,2,3),
{"input":random.random(),"loglevel":10}))
myQ.put(None)
a=WorkerB(requestQ=myQ, function=function3).start()
# Throw at it both args and kwargs
print "\n== Example 4 Does nothing!!"
for x in range(lod):myQ.put(("alpha",(1,2,3),
{"input":random.random(),"loglevel":10}))
myQ.put(None)
a=WorkerB(requestQ=myQ, function=function4).start()
if __name__ == ''__main__'':
main()
推荐答案
rh0diumaécrit:
rh0dium a écrit :
大家好,
以下是基本的线程程序。我的基本想法是我有一个
函数需要使用数据队列运行。早在我指定
时我的函数只需要接受基本参数(没有
postional * args或* kwargs)但是现在我正在重写它而我想要
接受这些。无论如何,确定给定函数需要什么参数
并格式化适当的参数。
Hi all,
Below is a basic threading program. The basic I idea is that I have a
function which needs to be run using a queue of data. Early on I
specified my function needed to only accept basic parameters ( no
postional *args or *kwargs ) but now I am re-writing it and I want to
accept these. Is there anyway to determine what parameters are needed
by a given function and format the arguments given appropriately.
是 - 使用inspect.getargspec。我手头还没有示例代码,
但它并不是很复杂。
Yes - using inspect.getargspec. I don''t have example code at hand yet,
but it''s not really complicated.
2007年5月2日07: 22:07 -0700,rh0dium< st ********** @ gmail.comwrote:
On 2 May 2007 07:22:07 -0700, rh0dium <st**********@gmail.comwrote:
大家好,
下面是一个基本的线程程序。我的基本想法是我有一个
函数需要使用数据队列运行。早在我指定
时我的函数只需要接受基本参数(没有
postional * args或* kwargs)但是现在我正在重写它而我想要
接受这些。无论如何,确定给定函数需要什么参数
并格式化适当的参数。我的问题是我觉得我的Kludge部分只是 - 一个kludge。
虽然它适用于大多数情况但它并不适用于所有人(尝试在function4上运行这个
,例如...)。我想我有多个
if语句的问题,我认为它应该只是看看接受了什么参数
并适当地格式化它们。想法?
如果我真的搞砸了 - 请帮我回到正确的
轨道。我很感激所有的帮助,很高兴向专家学习
。
随机导入
导入线程
导入队列
类WorkerB(threading.Thread):
def __init __(自我,* args,** kwargs):
self.id = kwargs.get(''id'',0)
self.requestQ = kwargs.get(''requestQ '',无)
self.function = kwargs.get(''function'',None)
threading.Thread .__ init __(self,name = self .__ class__ .__ name__
+"。" + str(self.id))
def run(self):
1:
input = self.requestQ.get()
如果输入为None:break
#我如何看看这个函数并确定它需要什么才能将它作为输入应用?
#--------启动Kludge --- -------
tu = dic =错误
如果isinstance(输入,元组):
尝试:
if isinstance(输入[0],元组):tu = True
elif isinstance(输入[0],列表):tu = True
除外:传递
尝试:
if isinstance(输入[1],dict):dic = True
除外:传递
如果tu和dic:
print" - 找到元组和列表
result = self.function(* input [0],** input [1])
elif tu而不是dic:
print" -Tuple发现
result = self.function(* input [0])
elif isinstance(输入,列表):
print" ; -list only found
result = self.function(* input)
else:
print" -Unknown"
result = self.function(输入)
#--------结束Kludge -------- -
def function1(arg1):
print arg1
def function2(* a):
print" args",
def function3(* a,** kw):
print" args" ,一个
打印kwargs,kw
def function4(arg1,* a,** kw):
print arg1
print" args",a
print" kwargs",kw
def main():
lod = 2
myQ = Queue.Queue()
#一个基本的例子
print" \\\
== Example 1"
for x in range(lod):myQ.put(random.random())
myQ。 put(无)
a = WorkerB(requestQ = myQ,function = function1).start()
#扔一些args
print" \\\
== Example 2" ;
for x in range(lod):myQ.put([" a"," b"," c"])
myQ.put(没有)
a = WorkerB(requestQ = myQ,function = function2).start()
#扔掉args和kwargs
print" \\\
== Example 3"
for x in range(lod):myQ.put(((1,2,3),
{" input":random.random()," loglevel":10}))
myQ.put(无)
a = WorkerB(requestQ = myQ) ,function = function3).start()
#扔掉args和kwargs
print" \\\
==例4什么都不做! !<
for x in range(lod):myQ.put((" alpha",(1,2,3),
{" input" :random.random()," loglevel":10}))
myQ.put(无)
a = WorkerB(requestQ = myQ,function = function4)。 start()
如果__name__ ==''__ main__'':
main()
Hi all,
Below is a basic threading program. The basic I idea is that I have a
function which needs to be run using a queue of data. Early on I
specified my function needed to only accept basic parameters ( no
postional *args or *kwargs ) but now I am re-writing it and I want to
accept these. Is there anyway to determine what parameters are needed
by a given function and format the arguments given appropriately. My
problem is that I feel my "Kludge"section is just that - a kludge.
While it works for most cases it won''t work for all (Try running this
on function4 for example...). I guess I have a problem with multiple
if statements and I think it should just look to see what parameters
are accepted and format them appropriately. Thoughts?
If I am really screwing this up - please help me get back on the right
track. I appreciate all of the help I get and it''s great to learn from
the experts.
import random
import threading
import Queue
class WorkerB(threading.Thread):
def __init__(self, *args, **kwargs):
self.id = kwargs.get(''id'', 0)
self.requestQ = kwargs.get(''requestQ'', None)
self.function = kwargs.get(''function'', None)
threading.Thread.__init__(self, name=self.__class__.__name__
+"."+str(self.id))
def run(self):
while 1:
input = self.requestQ.get()
if input is None: break
# How do I look at the function and determine what it
requires then apply that as an input?
# -------- Start Kludge ----------
tu=dic=False
if isinstance(input, tuple):
try:
if isinstance(input[0], tuple): tu = True
elif isinstance(input[0], list): tu=True
except: pass
try:
if isinstance(input[1], dict):dic = True
except: pass
if tu and dic:
print " -Tuple and list found"
result = self.function(*input[0], **input[1])
elif tu and not dic:
print " -Tuple found"
result = self.function(*input[0])
elif isinstance(input, list):
print " -list only found"
result = self.function(*input)
else:
print " -Unknown"
result = self.function(input)
# -------- End Kludge ----------
def function1(arg1):
print arg1
def function2(*a ):
print "args", a
def function3(*a, **kw):
print "args", a
print "kwargs", kw
def function4(arg1, *a, **kw):
print arg1
print "args", a
print "kwargs", kw
def main():
lod = 2
myQ=Queue.Queue()
# A basic example
print "\n== Example 1"
for x in range(lod):myQ.put( random.random() )
myQ.put(None)
a=WorkerB(requestQ=myQ, function=function1).start()
# Throw at is some args
print "\n== Example 2"
for x in range(lod):myQ.put(["a","b","c"])
myQ.put(None)
a=WorkerB(requestQ=myQ, function=function2).start()
# Throw at it both args and kwargs
print "\n== Example 3"
for x in range(lod):myQ.put(((1,2,3),
{"input":random.random(),"loglevel":10}))
myQ.put(None)
a=WorkerB(requestQ=myQ, function=function3).start()
# Throw at it both args and kwargs
print "\n== Example 4 Does nothing!!"
for x in range(lod):myQ.put(("alpha",(1,2,3),
{"input":random.random(),"loglevel":10}))
myQ.put(None)
a=WorkerB(requestQ=myQ, function=function4).start()
if __name__ == ''__main__'':
main()
这远远不够比你需要更多的工作。将(args,kwargs)元组推入
你的参数队列并调用self.function(* args,** kwargs)。
This is far more work than you need. Push an (args, kwargs) tuple into
your arguments queue and call self.function(*args, **kwargs).
这比你需要的工作多得多。将(args,kwargs)元组推入
This is far more work than you need. Push an (args, kwargs) tuple into
你的参数队列并调用self.function(* args,** kwargs)。
your arguments queue and call self.function(*args, **kwargs).
没有看到我尝试了那就不行了。
我假设你所指的是什么这是(有效)
Q.put(((),{a:" foo",b:" bar}))
输入= Q.get()
self.function(*输入[0],**输入[1])
这将如果没有满足几个条件,显然会失败 - 因此
kludge。我在这里遗漏了什么吗?
No see I tried that and that won''t work.
I''m assuming what you are referring to is this (effectively)
Q.put(((),{a:"foo", b:"bar}))
input = Q.get()
self.function( *input[0], **input[1])
This will obviously fail if several conditions aren''t met - hence the
kludge. Am I missing something here?
这篇关于是否有可能确定参数需要什么功能 - 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!