Pythonインタラクティブシェルのbpythonで入力補完する

提供: Python入門
移動: 案内検索
スポンサーリンク

bpythonは、Pythonインタプリターのためのファンシーインターフェースです。Pythonのインタラクティブシェルとして使用するとシンタックスハイライトや入力補完などができて、便利です。

読み方

bpython
びーぱいそん

概要

bpythonの主な機能は、以下のとおりです。

  • インラインシンタックスハイライト
  • readline ライクな入力自動補完と候補表示
  • Python関数の期待される引数のリスト
  • 記憶と再評価のためのコードの最終行をポップする"Rewind" 機能
  • 入力したコードをpastebinに送信
  • 入力したコードをファイルに保存
  • 自動インデント
  • Python 3 のサポート

インストール

テンプレート:ports

実行例

% bpython

関数を入力保管する

p を押すと、pではじまる関数が表示されます。 タブを押すとシェルのように補完できます。

>>> p
┌──────────────────────────────────────────────────┐
│pass      pow(      print     print(    property( │
└──────────────────────────────────────────────────┘
 
 
 <C-r> Rewind  <C-s> Save  <F8> Pastebin  <F9> Pager  <F2> Show S

一回タブを押すと、最初の pass が選択されます。

>>> pass
┌──────────────────────────────────────────────────┐
│pass      pow(      print     print(    property( │
└──────────────────────────────────────────────────┘

関数の説明も表示できる

関数の引数の説明もしてくれます。

>>> for i in xrange(
┌──────────────────────────────────────────────────────────┐
│ xrange: (stop)                                           │
│ xrange(stop) -> xrange object                            │
│ xrange(start, stop[, step]) -> xrange object             │
│                                                          │
│ Like range(), but instead of returning a list, returns an│
│ object that                                              │
│ generates the numbers in the range on demand.  For       │
│ looping, this is                                         │
│ slightly faster than range() and more memory efficient.  │
│                                                          │
└──────────────────────────────────────────────────────────┘

importするモジュールも入力補完できる

>>> import s
┌──────────────────────────────────────────────────────────┐
│scapy         sched         select        sets            │
│setuptools    sgmllib       sha           shelve          │
│shlex         shutil        signal        site            │
│smtpd         smtpd2        smtplib       sndhdr          │
│socket        sphinx        sqlite3       sre             │
│sre_compile   sre_constants sre_parse     ssl             │
│stat          statvfs       string        stringold       │
│stringprep    strop         struct        subprocess      │
│sunau         sunaudio      symbol        symtable        │
│sys           sysconfig     syslog                        │
└──────────────────────────────────────────────────────────┘

モジュールの関数を補完する

モジュールの関数をこのように補完できます。

>>> import sys
>>> sys.g
┌──────────────────────────────────────────────────────────┐
│getcheckinterval      getdefaultencoding                  │
│getdlopenflags        getfilesystemencoding               │
│getprofile            getrecursionlimit                   │
│getrefcount           getsizeof                           │
│gettrace                                                  │
└──────────────────────────────────────────────────────────┘

ファイルパスも補完できる

タブを押していくと、パスが補完できます。

>>> import os
>>> os.listdir('/usr/src/bin/ls/ls.c
┌─────────────────────────────────────────────────────────────────┐
│ os.listdir: (path)                                              │
│ls.c     cmp.c    ls.h     extern.h Makefile ls.1     print.c    │
│util.c                                                           │
│ listdir(path) -> list_of_strings                                │
│                                                                 │
│ Return a list containing the names of the entries in the        │
│ directory.                                                      │
│                                                                 │
│     path: path of directory to list                             │
│                                                                 │
│ The list is in arbitrary order.  It does not include the special│
│ entries '.' and '..' even if they are present in the directory. │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

関連項目




スポンサーリンク