documentation, comments, etc

This commit is contained in:
Dominik Moritz Roth 2020-07-17 20:40:49 +02:00
parent 9d2588306f
commit 2a423adb89

16
Rex.py
View File

@ -8,7 +8,7 @@ from prompt_toolkit.completion import Completer
from prompt_toolkit.completion import Completion from prompt_toolkit.completion import Completion
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from fuzzywuzzy import fuzz from fuzzywuzzy import fuzz
# @@@@ # @@@@
# &@((((((((@@&(@@@ # &@((((((((@@&(@@@
# &(((((@(((((((@((((((((((((((( # &(((((@(((((((@(((((((((((((((
@ -64,12 +64,13 @@ from fuzzywuzzy import fuzz
# @ @ # @ @
# @@@@, @@@ # @@@@, @@@
# These are the example functions, that we are going to map to
async def test(): async def test():
print("pokemon go") print("pokemon go")
async def close(): async def close():
exit() exit()
async def nA(): async def nA():
print("this is a test") print("this is a test")
async def nB(): async def nB():
@ -83,6 +84,9 @@ async def arg2(arg1, arg2):
async def arg4(arg1, arg2, arg3, arg4): async def arg4(arg1, arg2, arg3, arg4):
print("got " + arg1 + " and " + arg2 + " and " + arg3 + " and " + arg4) print("got " + arg1 + " and " + arg2 + " and " + arg3 + " and " + arg4)
# This is an example (the default) cmd-dict
cmds = { cmds = {
"test": test, "test": test,
"exit": close, "exit": close,
@ -99,26 +103,32 @@ cmds = {
} }
class _CompletionLookup(Completer): class _CompletionLookup(Completer):
# Dont touch it; it works...
def get_completions(self, document, complete_event): def get_completions(self, document, complete_event):
words = document.text.split(" ") words = document.text.split(" ")
pos = cmds pos = cmds
index = -1 index = -1
# For evere entered 'word'
for i,word in enumerate(words): for i,word in enumerate(words):
index = i index = i
# We traverse one step further down the tree, until we hit a function
if not str(type(pos))=="<class 'function'>" and word in pos: if not str(type(pos))=="<class 'function'>" and word in pos:
pos = pos[word] pos = pos[word]
else: else:
break break
# If we are not at a function yet, we are going to generate autocomplete hints
if not str(type(pos))=="<class 'function'>" : if not str(type(pos))=="<class 'function'>" :
comps = [] comps = []
for word in pos: for word in pos:
score = fuzz.partial_ratio(word,words[-1]) + fuzz.ratio(word, words[-1]) score = fuzz.partial_ratio(word,words[-1]) + fuzz.ratio(word, words[-1])
if score > 90 or (len(words)==index+1 and str(document.text).endswith(" ")): if score > 90 or (len(words)==index+1 and str(document.text).endswith(" ")):
comps.append([word, score]) comps.append([word, score])
# Which are sorted by relevance
comps.sort(key = lambda x: x[1], reverse = True) comps.sort(key = lambda x: x[1], reverse = True)
for i in range(min(5, len(comps))): for i in range(min(5, len(comps))):
yield Completion(comps[i][0], start_position=0) yield Completion(comps[i][0], start_position=0)
else: else:
# When we are already at a function, we give hints about expected arguments
args = inspect.getfullargspec(pos)[0] args = inspect.getfullargspec(pos)[0]
if len(args)>len(words)-index-1: if len(args)>len(words)-index-1:
arg = args[len(words)-index-1] arg = args[len(words)-index-1]