Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28066 > unrolled thread

How to program test(expr) ?

Started byFranck Ditter <franck@ditter.org>
First post2012-08-29 17:04 +0200
Last post2012-08-30 09:33 +0200
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  How to program test(expr) ? Franck Ditter <franck@ditter.org> - 2012-08-29 17:04 +0200
    Re: How to program test(expr) ? Terry Reedy <tjreedy@udel.edu> - 2012-08-29 17:35 -0400
    Re: How to program test(expr) ? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-08-30 09:33 +0200

#28066 — How to program test(expr) ?

FromFranck Ditter <franck@ditter.org>
Date2012-08-29 17:04 +0200
SubjectHow to program test(expr) ?
Message-ID<franck-E4DC28.17040529082012@news.free.fr>
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

[toc] | [next] | [standalone]


#28074

FromTerry Reedy <tjreedy@udel.edu>
Date2012-08-29 17:35 -0400
Message-ID<mailman.3944.1346276175.4697.python-list@python.org>
In reply to#28066
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

[toc] | [prev] | [next] | [standalone]


#28096

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2012-08-30 09:33 +0200
Message-ID<6kh3h9-p9o.ln1@satorlaser.homedns.org>
In reply to#28066
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web