Skip to content
Myr

clean up scientific notation support

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

add changelog clean up docs parametrize tests

authored by David Lord · committed by David Lord · parent fd00805 · Browse files · 4 files+40 added−21 removed
This commit

State

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

Diff stats

4 files+40 added−21 removed

  • tests:+30 added−3 removed
  • other:+10 added−18 removed

4 files+40 added−21 removed

modified:docs/templates.rst+6 added-11 removed
394 unmodified lines
395this template, it first locates the parent. The extends tag should be the396first tag in the template. Everything before it is printed out normally and397may cause confusion. For details about this behavior and how to take398advantage of it, see :ref:`null-master-fallback`. Also a block will always be399filled in regardless of whether the surrounding condition is evaluated to be true 399filled in regardless of whether the surrounding condition is evaluated to be true400or false.401402The filename of the template depends on the template loader. For example, the403:class:`FileSystemLoader` allows you to access other templates by giving the
773 unmodified lines
1177 arguments to function calls and filters, or just to extend or include a1178 template).1179118042: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).1181 Integers are whole numbers without a decimal part.1182118642.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. 118342.23 / 42.1e2:1184 Floating point numbers can be written using a '.' as a decimal mark.1185 They can also be written in scientific notation with an upper or1186 lower case 'e' to indicate the exponent part.11871188['list', 'of', 'objects']:1189 Everything between two brackets is a list. Lists are useful for storing1190 sequential data to be iterated over. For example, you can easily
569 unmodified lines
modified:jinja2/lexer.py+2 added-7 removed
13 unmodified lines
14 :copyright: (c) 2017 by the Jinja Team.15 :license: BSD, see LICENSE for more details.16"""17import re18from ast import literal_eval19from collections import deque20from operator import itemgetter2122from jinja2._compat import implements_iterator, intern, iteritems, text_type23from jinja2.exceptions import TemplateSyntaxError24from jinja2.utils import LRUCache2525from ast import literal_eval # to support scientific notation2626# cache for the lexers. Exists in order to be able to have multiple27# environments with the same lexer28_lexer_cache = LRUCache(50)29
22 unmodified lines
52 import jinja253 del jinja2._identifier54 del _identifier5557# 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.60float_re = re.compile(r'(?<!\.)\d+\.?\d+(?:e-?\d+)?', re.IGNORECASE)56float_re = re.compile(r'(?<!\.)\d+(?:\.\d+)?(?:e[+\-]?\d+)?', re.IGNORECASE)6157newline_re = re.compile(r'(\r\n|\r|\n)')5859# internal the tokens and keep references to them60TOKEN_ADD = intern('add')
690 unmodified lines
modified:tests/test_lexnparse.py+30 added-3 removed
334 unmodified lines
335 def test_inop(self, env):336 tmpl = env.from_string('{{ 1 in [1, 2, 3] }}|{{ 1 not in [1, 2, 3] }}')337 assert tmpl.render() == 'True|False'338339 def test_literals(self, env):340 tmpl = env.from_string('{{ [] }}|{{ {} }}|{{ () }}')341 assert tmpl.render().lower() == '[]|{}|()'339 @pytest.mark.parametrize("value", ("[]", "{}", "()"))340 def test_collection_literal(self, env, value):341 t = env.from_string("{{ %s }}" % value)342 assert t.render() == value343344 @pytest.mark.parametrize("value", ("1", "123"))345 def test_int_literal(self, env, value):346 t = env.from_string("{{ %s }}" % value)347 assert t.render() == value348349 @pytest.mark.parametrize(350 "value",351 (352 "1.2",353 "34.56",354 ("1e0", "1.0"),355 ("10e1", "100.0"),356 ("2.5e100", "2.5e+100"),357 "2.5e+100",358 ("25.6e-10", "2.56e-09"),359 ),360 )361 def test_float_literal(self, env, value):362 if isinstance(value, tuple):363 value, expect = value364 else:365 expect = value366367 t = env.from_string("{{ %s }}" % value)368 assert t.render() == expect369370 def test_bool(self, env):371 tmpl = env.from_string('{{ true and false }}|{{ false '372 'or true }}|{{ not false }}')373 assert tmpl.render() == 'False|True|True'
319 unmodified lines
modified:CHANGES.rst+2 added
19 unmodified lines
20 ``lstrip_blocks == False`` (`#748`_)21- Added a ``default`` parameter for the ``map`` filter. (`#557`_)22- Exclude environment globals from23 :func:`meta.find_undeclared_variables`. #93124- Float literals can be written with scientific notation, like25 ``{{ 2.56e-3 }}``. #912, #9222627.. _#557: https://github.com/pallets/jinja/issues/55728.. _#765: https://github.com/pallets/jinja/issues/76529.. _#748: https://github.com/pallets/jinja/issues/748
613 unmodified lines