ºìÁªLinuxÃÅ»§
Linux°ïÖú

ÈçºÎ¹¹½¨ÓÅÐãµÄÃüÁîÐÐÓû§½çÃæµÄ Python ¿â

·¢²¼Ê±¼ä:2017-11-08 13:04:06À´Ô´:ºìÁª×÷Õß:Ronny
ÔÚÕâ¸ö·ÖΪÁ½ÆªµÄ¹ØÓÚ¾ßÓоø¼ÑÃüÁîÐнçÃæµÄÖն˳ÌÐòµÄϵÁÐÎÄÕµĵڶþƪ½Ì³ÌÖУ¬ÎÒÃǽ«ÌÖÂÛ Prompt¡¢Toolkit¡¢Click¡¢Pygments ºÍ Fuzzy Finder ¡£
ÈçºÎ¹¹½¨ÓÅÐãµÄÃüÁîÐÐÓû§½çÃæµÄ Python ¿âÈçºÎ¹¹½¨ÓÅÐãµÄÃüÁîÐÐÓû§½çÃæµÄ Python ¿â

ÕâÊÇÎÒµÄÒ»¸ö·ÖΪÁ½ÆªµÄ¹ØÓÚ¾ßÓоø¼ÑÃüÁîÐнçÃæµÄÖն˳ÌÐòµÄϵÁÐÎÄÕµĵڶþƪ½Ì³Ì¡£ÔÚµÚһƪÎÄÕÂÖУ¬ÎÒÃÇÌÖÂÛÁËһЩÄܹ»Ê¹ÃüÁîÐÐÓ¦ÓÃÓÃÆðÀ´ÁîÈ˸е½ÓäÔõÄÌØÐÔ¡£ÔÚµÚ¶þƪÎÄÕÂÖУ¬ÎÒÃÇÀ´¿´¿´ÈçºÎÓà Python µÄһЩ¿âÀ´ÊµÏÖÕâЩÌØÐÔ¡£

ÎÒ´òËãÓÃÉÙÓÚ 20 ÐÐ Python ´úÂëÀ´ÊµÏÖ¡£ÈÃÎÒÃÇ¿ªÊ¼°É¡£

Python Prompt Toolkit
ÎÒÏ°¹ßÓÚ°ÑÕâ¸ö¿â³ÆΪÃüÁîÐÐÓ¦ÓõÄÈðÊ¿¾üµ¶£¬Ëü¿ÉÒÔ×÷Ϊreadline¡¢cursesµÈµÄÌæ´úÆ·¡£ÈÃÎÒÃÇÊ×ÏÈ°²×°Õâ¸ö¿â£¬È»ºó¿ªÊ¼¸Ã½Ì³Ì£º

pip install prompt_toolkit
ÎÒÃÇÒÔÒ»¸ö¼òµ¥µÄ REPL £¨LCTT Òë×¢£ºREPL ---- Read-Eval-Print Loop£¬½»»¥Ê½¿ª·¢»·¾³£©¿ªÊ¼¡£Ò»¸öµäÐ굀 REPL »á½ÓÊÕÓû§µÄÊäÈ룬½øÐÐÒ»¸ö²Ù×÷£¬È»ºóÊä³ö½á¹û¡£±ÈÈçÔÚÎÒÃǵÄÀý×ÓÖУ¬ÎÒÃǽ«ÒªÊµÏÖÒ»¸ö¾ßÓÐ ¡°»ØÏÔ¡± ¹¦ÄÜµÄ REPL ¡£Ëü½ö½öÊÇÔ­Ñù´òÓ¡³öÓû§µÄÊäÈ룺

REPL
from prompt_toolkit import prompt

while 1:
user_input = prompt('>')
print(user_input)
Õâ¾ÍÊÇʵÏÖ REPL µÄÈ«²¿´úÂë¡£Ëü¿ÉÒÔ¶ÁÈ¡Óû§µÄÊäÈ룬Ȼºó´òÓ¡³öÓû§µÄÊäÈëÄÚÈÝ¡£ÔÚÕâ¶Î´úÂëÖÐʹÓõÄpromptº¯ÊýÀ´×Ô prompt_toolkit¿â£¬ËüÊÇreadline¿âµÄÒ»¸öÌæ´úÆ·¡£

ÃüÁîÀúÊ·
ΪÁËÔöÇ¿ÎÒÃÇµÄ REPL µÄ¹¦ÄÜ£¬ÎÒÃÇ¿ÉÒÔÌí¼ÓÃüÁîÀúÊ·£º

from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory

while 1:
user_input = prompt('>',
history=FileHistory('history.txt'),
)
print(user_input)
ÎÒÃǸոոø REPL Ìí¼ÓÁ˳־õÄÃüÁîÀúÊ·¡£ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔʹÓÃÉÏ/ϼýÍ·À´ä¯ÀÀÃüÁîÀúÊ·£¬²¢Ê¹ÓÃCtrl-RÀ´ËÑË÷ÃüÁîÀúÊ·¡£ËüÂú×ãÁËÃüÁîÐеĻù±¾×¼Ôò¡£

