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
Provenance
- Commit
fd00805- Tree
a53cf0f- Author
CleoQc <cleoqc1124@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
626bdd0
3 files changed+21 added−6 removed
modified:docs/templates.rst+10 added-3 removed
1175 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 an1180+42: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).1185 1186+42.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. 1192+1193 ['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 linesmodified:jinja2/lexer.py+8 added-2 removed
20 unmodified lines21 from jinja2._compat import implements_iterator, intern, iteritems, text_type22 from jinja2.exceptions import TemplateSyntaxError23 from jinja2.utils import LRUCache24 25+from ast import literal_eval # to support scientific notation26+27 # 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 lines53 import jinja254 del jinja2._identifier55 del _identifier56 57+# 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.55-float_re = re.compile(r'(?<!\.)\d+\.\d+')60+float_re = re.compile(r'(?<!\.)\d+\.?\d+(?:e-?\d+)?', re.IGNORECASE)61+62 newline_re = re.compile(r'(\r\n|\r|\n)')63 64 # internal the tokens and keep references to them65 TOKEN_ADD = intern('add')540 unmodified lines606 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 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 lines