473,651 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it possible to determine what a function needs for parameters -

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"sec tion 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(threadi ng.Thread):

def __init__(self, *args, **kwargs):
self.id = kwargs.get('id' , 0)
self.requestQ = kwargs.get('req uestQ', None)
self.function = kwargs.get('fun ction', None)
threading.Threa d.__init__(self , name=self.__cla ss__.__name__
+"."+str(self.i d))

def run(self):
while 1:
input = self.requestQ.g et()
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(inpu t, tuple):
try:
if isinstance(inpu t[0], tuple): tu = True
elif isinstance(inpu t[0], list): tu=True
except: pass
try:
if isinstance(inpu t[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(inpu t, list):
print " -list only found"
result = self.function(* input)
else:
print " -Unknown"
result = self.function(i nput)

# -------- 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(reque stQ=myQ, function=functi on1).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(reque stQ=myQ, function=functi on2).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(),"logl evel":10}))
myQ.put(None)
a=WorkerB(reque stQ=myQ, function=functi on3).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(),"logl evel":10}))
myQ.put(None)
a=WorkerB(reque stQ=myQ, function=functi on4).start()
if __name__ == '__main__':
main()

May 2 '07 #1
10 1585
rh0dium a écrit :
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.
Yes - using inspect.getargs pec. I don't have example code at hand yet,
but it's not really complicated.
May 2 '07 #2
On 2 May 2007 07:22:07 -0700, rh0dium <st**********@g mail.comwrote:
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"sec tion 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(threadi ng.Thread):

def __init__(self, *args, **kwargs):
self.id = kwargs.get('id' , 0)
self.requestQ = kwargs.get('req uestQ', None)
self.function = kwargs.get('fun ction', None)
threading.Threa d.__init__(self , name=self.__cla ss__.__name__
+"."+str(self.i d))

def run(self):
while 1:
input = self.requestQ.g et()
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(inpu t, tuple):
try:
if isinstance(inpu t[0], tuple): tu = True
elif isinstance(inpu t[0], list): tu=True
except: pass
try:
if isinstance(inpu t[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(inpu t, list):
print " -list only found"
result = self.function(* input)
else:
print " -Unknown"
result = self.function(i nput)

# -------- 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(reque stQ=myQ, function=functi on1).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(reque stQ=myQ, function=functi on2).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(),"logl evel":10}))
myQ.put(None)
a=WorkerB(reque stQ=myQ, function=functi on3).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(),"logl evel":10}))
myQ.put(None)
a=WorkerB(reque stQ=myQ, function=functi on4).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).
May 2 '07 #3
This is far more work than you need. Push an (args, kwargs) tuple into
your arguments queue and call self.function(* args, **kwargs).
No see I tried that and that won't work.

I'm assuming what you are referring to is this (effectively)