×Ô¶¯ÍƼö
ÔÚµÚһƪ½Ì³ÌÖУ¬ÎÒ½²µ½µÄÒ»¸ö¿É·¢ÏÖÐÔ¼¼ÇÉÊÇ×Ô¶¯ÍƼöÀúÊ·ÃüÁî¡££¨ÎÒÊÇÊ×ÏÈÔÚ fish shell Öп´µ½µÄÕâÒ»ÌØÐÔ£©ÈÃÎÒÃÇ°ÑÕâÒ»ÌØÐÔ¼ÓÈëµ½ÎÒÃÇµÄ REPL ÖУº

from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory

while 1:
user_input = prompt('>',
history=FileHistory('history.txt'),
auto_suggest=AutoSuggestFromHistory(),
)
print(user_input)
ÎÒÃÇÖ»ÐèÒª¸øprompt() API µ÷ÓÃÌí¼ÓÒ»¸öеIJÎÊý¡£ÏÖÔÚ£¬ÎÒÃÇÓÐÁËÒ»¸ö¾ßÓÐfish shell·ç¸ñµÄ REPL£¬Ëü¿ÉÒÔ×Ô¶¯ÍƼöÀúÊ·ÃüÁî¡£

×Ô¶¯²¹È«
ÏÖÔÚ£¬ÈÃÎÒÃÇͨ¹ý×Ô¶¯²¹È«À´¼ÓÇ¿ Tab ²¹È«¡£ËüÄܹ»ÔÚÓû§¿ªÊ¼ÊäÈëµÄʱºòµ¯³ö¿ÉÄܵÄÃüÁîÍƼö¡£

REPL ÈçºÎÀ´½øÐÐÍƼöÄØ£¿ÎÒÃÇʹÓÃÒ»¸ö×ÖµäÀ´½øÐпÉÄÜÏîµÄÍƼö¡£

±ÈÈç˵ÎÒÃÇʵÏÖÒ»¸öÕë¶Ô SQL µÄ REPL ¡£ÎÒÃÇ¿ÉÒÔ°Ñ SQL ¹Ø¼ü×Ö´æµ½×Ô¶¯²¹È«×ÖµäÀïÃæ¡£ÈÃÎÒÃÇ¿´Ò»¿´ÕâÊÇÈçºÎʵÏֵģº

from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.contrib.completers import WordCompleter

SQLCompleter = WordCompleter(['select', 'from', 'insert', 'update', 'delete', 'drop'],
ignore_case=True)

while 1:
user_input = prompt('SQL>',
history=FileHistory('history.txt'),
auto_suggest=AutoSuggestFromHistory(),
completer=SQLCompleter,
)
print(user_input)
ÔÙ´Î˵Ã÷£¬ÎÒÃÇÖ»ÊǼòµ¥µÄʹÓÃÁË prompt-toolkit ÄÚ½¨µÄÒ»¸ö½Ð×ö WordCompleter µÄ²¹È«ÌØÐÔ£¬ËüÄܹ»°ÑÓû§ÊäÈëºÍ¿ÉÄÜÍƼöµÄ×Öµä½øÐÐÆ¥Å䣬ȻºóÌṩһ¸öÁÐ±í¡£

ÏÖÔÚ£¬ÎÒÃÇÓÐÁËÒ»¸öÄܹ»×Ô¶¯²¹È«¡¢fish shell ·ç¸ñµÄÀúÊ·ÃüÁîÍƼöÒÔ¼°ÉÏ/ÏÂä¯ÀÀÀúÊ·µÄ REPL ¡£ÊµÏÖÕâЩÌØÐÔÖ»ÓÃÁ˲»µ½ 10 ÐеÄʵ¼Ê´úÂë¡£

Click
ClickÊÇÒ»¸öÃüÁîÐд´½¨¹¤¾ß°ü£¬Ê¹ÓÃËüÄܹ»¸üÈÝÒ×µÄΪ³ÌÐò½âÎöÃüÁîÐÐÑ¡ÏîµÄ²ÎÊýºÍ³£Á¿¡£ÔÚÕâ¶ùÎÒÃDz»ÌÖÂÛÈçºÎʹÓÃClickÀ´×÷Ϊ²ÎÊý½âÎöÆ÷¡£Ïà·´£¬ÎÒÃǽ«»á¿´¿´Click´øÓеÄһЩ¹¦ÄÜ¡£

°²×°Click£º

