码迷,mamicode.com
首页 > 编程语言 > 详细

1001种玩法 | Python Prompt Toolkit:构建强大交互式命令行的 Python 工具库

时间:2017-06-02 18:59:47      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:psi   例子   oql   pen   构建   open   emacs   list   alt   

技术分享

Python Prompt Toolkit:构建强大交互式命令行的 Python 工具库

prompt_toolkit 是一个用于构建强大交互式命令行的 Python 工具库。

 

你是不是在找交互式的 Python shell 工具 ptpython 呢?我们把 ptpython 的源码转移到了一个独立的仓库。如此一来,我们确信  prompt_toolkit 库不会被其他 ptpython 东西“污染”,并且 ptpython 也可以独立开发。现在必须用下面这个命令安装 ptpython :

 

pip install ptpython

 

 

技术分享

prompt-toolkit 的特性features

 

prompt_toolkit 可用作 GNU readline 的替代品,但其实比后者还好得多。

部分特性:

  • 纯 Python;

  • 输入时支持语法高亮;(有 Pygments 词法分析器)

  • 支持多行输入编辑;

  • 高级代码补全;

  • 支持  Emacs 和 Vi 热键绑定;(和 readline 类似)

  • 支持反向和正向的增量搜索;

  • 支持 Python 2.6  到 3.4 之间的版本;

  • 支持 Unicode 双宽字符;(比如:中文输入)

  • 选择文本复制/粘贴 (Both Emacs and Vi style.)

  • 多输入缓存

  • 轻量级,只依赖于 Pygments、six 和 wcwidth;

  • Code written with love.

  • 支持 Linux、OS X、OpenBSD 和 Windows 系统;

 

安装

pip install prompt-toolkit

 

getting started

先来个简单的示例:

 

 

from prompt_toolkit.shortcuts import get_input

 

if __name__ == ‘__main__‘:

    answer = get_input(‘Give me some input: ‘)

    print(‘You said: %s‘ % answer)

 

复杂的示例,查看 examples 目录吧。所选示例都是直杆一件事的。不要怕看源码哦。 get_input 函数的实现,是个不错的开始。

注意:在 Python 2 下,你需要往上述例子添加 from __future__ import unicode_literals 。所有字符会看作是 unicode 字符。

 

 

使用 prompt-toolkit 的开源项目

 

  • ptpython: Python REPL

  • ptpdb: Python 调试器 (pdb 替代品)

  • pgcli: Postgres 客户端.

  • mycli: MySql 客户端.

  • pyvim: 纯 Python 实现的 Vim 克隆版

  • wharfee: Docker 命令行工具

  • 新智云官网  www.enncloud.cn

 

技术分享

技术分享

1001种玩法 | Python Prompt Toolkit:构建强大交互式命令行的 Python 工具库

标签:psi   例子   oql   pen   构建   open   emacs   list   alt   

原文地址:http://www.cnblogs.com/enncloudcn/p/6934705.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!