Merge pull request #922 from CleoQc/feature/support_scientific_notation
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Feature/support scientific notation
authored by David Lord · committed by GitHub · parents 626bdd0, 483b62e · Browse files · 5 files·+46 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+46 added−12 removed
- tests:+33 added−4 removed
- other:+13 added−8 removed
Provenance
- Commit
278a057- Tree
178cb43- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
626bdd0- Parent 2
483b62e
5 files changed+46 added−12 removed
modified:docs/templates.rst+8 added-6 removed
394 unmodified lines395 this template, it first locates the parent. The extends tag should be the396 first tag in the template. Everything before it is printed out normally and397 may cause confusion. For details about this behavior and how to take398 advantage of it, see :ref:`null-master-fallback`. Also a block will always be399-filled in regardless of whether the surrounding condition is evaluated to be true 399+filled in regardless of whether the surrounding condition is evaluated to be true400 or false.401 402 The filename of the template depends on the template loader. For example, the403 :class:`FileSystemLoader` allows you to access other templates by giving the772 unmodified lines1176 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).1179 1180-42 / 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 an1183- integer. Keep in mind that, in Python, ``42`` and ``42.0``1184- are different (``int`` and ``float``, respectively).1180+42:1181+ Integers are whole numbers without a decimal part.1182 1183+42.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.1187+1188 ['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 easily1191 create a list of links using lists and tuples for (and with) a for loop::568 unmodified linesmodified:jinja2/lexer.py+3 added-2 removed
13 unmodified lines14 :copyright: (c) 2017 by the Jinja Team.15 :license: BSD, see LICENSE for more details.16 """17 import re18+from ast import literal_eval19 from collections import deque20 from operator import itemgetter21 22 from jinja2._compat import implements_iterator, intern, iteritems, text_type29 unmodified lines52 import jinja253 del jinja2._identifier54 del _identifier55 55-float_re = re.compile(r'(?<!\.)\d+\.\d+')56+float_re = re.compile(r'(?<!\.)\d+(?:\.\d+)?(?:e[+\-]?\d+)?', re.IGNORECASE)57 newline_re = re.compile(r'(\r\n|\r|\n)')58 59 # internal the tokens and keep references to them60 TOKEN_ADD = intern('add')540 unmodified lines601 raise TemplateSyntaxError(msg, lineno, name, filename)602 elif token == 'integer':603 value = int(value)604 elif token == 'float':604- value = float(value)605+ value = literal_eval(value)606 elif token == 'operator':607 token = operators[value]608 yield Token(lineno, token, value)609 141 unmodified linesmodified:tests/test_filters.py+3 added-1 removed
134 unmodified lines135 136 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'144 145 def test_format(self, env):146 tmpl = env.from_string('''{{ "%s|%s"|format("a", "b") }}''')147 out = tmpl.render()577 unmodified linesmodified:tests/test_lexnparse.py+30 added-3 removed
334 unmodified lines335 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'338 339- 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() == value343 344+ @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() == value348+349+ @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 = value366+367+ t = env.from_string("{{ %s }}" % value)368+ assert t.render() == expect369+370 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 linesmodified:CHANGES.rst+2 added
19 unmodified lines20 ``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, #92226 27 .. _#557: https://github.com/pallets/jinja/issues/55728 .. _#765: https://github.com/pallets/jinja/issues/76529 .. _#748: https://github.com/pallets/jinja/issues/748613 unmodified lines