[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

1.5.2 and functools or similar

Troels Thomsen

3/9/2008 9:27:00 PM


Hello,

I am writing a simple delayed-call mechanism , that is causing a bit of
headache. Works like this:

myPrint(s)
print "..." + s

myTimer.add(myPrint , "hello" , 15)

This means that the myprint function is called in 15 seconds with the
parameter "hello".
The housekeeping of these timers is called by the main loop of the "os"

This works well but i would like to be able to use it with any number of
parameters

Functools is not a part of the 1.5.2+ python that I am running on (embedded
device),
so i tried to pass the parameters as a tuple like this

myTimer.add(myAdder , (3,6) , 15)

and the housekeeping function would then call the function like this

def updateTimers()
for timerItm in timerTable:
...
....
....
timerItm.func(*timerItm.parameters)

Works well on python 2.5 but not on 1.5.2 (?)


Current solution is to have default parameters None for the add function

def add( func , timeout , param1 = None , param2 = None)

And the update function then checks if parameters is specified

def updateTimers()
for timerItm in timerTable:
...
....
....
# ugly part :
if timerItm.param1 is not None and timerItm.param2 is not None:
timerItm.func(timerItm.param1, timerItm.param2) # two parameters
elif ......
timerItm.func(timerItm.param1) # one parameter
else
timerItm.func() # no parameters

This has the implication that I can not call a function with the parameter
None if I wanted to.
(not a huge problem)

Right now it works quite well with up to two parameters, it covers 99% of
usage. If I need to call a function with more parameters, i can always write
a wrapper function for it. Wondering if anyone had some sugestions ?


By the way, is it bad style to check for object identity instead of value
"None".
What aboutt integers ? if value is 0: ..
I guess it is implementation specific / could change in future versions ?


Thx,
Troels









3 Answers

Aaron Brady

3/9/2008 9:40:00 PM

0

On Mar 9, 4:26 pm, "Troels Thomsen" <nej tak ...> wrote:
> Hello,
>
> I am writing a simple delayed-call mechanism , that is causing a bit of
> headache. Works like this:
>
> myPrint(s)
>   print "..." + s
>
> myTimer.add(myPrint , "hello" , 15)
>
> This means that the myprint function is called in 15 seconds with the
> parameter "hello".
> The housekeeping of these timers is called by the main loop of the "os"
>
> This works well but i would like to be able to use it with any number of
> parameters
>
> Functools is not a part of the 1.5.2+ python that I am running on (embedded
> device),
> so i tried to pass the parameters as a tuple like this
>
> myTimer.add(myAdder , (3,6) , 15)
>
> and the housekeeping function would then call the function like this
>
> def updateTimers()
>   for timerItm in timerTable:
>   ...
>     ....
>       ....
>         timerItm.func(*timerItm.parameters)
>
> Works well on python 2.5 but not on 1.5.2 (?)
>
> Current solution is to have default parameters None for the add function
>
> def add( func , timeout , param1 = None , param2 = None)
>
> And the update function then checks if parameters is specified
>
> def updateTimers()
>   for timerItm in timerTable:
>   ...
>     ....
>       ....
>       # ugly part :
>       if timerItm.param1 is not None and timerItm.param2 is not None:
>         timerItm.func(timerItm.param1, timerItm.param2) # two parameters
>       elif ......
>         timerItm.func(timerItm.param1) # one parameter
>       else
>         timerItm.func() # no parameters
>
> This has the implication that I can not call a function with the parameter
> None if I wanted to.
> (not a huge problem)
>
> Right now it works quite well with up to two parameters, it covers 99% of
> usage. If I need to call a function with more parameters, i can always write
> a wrapper function for it. Wondering if anyone had some sugestions ?
>
> By the way, is it bad style to check for object identity instead of value
> "None".
> What aboutt integers ? if value is 0: ..
> I guess it is implementation specific / could change in future versions ?
>
> Thx,
> Troels

def g( arg1, arg2 ):
print( arg1, arg2 )
return arg1

def h( arg1 ):
print( arg1 )
return arg1

def caller( fun, *args, **kwargs ):
return fun( *args, **kwargs )

print( caller( g, 'abc', 'def' ) )
print( caller( h, 'abc' ) )

'''
abc def
abc
abc
abc
'''

Terry Reedy

3/9/2008 11:23:00 PM

0


"Troels Thomsen" <"nej tak..."@bag.python.org> wrote in message
news:47d45625$0$15876$edfadb0f@dtext01.news.tele.dk...
| def updateTimers()
| for timerItm in timerTable:
| ...
| ....
| ....
| timerItm.func(*timerItm.parameters)
|
| Works well on python 2.5 but not on 1.5.2 (?)

apply(timerItm.func, timerItm.parameters) # see
http://docs.python.org/lib/non-essential-built-in-...
apply disappears in 3.0

tjr



Paul Rubin

3/9/2008 11:35:00 PM

0

"Troels Thomsen" <nej tak ...> writes:
> timerItm.func(*timerItm.parameters)
>
> Works well on python 2.5 but not on 1.5.2 (?)

I think in 1.5.2 the *args notation wasn't present and you had to say:

apply(timerItm.func, timerItm.parameters)