MCPcopy Create free account
hub / github.com/nodejs/node / compile_expression

Method compile_expression

deps/v8/third_party/jinja2/environment.py:770–813  ·  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: str, undefined_to_none: bool = True
    )

Source from the content-addressed store, hash-verified

768 self.handle_exception(source=source_hint)
769
770 def compile_expression(
771 self, source: str, undefined_to_none: bool = True
772 ) -> "TemplateExpression":
773 """A handy helper method that returns a callable that accepts keyword
774 arguments that appear as variables in the expression. If called it
775 returns the result of the expression.
776
777 This is useful if applications want to use the same rules as Jinja
778 in template "configuration files" or similar situations.
779
780 Example usage:
781
782 >>> env = Environment()
783 >>> expr = env.compile_expression('foo == 42')
784 >>> expr(foo=23)
785 False
786 >>> expr(foo=42)
787 True
788
789 Per default the return value is converted to `None` if the
790 expression returns an undefined value. This can be changed
791 by setting `undefined_to_none` to `False`.
792
793 >>> env.compile_expression('var')() is None
794 True
795 >>> env.compile_expression('var', undefined_to_none=False)()
796 Undefined
797
798 .. versionadded:: 2.1
799 """
800 parser = Parser(self, source, state="variable")
801 try:
802 expr = parser.parse_expression()
803 if not parser.stream.eos:
804 raise TemplateSyntaxError(
805 "chunk after expression", parser.stream.current.lineno, None, None
806 )
807 expr.set_environment(self)
808 except TemplateSyntaxError:
809 self.handle_exception(source=source)
810
811 body = [nodes.Assign(nodes.Name("result", "store"), expr, lineno=1)]
812 template = self.from_string(nodes.Template(body, lineno=1))
813 return TemplateExpression(template, undefined_to_none)
814
815 def compile_templates(
816 self,

Callers

nothing calls this directly

Calls 8

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

Tested by

no test coverage detected