Q.put(((),{a:"f oo", 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?

May 2 '07 #4
rh0dium wrote:
>This is far more work than you need. Push an (args, kwargs) tuple into
your arguments queue and call self.function(* args, **kwargs).

No see I tried that and that won't work.
Won't work? How does it fail?
I'm assuming what you are referring to is this (effectively)

Q.put(((),{a:"f oo", 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?

Obviously? Conditions? What conditions?

We do things like this constantly, and in fact, it *does* work.

Please tell us how it fails, or what is unsatisfactory about it.

Gary Herron
May 2 '07 #5
On 2 May 2007 08:13:12 -0700, rh0dium <st**********@g mail.comwrote:
This is far more work than you need. Push an (args, kwargs) tuple into
your arguments queue and call self.function(* args, **kwargs).

No see I tried that and that won't work.

I'm assuming what you are referring to is this (effectively)

Q.put(((),{a:"f oo", 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?
Assuming that the caller correctly pushes arguments, how can this fail?
May 2 '07 #6
On May 2, 7:49 am, Bruno Desthuilliers <bruno.
42.desthuilli.. .@wtf.websitebu ro.oops.comwrot e:
Yes - using inspect.getargs pec. I don't have example code at hand yet,
but it's not really complicated.
Viola!! Hey this works!! Now I have modified my code to do this - way
cool (still kind of a mess though)

args, varargs, varkw, defs =
inspect.getargs pec(self.functi on)
# Deal with just some args
if varargs is None and varkw is None:
result=self.fun ction(input)
# Deal with *args
if varkw is None and varargs is not None and len(args) >
0:
result=self.fun ction(input[0:-1], *input[-1])
if varkw is None and varargs is not None and len(args)==0:
result=self.fun ction(*input[0])
# Deal with *kwargs
if varkw is not None and varargs is not None and len(args)
0:
result=self.fun ction(input[0:-2], *input[-2],
**input[-1])
if varkw is not None and varargs is not None and
len(args)==0:
result=self.fun ction(*input[-2], **input[-1])
if varkw is not None and varargs is None and len(args) >
0:
result=self.fun ction(input[0:-1], **input[-1])
if varkw is not None and varargs is None and len(args) ==
0:
result=self.fun ction(**input[0])

Now this worked until I threw a function which looked like this

def func5( input1, input2, input3 )
pass

So it barfed because of this..

if varargs is None and varkw is None:
result=self.fun ction(input)

but all of the parameters were lumped as a list so input1 contained
them all...
A small tweak turned into this..
if varargs is None and varkw is None:
if isinstance(inpu t, tuple):
result=self.fun ction(*input)
else:
result=self.fun ction(input)

But now I suppose I need to do this for all of them but that will
break my other logic...

Yuck - I have to be missing something here.
May 2 '07 #7
On May 2, 8:25 am, Gary Herron <gher...@island training.comwro te:
rh0dium wrote:
This is far more work than you need. Push an (args, kwargs) tuple into
your arguments queue and call self.function(* args, **kwargs).
No see I tried that and that won't work.

Won't work? How does it fail?I'm assuming what you are referring to is this (effectively)
Q.put(((),{a:"f oo", 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?

Obviously? Conditions? What conditions?

We do things like this constantly, and in fact, it *does* work.

Please tell us how it fails, or what is unsatisfactory about it.

Gary Herron
Good - It looks like I am the one who is clueless..

If I do this:

def funcA(input):
pass

Then I run the code
for x in range(lod):myQ. put(random.rand om())
myQ.put(None)
a=WorkerB(reque stQ=myQ, function=funcA) .start()

This will fail because there isn't an input[1]

May 2 '07 #8
On 2 May 2007 09:41:56 -0700, rh0dium <st**********@g mail.comwrote:
On May 2, 8:25 am, Gary Herron <gher...@island training.comwro te:
rh0dium wrote:
>This is far more work than you need. Push an (args, kwargs) tuple into
>your arguments queue and call self.function(* args, **kwargs).
No see I tried that and that won't work.
Won't work? How does it fail?I'm assuming what you are referring to is this (effectively)
Q.put(((),{a:"f oo", 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?
Obviously? Conditions? What conditions?

We do things like this constantly, and in fact, it *does* work.

Please tell us how it fails, or what is unsatisfactory about it.

Gary Herron

Good - It looks like I am the one who is clueless..

If I do this:

def funcA(input):
pass

Then I run the code
for x in range(lod):myQ. put(random.rand om())
myQ.put(None)
a=WorkerB(reque stQ=myQ, function=funcA) .start()

This will fail because there isn't an input[1]

Thats because you put the wrong value into the arguments queue.
For this use case, we define the arguments queue as being a source of 2-tuples,
with an argument list and a kwargs dict. So you have to do:

for x in range(lod):
myQ.put((random .random(), {}))

(don't be afraid of indentation and newlines - I started to modify
your source to provide a working example and got frustrated
reformatting it so I could read it)

Since you've now defined the external interface for your system (pass
it a queue of argument, kwargs tuples and a callable) it's the
responsibility of the caller to correctly satisfy that interface.
May 2 '07 #9
On Wed, 02 May 2007 07:22:07 -0700, rh0dium 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.
Is this meant to be just a programming exercise to see how clever you can
be? It's okay if it is, but if it is meant to be something useful,
well, I can't imagine ever being in a situation where I know I have
to pass arguments (say) 4, 5, "Hello", and None to a function, but not
know whether they should be positional arguments or keyword arguments.

Or, to put it another way... the usual procedure is for the developer
(that's you) to read the function API to find out what arguments the
function expects, and how it expects them, and then the developer
modifies the parameters used accordingly.

--
Steven.

May 2 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2753
by: Lenard Lindstrom | last post by:
I was wondering if anyone has suggested having Python determine a method's kind from its first parameter. 'self' is a de facto reserved word; 'cls' is a good indicator of a class method ( __new__ is a special case ). The closest to this I could find was the 2002-12-04 posting 'metaclasses and static methods' by Michele Simionato. The posting's example metaclass uses the method's name. I present my own example of automatic method kind...
20
3103
by: svend | last post by:
I'm messing with some code here... Lets say I have this array: a1 = ; And I apply slice(0) on it, to create a copy: a2 = a1.slice(0); But this isn't a true copy. If I go a1 = 42, and then alert(a2) I will see 42 there too. I'm doubting, but I was
18
2877
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code that applies to all these events, but I need to have specific code execute when the form closes. The properties for this method are sender (the originator) and e (event arguments). I know how to get typeof (sender) to determine what form or...
0
3163
by: Andrew Dowding | last post by:
Hi Everybody, I have been looking at problems with my Windows Forms C# application and it's little Jet 4 (Access) database for the last few days. The Windows Forms app implements a facade and implementation, data abstraction layer. But because each data adapter in the implementation layer has a connection object that opens and closes as needed, I found I got several errors from the Jet engine when there were simultaneous connections to...
0
281
by: martin | last post by:
Hi, I am writin a function to execute stored procedures. The function gets passed an array with the values of the parameters and if they are input out put params. I attach these to the command onject no problem and execute the sp. After execution I need to get back he values of the output parameters. what I would like to do is 1. loop through the parameters collection.
13
2536
by: Alison Givens | last post by:
....... that nobody knows the answer. I can't imagine that I am the only one that uses parameters in CR. So, my question again: I have the following problem. (VB.NET 2003 with CR) I have a report with a multiple-value discrete value and a rangevalue. The report shows fine in the viewer, but when I hit the export to pdf
11
2008
by: vbgunz | last post by:
Hello all, I am just learning Python and have come across something I feel might be a bug. Please enlightenment me... The following code presents a challenge. How in the world do you provide an argument for *arg4? ## ============================================================ def argPrecedence(par1, par2=0, par3=0, *par4, **par5): print 'par1 =', par1, ' # positional argument' print 'par2 =', par2, ' # keyword argument'
5
1797
by: patrin | last post by:
Hi All, given the source document: <?xml version="1.0" encoding="UTF-8"?> <root> <child> <test id="1" name="first child"/> </child> <child>
3
1362
by: | last post by:
I am enjoying making generalized methods to serve common needs, such as: ImageProcessing.MakeQuickResize(); ImageProcessing.Sharpen(); FileOps.CreateYearAndMonthAndDayDirectoryBasedOnDate() Sender.EmailCommaDelimitedList() What would be really cool is if I could make a generalized function that would accept other methods as parameters, and would perform the methods on another parameterized item that's fed to the function. So we might...
0
8357
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8803
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8700
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8581
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7298
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1910
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.