我一直在尝试使用以下示例使用提示工具包:
from pygments.lexer import RegexLexer
from pygments.token import *
class CalculatorLexer(RegexLexer):
name = 'Calculator'
tokens = {
'root': [
(r"[\+\-\/\*\(\)\%\^]", Operator), # All the single character operators
(r"\d*\.\d*", Number.Float), # A Decimal
(r"\d*", Number.Integer), # A Integer
(r".*", Text) # Any Other Thing
]
}
from prompt_toolkit.shortcuts import prompt
from prompt_toolkit.lexers import PygmentsLexer
text = prompt('Enter HTML: ', lexer=PygmentsLexer(CalculatorLexer()))
print('You said: %s' % text)
但它总是会引发这个错误:
Traceback (most recent call last):
File "test.py", line 21, in <module>
text = prompt('Enter HTML: ', lexer=PygmentsLexer(CalculatorLexer()))
File "/home/xcodz/.local/lib/python3.8/site-packages/prompt_toolkit/lexers/pygments.py", line 197, in __init__
self.pygments_lexer = pygments_lexer_cls(
TypeError: 'CalculatorLexer' object is not callable
起初我认为我不应该调用我创建的自定义词法分析器类。但是如果我不初始化我的自定义类,程序什么也不做,并且 RAM 使用率立即开始上升。由于这种行为,我的电脑已经崩溃了 2 次。
这是我启动并终止程序进程后发生的情况: