Skip to content

Commit

Permalink
fix(ConsolePlus): 🐛 debug and optimize some functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ARCJ137442 committed Sep 21, 2023
1 parent 316987d commit d7665d9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
32 changes: 21 additions & 11 deletions pynars/ConsolePlus.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,20 +251,27 @@ def printHistory(*args: list[str]) -> None:


@cmdRegister(('execute', 'exec'))
def execPythonCode(*args: list[str]) -> None:
def execCode(*args: list[str]) -> None:
'''Format: exec <Python code >
Directly invoke Python's built-in exec cmd to execute a single line of code'''
exec(' '.join(args))


@cmdRegister(('execute', 'exec'))
def execCode(*args: list[str]) -> None:
'''Format: eval <Python code >
Directly invoke Python's built-in eval cmd to evaluate a single line of code'''
print(f'eval result: {eval(" ".join(args))}')


@cmdRegister(('simplify-parse', 'parse'))
def toggleSimplifyParse() -> None:
'''Toggle the "automatic shorthand parsing" function of the cmd (enabled by default),
If so, the Narsese input are automatically parsed in a simplified format'''
global _parseNeedSlash
_parseNeedSlash = not _parseNeedSlash
print(
f'Narsese parse simplification {"closed" if _parseNeedSlash else "opened"}.')
f'Narsese automatic analytic simplification {"closed" if _parseNeedSlash else "opened"}.')


@cmdRegister('help')
Expand All @@ -285,7 +292,7 @@ def help(*keywords: list[str]) -> None:
f'''<{"/".join(cmdNameAlias)}>: {cmdFunction.__name__}\n{
cmdFunction.__doc__
if cmdFunction.__doc__
else "!!! This directive has not yet been specifically described !!!"
else "!!! This cmd don't have any description !!!"
}\n''')
return
print(f'No commends is browsed by "{", ".join(keywords)}"!')
Expand Down Expand Up @@ -373,34 +380,37 @@ def macroQuery(*args: list[str]) -> None:
showMacro(name=name)


def execMacro(name: str) -> None:
def macroExec1(name: str) -> None:
'''Execute 1 macro'''
cmds: list[str] = storedMacros[name]
for cmd in cmds:
execInput(cmd)


@cmdRegister(('macro-exec', 'm-exec'))
def execMacros(*args: list[str]) -> None:
def macroExec(*args: list[str]) -> None:
'''Format: macro-exec [... macro name]
Execute macros by name (unlimited number can be stacked)
If empty, execute macro "" (empty string)'''
args = args if args else ['']
for name in args:
execMacro(name=name)
macroExec1(name=name)


@cmdRegister(('macro-repeat', 'm-repeat'))
def macroRepeat(name: str, numExecutes: int) -> None:
'''Format: macro-repeat < macro name > < execution times >
Execute macros as "name + number" (number can be stacked indefinitely)'''
for _ in range(numExecutes):
execMacro(name=name)
macroExec1(name=name)

# Reasoner management #


currentNARSInterface: NARSInterface = NARSInterface.constructInterface(
137, 500, 500, silent=False)
137,
500, 500,
silent=False)

reasoners: dict[str:Reasoner] = {'initial': currentNARSInterface}

Expand Down Expand Up @@ -609,7 +619,7 @@ def execInput(inp: str, *otherInput: list[str]) -> None:
# auto complete
if not cmdName in nameAliasHints[0]:
print(
f'Autocompleted cmd to "{"/".join(nameAliasHints[0])}" ')
f'Autocompleted cmd to "{"/".join(nameAliasHints[0])}".')
nameAliasIndex = nameAliasHints[0]
# Cmd execution: Automatically adjust the "parameter requirements" of specific cmds and intercept parameters
cmdData = PRESET_CMDS[nameAliasIndex]
Expand All @@ -626,8 +636,8 @@ def execInput(inp: str, *otherInput: list[str]) -> None:
print('Cmd execute failed: ',
e.with_traceback(None) if e else e)
else:
hint = 'What are you looking for' + \
"\", \"".join('/'.join(alias) for alias in nameAliasHints) + \
hint = 'Are you looking for "' + \
"\"|\"".join('/'.join(alias) for alias in nameAliasHints) + \
'"?' if nameAliasHints else ''
print(f'Unknown cmd {cmdName}. {hint}')
return # If it's executed as a command, it won't execute as Narsese input
Expand Down
12 changes: 6 additions & 6 deletions pynars/Interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def floatRestrictStr(x):
else ' ')

def outMessageNoColor(type: PrintType, content,
pStr: float = None, dStr: float = None, qStr: float = None,
p: float = None, d: float = None, q: float = None,
comment_title: str = None):
''''from pynars.utils.Print import out_print'''
# show_budget = True
Expand All @@ -166,9 +166,9 @@ def outMessageNoColor(type: PrintType, content,
# show_budget = False
# else:
# show_budget = False
pStr: str = NARSInterface.floatRestrictStr(pStr)
qStr: str = NARSInterface.floatRestrictStr(qStr)
dStr: str = NARSInterface.floatRestrictStr(dStr)
pStr: str = NARSInterface.floatRestrictStr(p)
qStr: str = NARSInterface.floatRestrictStr(q)
dStr: str = NARSInterface.floatRestrictStr(d)
if type:
# ! ↓ The value of this enumeration class comes with a foreground color
value: str = type.value[5:-1]
Expand All @@ -186,8 +186,8 @@ def outPrintNoColor(type: PrintType, content, p: float = None, d: float = None,
NARSInterface.outMessageNoColor(
type=type, content=content,
p=p, d=d, q=q,
comment_title=comment_title,
end=end))
comment_title=comment_title),
end=end)

def _is0to1Float(x): return isinstance(x, float) and 0 <= x <= 1

Expand Down

0 comments on commit d7665d9

Please sign in to comment.