Download 1.x PDF - Read the Docs

Transcript
IPython Documentation, Release 1.2.1: An Afternoon Hack
line = (yield line)
leading_indent.look_in_string = True
Token-based transformers
There is an experimental framework that takes care of tokenizing and untokenizing lines of code. Define a function that accepts a list of tokens, and returns an iterable of output tokens, and decorate it with
TokenInputTransformer.wrap(). These should only be used in python_line_transforms.
6.6.2 AST transformations
After the code has been parsed as Python syntax, you can use Python’s powerful Abstract Syntax Tree tools to
modify it. Subclass ast.NodeTransformer, and add an instance to shell.ast_transformers.
This example wraps integer literals in an Integer class, which is useful for mathematical frameworks that
want to handle e.g. 1/3 as a precise fraction:
class IntegerWrapper(ast.NodeTransformer):
"""Wraps all integers in a call to Integer()"""
def visit_Num(self, node):
if isinstance(node.n, int):
return ast.Call(func=ast.Name(id='Integer', ctx=ast.Load()),
args=[node], keywords=[])
return node
6.7 Outdated configuration information that might still be useful
Warning: All of the information in this file is outdated. Until the new configuration system is better
documented, this material is being kept.
This section will help you set various things in your environment for your IPython sessions to be as efficient
as possible. All of IPython’s configuration information, along with several example files, is stored in a
directory named by default $HOME/.config/ipython if $HOME/.config exists (Linux), or $HOME/.ipython
as a secondary default. You can change this by defining the environment variable IPYTHONDIR, or at
runtime with the command line option -ipythondir.
If all goes well, the first time you run IPython it should automatically create a user copy of the config
directory for you, based on its builtin defaults. You can look at the files it creates to learn more about
configuring the system. The main file you will modify to configure IPython’s behavior is called ipythonrc
(with a .ini extension under Windows), included for reference here. This file is very commented and has
many variables you can change to suit your taste, you can find more details here. Here we discuss the basic
things you will want to make sure things are working properly from the beginning.
6.7. Outdated configuration information that might still be useful
365