top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Interactive interpreter hooks in python

0 votes
558 views

The sys module defines two hooks that are used in the interactive interpreter:

  • sys.displayhook(value) gets called with the result of evaluating the line when you press ENTER;

  • sys.excepthook(type, value, traceback) gets called with the details of the exception when your line raises an exception.

Is there a way to hook into the interactive interpreter *before* it is evaluated? That is, if I type "len([])" at the prompt and hit ENTER, I want a hook that runs before len([]) is evaluated to 0, so that I get the string "len([])".

posted Jun 3, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
 
Best answer

I dont know whether that is possible, but you could recreate the repl. This page seems to have good resources for that: http://stackoverflow.com/questions/1395913/python-drop-into-repl-read-eval-print-loop

A trace function could also work. See docs for sys.settrace. The source code for the python GOTO module is a good example of its usage.

answer Jun 3, 2013 by anonymous
0 votes

You will need to write your own REPL for this. Use the code.InteractiveConsole class:
http://docs.python.org/2/library/code

I recommend source-diving to see what you need to override, but I suspect you can just wrap around the runsource() method.

answer Jun 3, 2013 by anonymous
Similar Questions
–1 vote

R has the function edit() which allows the editing of the definition of a function. Does python have something similar so that users can edit python functions on the fly?

Ref:
https://www.rdocumentation.org/packages/utils/versions/3.4.3/topics/edit

+1 vote

I'm trying to work through Skienna's algorithms handbook, and note that the author often uses graphical representations of the diagrams to help understand (and even debug) the algorithms. I'd like to reproduce this in python.

How would you go about this? pyQt, pygame and pyglet immediately come to mind, but if I go that route the number of people that I can share my work with becomes quite limited, as compared to the portability of javascript projects.

I guess my question really is: has anyone had success creating an interactive graphical project in the browser using python?

...