本文整理汇总了Python中pygments.lexers.python.PythonLexer方法的典型用法代码示例。如果您正苦于以下问题:Python python.PythonLexer方法的具体用法?Python python.PythonLexer怎么用?Python python.PythonLexer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygments.lexers.python
的用法示例。
在下文中一共展示了python.PythonLexer方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from pygments.lexers import python [as 别名]
# 或者: from pygments.lexers.python import PythonLexer [as 别名]
def main():
# Create user interface.
hello_world_window()
# Enable threading in GTK. (Otherwise, GTK will keep the GIL.)
gtk.gdk.threads_init()
# Read input from the command line, using an event loop with this hook.
# We use `patch_stdout`, because clicking the button will print something;
# and that should print nicely 'above' the input line.
with patch_stdout():
session = PromptSession(
"Python >>> ", inputhook=inputhook, lexer=PygmentsLexer(PythonLexer)
)
result = session.prompt()
print("You said: %s" % result)
示例2: __init__
# 需要导入模块: from pygments.lexers import python [as 别名]
# 或者: from pygments.lexers.python import PythonLexer [as 别名]
def __init__(self, **kwargs):
super(Python2Tokenizer, self).__init__(**kwargs)
self._lexer = PythonLexer()
示例3: _highlight
# 需要导入模块: from pygments.lexers import python [as 别名]
# 或者: from pygments.lexers.python import PythonLexer [as 别名]
def _highlight(self, source: str) -> str:
"""Highlight the given source code if we have markup support."""
if not self.hasmarkup or not self.code_highlight:
return source
try:
from pygments.formatters.terminal import TerminalFormatter
from pygments.lexers.python import PythonLexer
from pygments import highlight
except ImportError:
return source
else:
highlighted = highlight(
source, PythonLexer(), TerminalFormatter(bg="dark")
) # type: str
return highlighted
示例4: __init__
# 需要导入模块: from pygments.lexers import python [as 别名]
# 或者: from pygments.lexers.python import PythonLexer [as 别名]
def __init__(self, **options):
super(AntlrPythonLexer, self).__init__(PythonLexer, AntlrLexer,
**options)
示例5: get_tokens_unprocessed
# 需要导入模块: from pygments.lexers import python [as 别名]
# 或者: from pygments.lexers.python import PythonLexer [as 别名]
def get_tokens_unprocessed(self, text):
pylexer = PythonLexer(**self.options)
for pos, type_, value in pylexer.get_tokens_unprocessed(text):
if type_ == Token.Error and value == '$':
type_ = Comment.Preproc
yield pos, type_, value
示例6: main
# 需要导入模块: from pygments.lexers import python [as 别名]
# 或者: from pygments.lexers.python import PythonLexer [as 别名]
def main():
# Printing a manually constructed list of (Token, text) tuples.
text = [
(Token.Keyword, "print"),
(Token.Punctuation, "("),
(Token.Literal.String.Double, '"'),
(Token.Literal.String.Double, "hello"),
(Token.Literal.String.Double, '"'),
(Token.Punctuation, ")"),
(Token.Text, "\n"),
]
print_formatted_text(PygmentsTokens(text))
# Printing the output of a pygments lexer.
tokens = list(pygments.lex('print("Hello")', lexer=PythonLexer()))
print_formatted_text(PygmentsTokens(tokens))
# With a custom style.
style = Style.from_dict(
{
"pygments.keyword": "underline",
"pygments.literal.string": "bg:#00ff00 #ffffff",
}
)
print_formatted_text(PygmentsTokens(tokens), style=style)
示例7: output_why_test_failed
# 需要导入模块: from pygments.lexers import python [as 别名]
# 或者: from pygments.lexers.python import PythonLexer [as 别名]
def output_why_test_failed(self, test_result: TestResult):
err = test_result.error
if isinstance(err, TestFailure):
src_lines, line_num = inspect.getsourcelines(test_result.test.fn)
# TODO: Only include lines up to where the failure occurs
if src_lines[-1].strip() == "":
src_lines = src_lines[:-1]
gutter_width = len(str(len(src_lines) + line_num))
def gutter(i):
offset_line_num = i + line_num
rv = f"{str(offset_line_num):>{gutter_width}}"
if offset_line_num == err.error_line:
return colored(f"{rv} ! ", color="red")
else:
return lightblack(f"{rv} | ")
if err.operator in Comparison:
src = "".join(src_lines)
src = highlight(src, PythonLexer(), TerminalFormatter())
src = f"".join(
[gutter(i) + l for i, l in enumerate(src.splitlines(keepends=True))]
)
print(indent(src, DOUBLE_INDENT))
if err.operator == Comparison.Equals:
self.print_failure_equals(err)
else:
self.print_traceback(err)
print(Style.RESET_ALL)