pip install click
·ÖÒ³Æ÷ÊÇ Unix ϵͳÉϵÄʵÓù¤¾ß£¬ËüÃÇÄܹ»Ò»´ÎÒ»Ò³µØÏÔʾºÜ³¤µÄÊä³ö¡£·ÖÒ³Æ÷µÄһЩÀý×Ó°üÀ¨less¡¢more¡¢mostµÈ¡£Í¨¹ý·ÖÒ³Æ÷À´ÏÔʾһ¸öÃüÁîµÄÊä³ö²»½ö½öÊÇÒ»¸öÓѺõÄÉè¼Æ£¬Í¬Ê±Ò²ÊDZØÒªµÄ¡£

ÈÃÎÒÃǽøÒ»²½¸Ä½øÇ°ÃæµÄÀý×Ó¡£ÎÒÃDz»ÔÙʹÓÃĬÈÏprint()Óï¾ä£¬È¡¶ø´úÖ®µÄÊÇclick.echo_via_pager()¡£Ëü½«»á°ÑÊä³öͨ¹ý·ÖÒ³Æ÷·¢Ë͵½±ê×¼Êä³ö¡£ÕâÊÇƽ̨Î޹صģ¬Òò´ËÔÚ Unix ϵͳ»ò Windows ϵͳÉϾùÄܹ¤×÷¡£Èç¹û±ØÒªµÄ»°£¬click_via_pager »á³¢ÊÔʹÓÃÒ»¸öºÏÊʵÄĬÈÏ·ÖÒ³Æ÷À´Êä³ö£¬´Ó¶øÄܹ»ÏÔʾ´úÂë¸ßÁÁ¡£

from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.contrib.completers import WordCompleter
import click

SQLCompleter = WordCompleter(['select', 'from', 'insert', 'update', 'delete', 'drop'],
ignore_case=True)

while 1:
user_input = prompt(u'SQL>',
history=FileHistory('history.txt'),
auto_suggest=AutoSuggestFromHistory(),
completer=SQLCompleter,
)
click.echo_via_pager(user_input)
±à¼­Æ÷
ÔÚÎÒÇ°ÃæµÄÎÄÕÂÖÐÒ»¸öÖµµÃÒ»ÌáµÄϸ½ÚÊÇ£¬µ±ÃüÁî¹ýÓÚ¸´ÔÓµÄʱºò½øÈë±à¼­Æ÷À´±à¼­¡£Click ÓÐÒ»¸ö¼òµ¥µÄ API Äܹ»´ò¿ª±à¼­Æ÷£¬È»ºó°ÑÔڱ༭Æ÷ÖÐÊäÈëµÄÎı¾·µ»Ø¸øÓ¦Óá£

import click
message = click.edit()
Fuzzy Finder
Fuzzy Finder ÊÇÒ»ÖÖͨ¹ýÉÙÁ¿ÊäÈëÀ´ÎªÓû§¼õÉÙÍƼöµÄ·½·¨¡£ÐÒÔ˵ÄÊÇ£¬ÓÐÒ»¸ö¿â¿ÉÒÔʵÏÖ Fuzzy Finder ¡£ÈÃÎÒÃÇÊ×ÏÈ°²×°Õâ¸ö¿â£º

pip install fuzzyfinder
Fuzzy FinderµÄ API ºÜ¼òµ¥¡£Óû§ÏòËü´«µÝ²¿·Ö×Ö·û´®ºÍһϵÁпÉÄܵÄÑ¡Ôñ£¬È»ºó£¬Fuzzy Finder½«»á·µ»ØÒ»¸öÓ벿·Ö×Ö·û´®Æ¥ÅäµÄÁÐ±í£¬ÕâÒ»ÁбíÊÇͨ¹ýÄ£ºýËã·¨¸ù¾ÝÏà¹ØÐÔÅÅÐòµÃ³öµÄ¡£±ÈÈ磺

>>> from fuzzyfinder import fuzzyfinder

>>> suggestions = fuzzyfinder('abc', ['abcd', 'defabca', 'aagbec', 'xyz', 'qux'])

>>> list(suggestions)
['abcd', 'defabca', 'aagbec']
ÏÖÔÚÎÒÃÇÓÐÁËfuzzyfinder>£¬ÈÃÎÒÃÇ°ÑËü¼ÓÈëµ½ÎÒÃÇµÄ SQL REPL ÖС£·½·¨ÊÇÎÒÃÇ×Ô¶¨ÒåÒ»¸öcompleter¶ø²»ÊÇʹÓÃÀ´×Ôprompt-toolkit¿âµÄWordCompleter¡£±ÈÈ磺

from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.completion import Completer, Completion
import click
from fuzzyfinder import fuzzyfinder

SQLKeywords = ['select', 'from', 'insert', 'update', 'delete', 'drop']

class SQLCompleter(Completer):
def get_completions(self, document, complete_event):
word_before_cursor = document.get_word_before_cursor(WORD=True)
matches = fuzzyfinder(word_before_cursor, SQLKeywords)
for m in matches:
yield Completion(m, start_position=-len(word_before_cursor))

