如何code自动完成在Python? [英] How to code autocompletion in python?

查看:115
本文介绍了如何code自动完成在Python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想美元的Linux端子C $ C自动完成。在code应该工作如下:

I'd like to code autocompletion in Linux terminal. The code should work as follows.

它有一个字符串列表(例如你好,喜,你好吗,再见,伟大,...)。

It has a list of strings (e.g. "hello, "hi", "how are you", "goodbye", "great", ...).

在终端的用户将开始打字,当有一些匹配的可能性,他得到了可能的字符串的提示,从中他可以选择(类似于在的 vim编辑器谷歌增量搜索)。

In terminal the user will start typing and when there is some match possibility, he gets the hint for possible strings, from which he can choose (similarly as in vim editor or google incremental search).

例如。他开始输入H,他就会提示

e.g. he starts typing "h" and he gets the hint

HELLO

_我

_嗷嗷你

和更好的是,如果它不仅会从一开始,但是从字符串任意部分完成的话。

And better yet would be if it would complete words not only from the beginning but from arbitrary part of the string.

感谢您的提醒。

推荐答案

(我知道这不正是你问什么,但)如果你感到高兴的自动完成/建议出现在<大骨节病>标签(在许多shell中使用),那么你可以迅速获得和使用运行的readline 模块。

(I'm aware this isn't exactly what you're asking for, but) If you're happy with the auto-completion/suggestions appearing on TAB (as used in many shells), then you can quickly get up and running using the readline module.

下面是一个基于道格乐门PyM​​OTW上的ReadLine 的书面记录。

Here's a quick example based on Doug Hellmann's PyMOTW writeup on readline.

import readline

class MyCompleter(object):  # Custom completer

    def __init__(self, options):
        self.options = sorted(options)

    def complete(self, text, state):
        if state == 0:  # on first trigger, build possible matches
            if text:  # cache matches (entries that start with entered text)
                self.matches = [s for s in self.options 
                                    if s and s.startswith(text)]
            else:  # no text entered, all matches possible
                self.matches = self.options[:]

        # return match indexed by state
        try: 
            return self.matches[state]
        except IndexError:
            return None

completer = MyCompleter(["hello", "hi", "how are you", "goodbye", "great"])
readline.set_completer(completer.complete)
readline.parse_and_bind('tab: complete')

input = raw_input("Input: ")
print "You entered", input

这将导致以下行为(&LT;标签&gt; 重新presenting一个tab键感pressed):

This results in the following behaviour (<TAB> representing a the tab key being pressed):

Input: <TAB><TAB>
goodbye      great        hello        hi           how are you

Input: h<TAB><TAB>
hello        hi           how are you

Input: ho<TAB>ow are you

在最后一行(<大骨节病> ^ h <大骨节病> 0 <大骨节病>标签进入),只有一个可能的匹配和全句你好吗是自动完成的。

In the last line (HOTAB entered), there is only one possible match and the whole sentence "how are you" is auto completed.

检查出更多的信息链接的文章上的readline

Check out the linked articles for more information on readline.

而更好的是,如果它能够完成的话,不仅从开始...结束从字符串任意一部分。

"And better yet would be if it would complete words not only from the beginning ... completion from arbitrary part of the string."

这可以通过简单地修改在完成试验功能的匹配标准,即可以实现。来自:

This can be achieved by simply modifying the match criteria in the completer function, ie. from:

self.matches = [s for s in self.options 
                   if s and s.startswith(text)]

喜欢的东西:

self.matches = [s for s in self.options 
                   if text in s]

这会给你以下行为:

Input: <TAB><TAB>
goodbye      great        hello        hi           how are you

Input: o<TAB><TAB>
goodbye      hello        how are you


更新:使用历史缓冲区(如在评论中提到)

一个简单的方法来创建滚动/检索伪菜单的关键字加载到历史缓冲区。然后,您将能够使用上/下箭头键以及使用过的条目滚动<大骨节病>控制 + <大骨节病>研究执行反向搜索。

要尝试了这一点,进行以下更改:

To try this out, make the following changes:

keywords = ["hello", "hi", "how are you", "goodbye", "great"]
completer = MyCompleter(keywords)
readline.set_completer(completer.complete)
readline.parse_and_bind('tab: complete')
for kw in keywords:
    readline.add_history(kw)

input = raw_input("Input: ")
print "You entered", input

当您运行该脚本,尝试键入<大骨节病>控制 + <大骨节病>研究后跟<大骨节病> A 。这将返回一个包含A的第一场比赛。输入<大骨节病>控制 + <大骨节病>研究再次为下一场比赛。要选择一个条目,preSS <大骨节病> ENTER

When you run the script, try typing Ctrl+r followed by a. That will return the first match that contains "a". Enter Ctrl+r again for the next match. To select an entry, press ENTER.

也可以尝试使用UP / DOWN键通过关键字来滚动。

Also try using the UP/DOWN keys to scroll through the keywords.

这篇关于如何code自动完成在Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