##// END OF EJS Templates
Merge pull request #10391 from Carreau/redox...
Thomas Kluyver -
r23500:226bd07d merge
parent child Browse files
Show More
@@ -71,6 +71,39 b' The decorator returns a factory function which will produce instances of'
71 :class:`~IPython.core.inputtransformer.StatelessInputTransformer` using your
71 :class:`~IPython.core.inputtransformer.StatelessInputTransformer` using your
72 function.
72 function.
73
73
74 Transforming a full block
75 -------------------------
76
77 .. warning::
78
79 Transforming a full block at once will break the automatic detection of
80 whether a block of code is complete in interfaces relying on this
81 functionality, such as terminal IPython. You will need to use a
82 shortcut to force-execute your cells.
83
84 Transforming a full block of python code is possible by implementing a
85 :class:`~IPython.core.inputtransformer.Inputtransformer` and overwriting the
86 ``push`` and ``reset`` methods. The reset method should send the full block of
87 transformed text. As an example a transformer the reversed the lines from last
88 to first.
89
90 from IPython.core.inputtransformer import InputTransformer
91
92 class ReverseLineTransformer(InputTransformer):
93
94 def __init__(self):
95 self.acc = []
96
97 def push(self, line):
98 self.acc.append(line)
99 return None
100
101 def reset(self):
102 ret = '\n'.join(self.acc[::-1])
103 self.acc = []
104 return ret
105
106
74 Coroutine transformers
107 Coroutine transformers
75 ----------------------
108 ----------------------
76
109
@@ -79,9 +112,30 b' sent each line in turn, followed by ``None`` to reset it. It can yield lines, or'
79 ``None`` if it is accumulating text to yield at a later point. When reset, it
112 ``None`` if it is accumulating text to yield at a later point. When reset, it
80 should give up any code it has accumulated.
113 should give up any code it has accumulated.
81
114
115 You may use :meth:`CoroutineInputTransformer.wrap` to simplify the creation of
116 such a transformer.
117
118 Here is a simple :class:`CoroutineInputTransformer` that can be thought of
119 being the identity::
120
121 from IPython.core.inputtransformer import CoroutineInputTransformer
122
123 @CoroutineInputTransformer.wrap
124 def noop():
125 line = ''
126 while True:
127 line = (yield line)
128
129 ip = get_ipython()
130
131 ip.input_splitter.logical_line_transforms.append(noop())
132 ip.input_transformer_manager.logical_line_transforms.append(noop())
133
82 This code in IPython strips a constant amount of leading indentation from each
134 This code in IPython strips a constant amount of leading indentation from each
83 line in a cell::
135 line in a cell::
84
136
137 from IPython.core.inputtransformer import CoroutineInputTransformer
138
85 @CoroutineInputTransformer.wrap
139 @CoroutineInputTransformer.wrap
86 def leading_indent():
140 def leading_indent():
87 """Remove leading indentation.
141 """Remove leading indentation.
General Comments 0
You need to be logged in to leave comments. Login now