MCPcopy Index your code
hub / github.com/nodejs/node / compile_expression

Method compile_expression

tools/inspector_protocol/jinja2/environment.py:593–636  ·  view source on GitHub ↗

A handy helper method that returns a callable that accepts keyword arguments that appear as variables in the expression. If called it returns the result of the expression. This is useful if applications want to use the same rules as Jinja in template "configuration

(self, source, undefined_to_none=True)

Source from the content-addressed store, hash-verified

591 self.handle_exception(exc_info, source_hint=source_hint)
592
593 def compile_expression(self, source, undefined_to_none=True):
594 """A handy helper method that returns a callable that accepts keyword
595 arguments that appear as variables in the expression. If called it
596 returns the result of the expression.
597
598 This is useful if applications want to use the same rules as Jinja
599 in template "configuration files" or similar situations.
600
601 Example usage:
602
603 >>> env = Environment()
604 >>> expr = env.compile_expression('foo == 42')
605 >>> expr(foo=23)
606 False
607 >>> expr(foo=42)
608 True
609
610 Per default the return value is converted to `None` if the
611 expression returns an undefined value. This can be changed
612 by setting `undefined_to_none` to `False`.
613
614 >>> env.compile_expression('var')() is None
615 True
616 >>> env.compile_expression('var', undefined_to_none=False)()
617 Undefined
618
619 .. versionadded:: 2.1
620 """
621 parser = Parser(self, source, state='variable')
622 exc_info = None
623 try:
624 expr = parser.parse_expression()
625 if not parser.stream.eos:
626 raise TemplateSyntaxError('chunk after expression',
627 parser.stream.current.lineno,
628 None, None)
629 expr.set_environment(self)
630 except TemplateSyntaxError:
631 exc_info = sys.exc_info()
632 if exc_info is not None:
633 self.handle_exception(exc_info, source_hint=source)
634 body = [nodes.Assign(nodes.Name('result', 'store'), expr, lineno=1)]
635 template = self.from_string(nodes.Template(body, lineno=1))
636 return TemplateExpression(template, undefined_to_none)
637
638 def compile_templates(self, target, extensions=None, filter_func=None,
639 zip='deflated', log_function=None,

Callers

nothing calls this directly

Calls 9

parse_expressionMethod · 0.95
handle_exceptionMethod · 0.95
from_stringMethod · 0.95
ParserClass · 0.90
TemplateSyntaxErrorClass · 0.90
exc_infoMethod · 0.80
TemplateExpressionClass · 0.70
set_environmentMethod · 0.45
NameMethod · 0.45

Tested by

no test coverage detected