本文整理匯總了Python中rlcompleter.Completer方法的典型用法代碼示例。如果您正苦於以下問題:Python rlcompleter.Completer方法的具體用法?Python rlcompleter.Completer怎麽用?Python rlcompleter.Completer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類rlcompleter
的用法示例。
在下文中一共展示了rlcompleter.Completer方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: run_console
# 需要導入模塊: import rlcompleter [as 別名]
# 或者: from rlcompleter import Completer [as 別名]
def run_console(glob):
import readline
import rlcompleter
import atexit
import code
history_path = os.path.expanduser("~/.python_history")
def save_history(history_path=history_path):
readline.write_history_file(history_path)
if os.path.exists(history_path):
readline.read_history_file(history_path)
atexit.register(save_history)
readline.set_completer(rlcompleter.Completer(glob).complete)
readline.parse_and_bind("tab: complete")
code.InteractiveConsole(locals=glob).interact()
示例2: complete
# 需要導入模塊: import rlcompleter [as 別名]
# 或者: from rlcompleter import Completer [as 別名]
def complete(self, text, state):
if state == 0:
self.completions = []
append = self.completions.append
for path in glob.glob(text + '*'):
if os.path.isdir(path):
if not path.endswith(os.sep):
path += os.sep
else:
path += ' '
append(path)
pycompleter = rlcompleter.Completer(namespace=self.local).complete
for i in count():
completion = pycompleter(text, i)
if completion is None:
break
else:
append(completion)
try:
return self.completions[state]
except IndexError:
return None
示例3: test_main
# 需要導入模塊: import rlcompleter [as 別名]
# 或者: from rlcompleter import Completer [as 別名]
def test_main():
support.run_unittest(TestRlcompleter)
def test_duplicate_globals(self):
namespace = {
'False': None, # Keyword vs builtin vs namespace
'assert': None, # Keyword vs namespace
'try': lambda: None, # Keyword vs callable
'memoryview': None, # Callable builtin vs non-callable
'Ellipsis': lambda: None, # Non-callable builtin vs callable
}
completer = rlcompleter.Completer(namespace)
self.assertEqual(completer.complete('False', 0), 'False')
self.assertIsNone(completer.complete('False', 1)) # No duplicates
self.assertEqual(completer.complete('assert', 0), 'assert')
self.assertIsNone(completer.complete('assert', 1))
self.assertEqual(completer.complete('try', 0), 'try')
self.assertIsNone(completer.complete('try', 1))
# No opening bracket "(" because we overrode the built-in class
self.assertEqual(completer.complete('memoryview', 0), 'memoryview')
self.assertIsNone(completer.complete('memoryview', 1))
self.assertEqual(completer.complete('Ellipsis', 0), 'Ellipsis(')
self.assertIsNone(completer.complete('Ellipsis', 1))
示例4: complete_names
# 需要導入模塊: import rlcompleter [as 別名]
# 或者: from rlcompleter import Completer [as 別名]
def complete_names(word, namespace):
"""Complete variable names or attributes
:param word: word to be completed
:type word: str
:param namespace: namespace
:type namespace: dict
:returns: completion matches
:rtype: list of str
>>> complete_names('fo', {'foo': 'bar'})
['foo', 'for', 'format(']
"""
# start completer
completer = rlcompleter.Completer(namespace)
# find matches with std library (don't try to implement this yourself)
completer.complete(word, 0)
return sorted(set(completer.matches))
示例5: test_duplicate_globals
# 需要導入模塊: import rlcompleter [as 別名]
# 或者: from rlcompleter import Completer [as 別名]
def test_duplicate_globals(self):
namespace = {
'False': None, # Keyword vs builtin vs namespace
'assert': None, # Keyword vs namespace
'try': lambda: None, # Keyword vs callable
'memoryview': None, # Callable builtin vs non-callable
'Ellipsis': lambda: None, # Non-callable builtin vs callable
}
completer = rlcompleter.Completer(namespace)
self.assertEqual(completer.complete('False', 0), 'False')
self.assertIsNone(completer.complete('False', 1)) # No duplicates
self.assertEqual(completer.complete('assert', 0), 'assert')
self.assertIsNone(completer.complete('assert', 1))
self.assertEqual(completer.complete('try', 0), 'try')
self.assertIsNone(completer.complete('try', 1))
# No opening bracket "(" because we overrode the built-in class
self.assertEqual(completer.complete('memoryview', 0), 'memoryview')
self.assertIsNone(completer.complete('memoryview', 1))
self.assertEqual(completer.complete('Ellipsis', 0), 'Ellipsis(')
self.assertIsNone(completer.complete('Ellipsis', 1))
示例6: enable_autocomplete_and_history
# 需要導入模塊: import rlcompleter [as 別名]
# 或者: from rlcompleter import Completer [as 別名]
def enable_autocomplete_and_history(adir,env):
try:
import rlcompleter
import atexit
import readline
except ImportError:
pass
else:
readline.parse_and_bind("bind ^I rl_complete"
if sys.platform == 'darwin'
else "tab: complete")
history_file = os.path.join(adir,'.pythonhistory')
try:
readline.read_history_file(history_file)
except IOError:
open(history_file, 'a').close()
atexit.register(readline.write_history_file, history_file)
readline.set_completer(rlcompleter.Completer(env).complete)
示例7: start
# 需要導入模塊: import rlcompleter [as 別名]
# 或者: from rlcompleter import Completer [as 別名]
def start(self) -> None:
try:
import readline
except ImportError:
pass
else:
# We don't have to wrap the following import in a 'try', because
# we already know 'readline' was imported successfully.
import rlcompleter
readline.set_completer(rlcompleter.Completer(self.context).complete)
readline.parse_and_bind("tab:complete")
if self.prompt:
sys.ps1 = self.prompt
if self.output:
warnings.warn("Custom output templates not supported by PythonShell.")
code.interact(self.banner, local=self.context)
return None
示例8: run_client
# 需要導入模塊: import rlcompleter [as 別名]
# 或者: from rlcompleter import Completer [as 別名]
def run_client(host="localhost", port=9901):
import code
import readline
import rlcompleter
url = "http://{host}:{port}/jsonrpc".format(host=host, port=port)
cli = TeleniumHttpClient(url=url, timeout=5)
print("Connecting to {}".format(url))
while not cli.ping():
sleep(.1)
print("Connected!")
vars = globals()
vars.update(locals())
readline.set_completer(rlcompleter.Completer(vars).complete)
readline.parse_and_bind("tab: complete")
shell = code.InteractiveConsole(vars)
shell.interact()
示例9: setupReadline
# 需要導入模塊: import rlcompleter [as 別名]
# 或者: from rlcompleter import Completer [as 別名]
def setupReadline(local):
"""Initialize the readline library and command history.
@return: A C{bool} to indicate whether standard input is a terminal
(and therefore interactive).
"""
readline.parse_and_bind('tab: complete')
readline.set_completer_delims(' \t\n')
readline.set_completer(Completer(local).complete)
# Readline code from https://docs.python.org/3.7/library/readline.html
histfile = os.path.join(os.path.expanduser('~'), '.daudin_history')
try:
readline.read_history_file(histfile)
historyLen = readline.get_current_history_length()
except FileNotFoundError:
open(histfile, 'wb').close()
historyLen = 0
try:
readline.append_history_file
except AttributeError:
# We won't be able to save readline history. This can happen on
# Python 3.5 at least - not sure why.
pass
else:
import atexit
def saveHistory(prevHistoryLen, histfile):
newHistoryLen = readline.get_current_history_length()
readline.set_history_length(1000)
readline.append_history_file(newHistoryLen - prevHistoryLen,
histfile)
atexit.register(saveHistory, historyLen, histfile)
return True
示例10: handle
# 需要導入模塊: import rlcompleter [as 別名]
# 或者: from rlcompleter import Completer [as 別名]
def handle(self, **options):
try:
if options['plain']:
# Don't bother loading IPython, because the user wants plain Python.
raise ImportError
self.run_shell(shell=options['interface'])
except ImportError:
import code
# Set up a dictionary to serve as the environment for the shell, so
# that tab completion works on objects that are imported at runtime.
# See ticket 5082.
imported_objects = {}
try: # Try activating rlcompleter, because it's handy.
import readline
except ImportError:
pass
else:
# We don't have to wrap the following import in a 'try', because
# we already know 'readline' was imported successfully.
import rlcompleter
readline.set_completer(rlcompleter.Completer(imported_objects).complete)
readline.parse_and_bind("tab:complete")
# We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system
# conventions and get $PYTHONSTARTUP first then .pythonrc.py.
if not options['no_startup']:
for pythonrc in (os.environ.get("PYTHONSTARTUP"), '~/.pythonrc.py'):
if not pythonrc:
continue
pythonrc = os.path.expanduser(pythonrc)
if not os.path.isfile(pythonrc):
continue
try:
with open(pythonrc) as handle:
exec(compile(handle.read(), pythonrc, 'exec'), imported_objects)
except NameError:
pass
code.interact(local=imported_objects)
示例11: __exec_py
# 需要導入模塊: import rlcompleter [as 別名]
# 或者: from rlcompleter import Completer [as 別名]
def __exec_py(self, args):
ns = {"api": self.api, "args": args[1:], "analyzer": self.analyzer}
ns.update(EXPORTED_SYMBOLS)
if len(args) > 1:
if args[1].startswith("!"):
args[1] = "%s/%s" % (PLASMA_SCRIPTS_DIR, args[1][1:])
exec(open(args[1]).read(), ns)
else:
readline.set_completer(rlcompleter.Completer(ns).complete)
code.interact(local=ns)
readline.set_completer(self.comp.complete)
示例12: setUp
# 需要導入模塊: import rlcompleter [as 別名]
# 或者: from rlcompleter import Completer [as 別名]
def setUp(self):
self.stdcompleter = rlcompleter.Completer()
self.completer = rlcompleter.Completer(dict(spam=int,
egg=str,
CompleteMe=CompleteMe))
# forces stdcompleter to bind builtins namespace
self.stdcompleter.complete('', 0)
示例13: test_namespace
# 需要導入模塊: import rlcompleter [as 別名]
# 或者: from rlcompleter import Completer [as 別名]
def test_namespace(self):
class A(dict):
pass
class B(list):
pass
self.assertTrue(self.stdcompleter.use_main_ns)
self.assertFalse(self.completer.use_main_ns)
self.assertFalse(rlcompleter.Completer(A()).use_main_ns)
self.assertRaises(TypeError, rlcompleter.Completer, B((1,)))
示例14: test_excessive_getattr
# 需要導入模塊: import rlcompleter [as 別名]
# 或者: from rlcompleter import Completer [as 別名]
def test_excessive_getattr(self):
# Ensure getattr() is invoked no more than once per attribute
class Foo:
calls = 0
@property
def bar(self):
self.calls += 1
return None
f = Foo()
completer = rlcompleter.Completer(dict(f=f))
self.assertEqual(completer.complete('f.b', 0), 'f.bar')
self.assertEqual(f.calls, 1)
示例15: global_matches
# 需要導入模塊: import rlcompleter [as 別名]
# 或者: from rlcompleter import Completer [as 別名]
def global_matches( self, text ):
matches = rlcompleter.Completer.global_matches( self, text )
if not matches: matches = []
matches += self.file_matches( text )
return matches