Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

input_prefilter hook not called #4709

Closed
vharitonsky opened this issue Dec 17, 2013 · 9 comments
Closed

input_prefilter hook not called #4709

vharitonsky opened this issue Dec 17, 2013 · 9 comments
Assignees
Milestone

Comments

@vharitonsky
Copy link

There is an input_prefilter hook in IPython documentation, but judging from the source it is not called anywhere.

@Carreau
Copy link
Member

Carreau commented Dec 17, 2013

For the record, I think this came from http://stackoverflow.com/questions/20633985/ipython-input-prefilter/20634792#20634792 as the delta of time between this issues and the SO question is 7 minutes.

@vharitonsky
Copy link
Author

Sorry, I am in despair.

@Carreau
Copy link
Member

Carreau commented Dec 17, 2013

No problem, I'm not an expert on this part of the code and most of the other core dev are asleep at this time, they should be available in a few hours. You can also pre-describe exactly what you want to do it might help !

@vharitonsky
Copy link
Author

I am writing a profiling extension for ipython which will measure resource usage of each executed line.
I imagine this kind of workflow:

In [1]: %profile
Profiling is enabled

In [2]:a = [a**a for a in range(100)]
CPU: 100ms MEM: +200 Mb

In [3]:%stats.cpu
Out[3]:100ms

I am using pre_prompt_hook and pre_run_code_hook in an object to achieve this. But the trick is NOT measuring anything when you call methods and attributes in %stats object which holds the profiling data. So I need some kind of a hook which runs BEFORE the pre_run_code_hook and gives me the ability to temporary disable profiling when i call %stats magic.

It's not very clear how to add custom input transformer, as the page only describes how to declare one, maybe I am missing something ?

@takluyver
Copy link
Member

I guess you're talking about this page? http://ipython.org/ipython-doc/stable/config/inputtransforms.html

Looking back at it, I agree it's not very clear how to add an input transformer. The closest it gets to describing that is this sentence: "An InteractiveShell instance actually has two IPythonInputSplitter instances, as the attributes input_splitter, to tell when a block of input is complete, and input_transformer_manager, to transform complete cells. If you add a transformer, you should make sure that it gets added to both."

From an extension, that would look something like this:

def load_ipython_extension(ip):
    ip.input_splitter.logical_line_transforms.append(my_transformer())
    ip.input_transformer_manager.logical_line_transforms.append(my_transformer())

Note that you have to decide which of physical_line_transforms, logical_line_transforms and python_line_transforms your transformations act on:

  • physical transforms get a line each time the user presses enter (e.g. stripping prompts)
  • logical transforms get lines with backslash continuations stitched together (e.g. transforming line magics)
  • Python transforms get multi-line strings, lists, etc. assembled into blocks

You can also define AST transforms, which can inspect and modify the parsed Python code before it is executed.

@ghost ghost assigned takluyver Dec 17, 2013
@vharitonsky
Copy link
Author

That's exactly what I need. Thanks alot.

That also gives me the idea that maybe input_prefilter hook can be also implemented as a transformer. I mean inside IPython it will look as just another physical line transformer. This could provide a backward compatibility in a very clean way.

@takluyver
Copy link
Member

If no-one has complained until now, there's probably no-one using the hook, so we might just get rid of it to keep things simple.

@vharitonsky
Copy link
Author

It's always exciting to be the first :) So, should I close the issue then ?

@takluyver
Copy link
Member

I'll improve the docs a bit and close it from the commit.

takluyver added a commit to takluyver/ipython that referenced this issue Dec 18, 2013
takluyver added a commit to takluyver/ipython that referenced this issue Dec 18, 2013
Our code already doesn't call this hook (as pointed out in ipythongh-4709), and
probably hasn't for some time, so it seems ~no-one is relying on it. We
have much more powerful APIs to do the same thing.
@minrk minrk closed this as completed in 6cac22f Dec 18, 2013
minrk added a commit that referenced this issue Dec 27, 2013
Various docs fixes

* Tweak exclude patterns, which were accidentally excluding qt.console.frontend_widget
* Added example of adding input transformers (#4709)
* Tidied up and added some module docstrings, now that we're using the first line in the API index
* Update the inputsplitter module docstring, which was rather out of date.
* Fix some header levels introduced in #4265
* pyreadline is apparently no longer mandatory on Windows (#4544)
jdfreder pushed a commit to jdfreder/ipython that referenced this issue Jan 2, 2014
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014
Our code already doesn't call this hook (as pointed out in ipythongh-4709), and
probably hasn't for some time, so it seems ~no-one is relying on it. We
have much more powerful APIs to do the same thing.
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014
Various docs fixes

* Tweak exclude patterns, which were accidentally excluding qt.console.frontend_widget
* Added example of adding input transformers (ipython#4709)
* Tidied up and added some module docstrings, now that we're using the first line in the API index
* Update the inputsplitter module docstring, which was rather out of date.
* Fix some header levels introduced in ipython#4265
* pyreadline is apparently no longer mandatory on Windows (ipython#4544)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants