[IPython-User] Advice on using a prefilter

Aaron Meurer asmeurer@gmail....
Mon Aug 6 18:39:52 CDT 2012


I'm trying to write an input prefilter for SymPy that wraps integer
literals with Integer, so that things like 1/2 will produce
Rational(1, 2).  On the advice of minrk on IRC, I'm using something
like https://bitbucket.org/birkenfeld/ipython-physics/src/71b2d850da00/physics.py
(particularly the code at the very bottom).  I have some questions,
though:

- What is the purpose of the continue_prompt argument to transform()?

- How do I make this work for multi-line inputs.  So far, I have something like

In [1]: 1/2
Out[1]: 1/2

In [2]: def f():
   ...:     return 1/2
   ...:

In [3]: f()
Out[3]: 0.5

The transformer doesn't seem to be called at all if the input is more
than one line (it doesn't seem to matter if this is because it's a
block or just because of a line continuation).

- If the input is bad, it raises a SyntaxError, but on the transformed
expresion.  This is expected, but I'd like to avoid it for user
experience purposes. What is the best way to fall back to the original
input in the case of a SyntaxError?

- The code at https://bitbucket.org/birkenfeld/ipython-physics/src/71b2d850da00/physics.py
claims that the transformer object is supposed to subclass
PrefilterTransformer, but it doesn't work.  I also didn't get it to
work.  If I do, I get

Traceback (most recent call last):
  File "./bin/isympy", line 305, in <module>
    main()
  File "./bin/isympy", line 298, in main
    from sympy.utilities.exceptions import SymPyDeprecationWarning
  File "/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/__init__.py",
line 75, in <module>
    from interactive import init_session, init_printing
  File "/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/interactive/__init__.py",
line 4, in <module>
    from session import init_session
  File "/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/interactive/session.py",
line 129, in <module>
    int_transformer = IntTransformer()
  File "/Users/aaronmeurer/Documents/ipython/IPython/core/prefilter.py",
line 383, in __init__
    self.prefilter_manager.register_transformer(self)
AttributeError: 'NoneType' object has no attribute 'register_transformer'

Is this a bug?

If it would help, I can push up what I've done so far to GitHub.

Aaron Meurer


More information about the IPython-User mailing list