Is it possible to determine what a function needs for parameters -
Chris Mellon
arkanes at gmail.com
Wed May 2 11:00:54 EDT 2007
On 2 May 2007 07:22:07 -0700, rh0dium <steven.klass at gmail.com> wrote:
> 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()
>
This is far more work than you need. Push an (args, kwargs) tuple into
your arguments queue and call self.function(*args, **kwargs).
More information about the Python-list
mailing list