1.5.2 and functools or similar

Discussion in 'Python' started by Troels Thomsen, Mar 9, 2008.

  1. 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
     
    Troels Thomsen, Mar 9, 2008
    #1
    1. Advertising

  2. Troels Thomsen

    Guest

    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
    '''
     
    , Mar 9, 2008
    #2
    1. Advertising

  3. Troels Thomsen

    Terry Reedy Guest

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

    tjr
     
    Terry Reedy, Mar 9, 2008
    #3
  4. Troels Thomsen

    Paul Rubin Guest

    "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)
     
    Paul Rubin, Mar 9, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    454
  2. Joel Smith
    Replies:
    1
    Views:
    486
    Peter Otten
    Oct 6, 2009
  3. Ulrich Eckhardt

    functools.wraps and help()

    Ulrich Eckhardt, Jun 2, 2010, in forum: Python
    Replies:
    1
    Views:
    382
    Michele Simionato
    Jun 2, 2010
  4. Mohsen Pahlevanzadeh

    functools and objective usage

    Mohsen Pahlevanzadeh, Oct 22, 2013, in forum: Python
    Replies:
    1
    Views:
    95
    Piet van Oostrum
    Oct 23, 2013
  5. MRAB
    Replies:
    0
    Views:
    101
Loading...

Share This Page