while 1:
user_input = prompt(u'SQL>',
history=FileHistory('history.txt'),
auto_suggest=AutoSuggestFromHistory(),
completer=SQLCompleter(),
)
click.echo_via_pager(user_input)
Pygments
ÏÖÔÚ£¬ÈÃÎÒÃǸøÓû§ÊäÈëÌí¼ÓÓï·¨¸ßÁÁ¡£ÎÒÃÇÕýÔڴһ¸ö SQL REPL£¬Èç¹û¾ßÓвÊÉ«¸ßÁÁµÄ SQL Óï¾ä£¬Õâ»áºÜ°ô¡£

PygmentsÊÇÒ»¸öÌṩÓï·¨¸ßÁÁµÄ¿â£¬ÄÚ½¨Ö§³Ö³¬¹ý 300 ÖÖÓïÑÔ¡£Ìí¼ÓÓï·¨¸ßÁÁÄܹ»Ê¹Ó¦ÓñäµÃ²ÊÉ«»¯£¬´Ó¶øÄܹ»°ïÖúÓû§ÔÚÖ´ÐгÌÐòÇ°·¢ÏÖ SQL ÖдæÔڵĴíÎ󣬱ÈÈçƴд´íÎó¡¢ÒýºÅ²»Æ¥Åä»òÀ¨ºÅ²»Æ¥Åä¡£

Ê×ÏÈ£¬°²×°Pygments£º

pip install pygments
ÈÃÎÒÃÇʹÓÃPygmentsÀ´Îª SQL REPL Ìí¼ÓÑÕÉ«£º

from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.completion import Completer, Completion
import click
from fuzzyfinder import fuzzyfinder
from pygments.lexers.sql import SqlLexer

SQLKeywords = ['select', 'from', 'insert', 'update', 'delete', 'drop']

class SQLCompleter(Completer):
def get_completions(self, document, complete_event):
word_before_cursor = document.get_word_before_cursor(WORD=True)
matches = fuzzyfinder(word_before_cursor, SQLKeywords)
for m in matches:
yield Completion(m, start_position=-len(word_before_cursor))

while 1:
user_input = prompt(u'SQL>',
history=FileHistory('history.txt'),
auto_suggest=AutoSuggestFromHistory(),
completer=SQLCompleter(),
lexer=SqlLexer,
)
click.echo_via_pager(user_input)
Prompt ToolkitÄܹ»ºÍPygmentsһͬºÜºÃµÄ¹¤×÷¡£ÎÒÃÇ°ÑPygmentsÌṩµÄSqlLexer¼ÓÈëµ½À´×Ôprompt-toolkitµÄpromptÖС£ÏÖÔÚ£¬ËùÓеÄÓû§ÊäÈ붼»á±»µ±×÷ SQL Óï¾ä£¬²¢½øÐÐÊʵ±×ÅÉ«¡£

½áÂÛ
ÎÒÃǵġ°ÂÃ;¡±Í¨¹ý´´½¨Ò»¸öÇ¿´óµÄ REPL ½áÊø£¬Õâ¸ö REPL ¾ßÓг£¼ûµÄ shell µÄÈ«²¿ÌØÐÔ£¬±ÈÈçÀúÊ·ÃüÁ¼üλ°ó¶¨£¬Óû§ÓѺÃÐÔ±ÈÈç×Ô¶¯²¹È«¡¢Ä£ºý²éÕÒ¡¢·ÖÒ³Æ÷Ö§³Ö¡¢±à¼­Æ÷Ö§³ÖºÍÓï·¨¸ßÁÁ¡£ÎÒÃǽöÓÃÉÙÓÚ 20 ÐÐ Python ´úÂë¾ÍʵÏÖÁËÕâ¸ö REPL ¡£

²»ÊǺܼòµ¥Âð£¿ÏÖÔÚ£¬ÄãûÓÐÀíÓɲ»»áдһ¸ö×Ô¼ºµÄÃüÁîÐÐÓ¦ÓÃÁË¡£
±¾ÎÄתÔØ×Ô£ºhttp://www.linuxprobe.com/interface-python-library.html

Ãâ·ÑÌṩ×îÐÂLinux¼¼Êõ½Ì³ÌÊé¼®£¬Îª¿ªÔ´¼¼Êõ°®ºÃÕßŬÁ¦×öµÃ¸ü¶à¸üºÃ£¬¿ªÔ´Õ¾µã£ºhttp://www.linuxprobe.com/
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 1 ÌõÆÀÂÛ

  1. Ronny ÓÚ 2017-11-08 13:04:12·¢±í:

    ÈçºÎ¹¹½¨ÓÅÐãµÄÃüÁîÐÐÓû§½çÃæµÄ Python ¿â