Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3:select.select()中的readline equalent_Python_Python 3.x_Gnu_Readline - Fatal编程技术网

Python3:select.select()中的readline equalent

Python3:select.select()中的readline equalent,python,python-3.x,gnu,readline,Python,Python 3.x,Gnu,Readline,在Python脚本中,程序员可以导入readline,这将提供input()扩展功能(readline还有许多其他用途)。我想在脚本中使用select.select()而不是input(),因为我喜欢超时功能。但是,在导入readline时,我无法使用input()在readline中获得的功能。我所指的“扩展功能”的一个例子是,可以按向上键查看以前的输入,或者使用左右箭头键移动内联光标以更改输入 问题:如何使select.select()具有GNU readline功能?这可能吗 编辑:为了防

在Python脚本中,程序员可以导入readline,这将提供input()扩展功能(readline还有许多其他用途)。我想在脚本中使用select.select()而不是input(),因为我喜欢超时功能。但是,在导入readline时,我无法使用input()在readline中获得的功能。我所指的“扩展功能”的一个例子是,可以按向上键查看以前的输入,或者使用左右箭头键移动内联光标以更改输入

问题:如何使select.select()具有GNU readline功能?这可能吗


编辑:为了防止你们中的任何人对我试图实现的目标感到好奇,我制作了一个基于终端的聊天机器人(有点像Alicebot)。我希望机器人变得无聊,如果在设定的时间内没有收到任何输入,就做其他事情。()

您可以使用readline.set_pre_input_hook([function])机制来实现这一点

这里有一个例子,没有输入10秒后超时-未实现是一种在提供输入时禁用报警的机制

由于信号不能遍历线程,因此必须对线程执行不同的操作。但是,你得到了基本的想法

我为这段代码提前道歉,我在一家咖啡店用笔记本电脑把它拼凑起来。这是python2.7代码,但基本上应该与python3兼容-概念是重要的部分

我认为如果希望每一行输入都有一个超时,那么应该将报警禁用放在input_loop()函数开头的某个位置

您还应该查看Python模块树中的readline.c源代码,以了解更多信息

#!/usr/bin/python

import readline
import logging
import signal
import os

LOG_FILENAME = '/tmp/completer.log'
HISTORY_FILENAME = '/tmp/completer.hist'

logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG,)

class YouAreTooSlow(Exception): pass


def get_history_items():
    return [ readline.get_history_item(i)
             for i in xrange(1, readline.get_current_history_length() + 1)
             ]

class HistoryCompleter(object):

    def __init__(self):
        self.matches = []
        return

    def complete(self, text, state):
        response = None
        if state == 0:
            history_values = get_history_items()
            logging.debug('history: %s', history_values)
            if text:
                self.matches = sorted(h 
                                      for h in history_values 
                                      if h and h.startswith(text))
            else:
                self.matches = []
            logging.debug('matches: %s', self.matches)
        try:
            response = self.matches[state]
        except IndexError:
            response = None
        logging.debug('complete(%s, %s) => %s', 
                      repr(text), state, repr(response))
        return response

def input_loop():
    if os.path.exists(HISTORY_FILENAME):
        readline.read_history_file(HISTORY_FILENAME)
    print 'Max history file length:', readline.get_history_length()
    print 'Startup history:', get_history_items()
    try:
        while True:
            line = raw_input('Prompt ("stop" to quit): ')
            if line == 'stop':
                break
            if line:
                print 'Adding "%s" to the history' % line
    finally:
        print 'Final history:', get_history_items()
        readline.write_history_file(HISTORY_FILENAME)

# Register our completer function

def slow_handler(signum, frame):
    print 'Signal handler called with signal', signum
    raise YouAreTooSlow()

def pre_input_hook():
    signal.signal(signal.SIGALRM, slow_handler)
    signal.alarm(10)

readline.set_pre_input_hook(pre_input_hook)
readline.set_completer(HistoryCompleter().complete)

# Use the tab key for completion
readline.parse_and_bind('tab: complete')

# Prompt the user for text
input_loop()

看起来readline的设计考虑到了这一能力()

普通readline()可以使用另一个接口。一些应用程序需要将键盘I/O与文件、设备或窗口系统I/O交错,通常是使用主循环在各种文件描述符上选择()。为了满足这一需求,readline还可以作为事件循环中的“回调”函数调用。有一些功能可以使这变得容易

然而,这些绑定似乎没有在Python中实现。进行快速搜索时,有一个家伙通过使用CTypes加载函数调用完成了POC。这并不理想,但缺乏任何选择,也许这是你唯一的行动方针