Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > 1.5.2 and functools or similar

Reply
Thread Tools

1.5.2 and functools or similar

 
 
Troels Thomsen
Guest
Posts: n/a
 
      03-09-2008

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









 
Reply With Quote
 
 
 
 
castironpi@gmail.com
Guest
Posts: n/a
 
      03-09-2008
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
'''
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      03-09-2008

"Troels Thomsen" <"nej tak..."@bag.python.org> wrote in message
news:47d45625$0$15876$(E-Mail Removed) k...
| 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-essen...-in-funcs.html
apply disappears in 3.0

tjr



 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      03-09-2008
"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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
functools.wraps and help() Ulrich Eckhardt Python 1 06-02-2010 12:47 PM
unittest.TestCase and functools.partial don't seem to mix Joel Smith Python 1 10-06-2009 10:43 AM
functools's partial and update_wrapper does not work together piotr.findeisen@gmail.com Python 2 08-21-2008 08:45 PM
functools possibilities castironpi@gmail.com Python 3 02-03-2008 12:10 PM
itertools, functools, file enhancement ideas Paul Rubin Python 8 04-11-2007 04:26 AM



Advertisments