Skip to content
Myr

support scientific notation

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by CleoQc · committed by David Lord · parent 626bdd0 · Browse files · 3 files+21 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+21 added−6 removed

  • tests:+3 added−1 removed
  • other:+18 added−5 removed

3 files+21 added−6 removed

modified:docs/templates.rst+10 added-3 removed
1175 unmodified lines
1176 useful whenever you need a string in the template (e.g. as1177 arguments to function calls and filters, or just to extend or include a1178 template).1179118042 / 42.23:1181 Integers and floating point numbers are created by just writing the1182 number down. If a dot is present, the number is a float, otherwise an118042:1181 Integers numbers are created by just writing the number down. 1182 If a dot is present, the number will be considered a float, otherwise an1183 integer. Keep in mind that, in Python, ``42`` and ``42.0``1184 are different (``int`` and ``float``, respectively).1185118642.23 / 42e2 / 42E2 / 1e0:1187 Floating point numbers are created by just writing the1188 number down. Floating points can be written using the dot as a decimal mark,1189 or they can be written in scientific notation in which1190 case you can use lower case 'e' or upper case 'E' to indicate the exponent1191 part. 11921193['list', 'of', 'objects']:1194 Everything between two brackets is a list. Lists are useful for storing1195 sequential data to be iterated over. For example, you can easily1196 create a list of links using lists and tuples for (and with) a for loop::
568 unmodified lines
modified:jinja2/lexer.py+8 added-2 removed
20 unmodified lines
21from jinja2._compat import implements_iterator, intern, iteritems, text_type22from jinja2.exceptions import TemplateSyntaxError23from jinja2.utils import LRUCache2425from ast import literal_eval # to support scientific notation2627# cache for the lexers. Exists in order to be able to have multiple28# environments with the same lexer29_lexer_cache = LRUCache(50)30
22 unmodified lines
53 import jinja254 del jinja2._identifier55 del _identifier5657# Note: Float now supports 0 or 1 dots, and must thus be evaluated in the right 58# order so that pure integers are caught first. Floats can now be written with 59# scientific notation.55float_re = re.compile(r'(?<!\.)\d+\.\d+')60float_re = re.compile(r'(?<!\.)\d+\.?\d+(?:e-?\d+)?', re.IGNORECASE)6162newline_re = re.compile(r'(\r\n|\r|\n)')6364# internal the tokens and keep references to them65TOKEN_ADD = intern('add')
540 unmodified lines
606 raise TemplateSyntaxError(msg, lineno, name, filename)607 elif token == 'integer':608 value = int(value)609 elif token == 'float':604 value = float(value)610 value = literal_eval(value)611 elif token == 'operator':612 token = operators[value]613 yield Token(lineno, token, value)614
141 unmodified lines
modified:tests/test_filters.py+3 added-1 removed
134 unmodified lines
135136 def test_float(self, env):137 tmpl = env.from_string('{{ "42"|float }}|'138 '{{ "ajsghasjgd"|float }}|'139 '{{ "10e1"|float }}|'140 '{{ "10.5e-10"|float }}|'141 '{{ "32.32"|float }}')142 out = tmpl.render()141 assert out == '42.0|0.0|32.32'143 assert out == '42.0|0.0|100.0|1.05e-09|32.32'144145 def test_format(self, env):146 tmpl = env.from_string('''{{ "%s|%s"|format("a", "b") }}''')147 out = tmpl.render()
577 unmodified lines