python控制台开发_从命令行在python交互式控制台中运行你的代码

Picon

picon runs your code in python interactive console from the command line. Besides other purposes, it can be easily integrated to your editor to create a live worksheet environment. This is useful to evaluate your code on the fly and show you the result right below the code within your editor:

b2ed9ee74fc6018ffc7d0b262d87eba3.gif

Usage

picon is implemented as a thin wrapper around code module to be used as a command line application. There are three running modes available in picon. Following example code is used for demonstration:

$ cat demo.py

'hello world'

x = 42

x

print x

y

total = 0

for i in range(1000):

if i % 3 == 0 or i % 5 == 0:

total += i

total

Default mode evaluates the code and only shows the output. This is similar to running your code with python but it uses interactive console semantics (see syntax section below):

$ picon demo.py

'hello world'

42

42

Traceback (most recent call last):

File "", line 1, in

NameError: name 'y' is not defined

233168

Live mode shows the code and the output as in a live session. This can be used to prepare programming notes or documentation:

$ picon demo.py -l

>>> 'hello world'

'hello world'

>>>

>>> x = 42

>>> x

42

>>> print x

42

>>>

>>> y

Traceback (most recent call last):

File "", line 1, in

NameError: name 'y' is not defined

>>>

>>> total = 0

>>> for i in range(1000):

... if i % 3 == 0 or i % 5 == 0:

... total += i

...

>>> total

233168

Append mode appends the output as comments below the code. A pipe character (|) is added to differentiate these comments from regular ones and strip them in consequent executions. This mode is designed to be integrated to editors to create a live worksheet environment:

$ picon demo.py -a

'hello world'

#|'hello world'

x = 42

x

#|42

print x

#|42

y

#|Traceback (most recent call last):

#| File "", line 1, in

#|NameError: name 'y' is not defined

total = 0

for i in range(1000):

if i % 3 == 0 or i % 5 == 0:

total += i

total

#|233168

Syntax

Syntax of python interactive console differs from regular python in two main aspects. First, return values are automatically shown without a print statement:

$ cat return.py

x = 42

x

print x

$ python return.py

42

$ picon return.py

42

42

Second, blocks are separated with blank newlines in addition to indentation:

$ cat block.py

if True:

print 'one'

print 'two'

$ python block.py

one

two

$ picon block.py

one

File "", line 1

print 'two'

^

IndentationError: unexpected indent

Having blank trailing spaces equal to the indentation works in interactive console but not in picon.

Installation

You can install picon as a python package using pip:

pip install picon

Or you can download it from github and put it somewhere in $PATH:

curl https://raw.githubusercontent.com/gokcehan/picon/master/picon/picon.py -o picon

chmod +x picon

sudo mv picon /usr/local/bin

Vim Integration

Simply running :%!picon -a evaluates the buffer content and puts the output as comments below the code. In order to keep the cursor position fixed and join undo operations to a single step you can use a command similar to the following:

command! Picon exe 'normal m`' | silent! undojoin | exe '%!picon -a' | exe 'normal ``'

You may want to assign this command to either BufWritePre event to run on save and/or CursorHold event to run on idle as follows (see also :h updatetime):

autocmd Filetype python autocmd BufWritePre Picon

autocmd Filetype python autocmd CursorHold Picon

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值