开发者

Interpreter that ignores leading >>> characters and ellipsis

开发者 https://www.devze.com 2022-12-16 04:53 出处:网络
I am studying some examples in a tutorial where there are a lot of leading >>> characters and ellipsis in the text. This makes it hard to cut and paste into the IPython interpreter since it doesn\'t l

I am studying some examples in a tutorial where there are a lot of leading >>> characters and ellipsis in the text. This makes it hard to cut and paste into the IPython interpreter since it doesn't like these strings.

Is there another interpreter I could use that will appropriately ignore and interpret these leading terms?

For example, I cannot paste the following directly into the interpreter:

>>> d = dict(x开发者_开发百科.__array_interface__)
>>> d['shape'] = (3, 2, 5)
>>> d['strides'] = (20, 20, 4)

>>> class Arr:
...     __array_interface__ = d
...     base = x


IPython can do this (look at the %paste magic command)


In any case, a way to clean up such stuff by python code:

import re
matcher= re.compile("(?m)^[.>]{3} ")
def cleanup(text):
    return matcher.sub('', text)

Example use:

>>> print (cleanup(""">>> d = dict(x.__array_interface__)
>>> d['shape'] = (3, 2, 5)
>>> d['strides'] = (20, 20, 4)

>>> class Arr:
...     __array_interface__ = d
...     base = x"""))
0

精彩评论

暂无评论...
验证码 换一张
取 消