How to program test(expr) ?

Discussion in 'Python' started by Franck Ditter, Aug 29, 2012.

  1. Hi !
    I use Python 3.2.3 + Idle.
    Is it possible to program test(e) which takes
    an expression e and whose execution produces
    at the toplevel an echo of e and the effects
    and result of its evaluation ?

    # file foo.py
    def foo(x) :
    print('x =',x)
    return x+1

    test(foo(5))

    # RUN !

    # produces at the toplevel :
    ? foo(5)
    x = 5
    --> 6

    I know I could put the expression e within a string, but
    is it possible to avoid the string, like a Lisp macro ?

    Thanks.

    franck
     
    Franck Ditter, Aug 29, 2012
    #1
    1. Advertising

  2. Franck Ditter

    Terry Reedy Guest

    On 8/29/2012 11:04 AM, Franck Ditter wrote:

    > I use Python 3.2.3 + Idle.
    > Is it possible to program test(e) which takes
    > an expression e and whose execution produces
    > at the toplevel an echo of e and the effects
    > and result of its evaluation ?


    No, not as Python is delivered.

    > # file foo.py
    > def foo(x) :
    > print('x =',x)
    > return x+1
    >
    > test(foo(5))
    >
    > # RUN !
    >
    > # produces at the toplevel :
    > ? foo(5)
    > x = 5
    > --> 6
    >
    > I know I could put the expression e within a string, but
    > is it possible to avoid the string, like a Lisp macro ?


    It might be possible to write an IDLE extension that would 'process'
    interactive input looking for (untested) re pattern something like
    'test\((.*)\)'. Given a match, it prints the captured .* part and passes
    it on to the Python interpreter, and prefixes output with '-->'.
    (I have not yet looked at how to write extensions.)



    --
    Terry Jan Reedy
     
    Terry Reedy, Aug 29, 2012
    #2
    1. Advertising

  3. Am 29.08.2012 17:04, schrieb Franck Ditter:
    > I use Python 3.2.3 + Idle.
    > Is it possible to program test(e) which takes
    > an expression e and whose execution produces
    > at the toplevel an echo of e and the effects
    > and result of its evaluation ?


    Yes, the key to this is using a lambda expression.


    > # file foo.py
    > def foo(x) :
    > print('x =',x)
    > return x+1
    >
    > test(foo(5))


    def test(exp):
    global print
    print_saved = print
    print = my_print
    res = exp()
    print = print_saved
    return res

    test(lambda: foo(5))


    The idea is to run the callable expression inside a modified
    environment, in the sketch above it intercepts the calles to print()
    using a separate my_print() function. Note that the calling syntax is
    slightly different than the one you would have wanted, don't know if
    that is important.

    Things I'll leave to you:
    - exception handling
    - exception forwarding
    - intercepting other environment accesses
    - putting all that into a context manager :)


    Good luck!


    Uli
     
    Ulrich Eckhardt, Aug 30, 2012
    #3
    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:
    1
    Views:
    356
    intrader
    Sep 26, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    342
    Paul Rubin
    Jul 14, 2005
  3. George Young
    Replies:
    7
    Views:
    274
    David Isaac
    Oct 4, 2006
  4. Replies:
    3
    Views:
    335
    castironpi
    May 20, 2008
  5. Good Night Moon
    Replies:
    9
    Views:
    316
    Rick DeNatale
    Jul 25, 2007
Loading...

Share This Page