Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scientific notation not supported in Python expressions #912

Closed
jason-s opened this issue Oct 16, 2018 · 4 comments · Fixed by #922
Closed

scientific notation not supported in Python expressions #912

jason-s opened this issue Oct 16, 2018 · 4 comments · Fixed by #922

Comments

@jason-s
Copy link

@jason-s jason-s commented Oct 16, 2018

>>> import jinja2
>>> jinja2.__version__
'2.10'
>>> T = jinja2.Template('{{ 0.001 }}')
>>> T.render()
u'0.001'
>>> T = jinja2.Template('{{ 1e-3 }}')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\app\python\anaconda\2\lib\site-packages\jinja2\environment.py", line 945, in __new__
    return env.from_string(source, template_class=cls)
  File "c:\app\python\anaconda\2\lib\site-packages\jinja2\environment.py", line 880, in from_string
    return cls.from_code(self, self.compile(source), globals, None)
  File "c:\app\python\anaconda\2\lib\site-packages\jinja2\environment.py", line 591, in compile
    self.handle_exception(exc_info, source_hint=source_hint)
  File "c:\app\python\anaconda\2\lib\site-packages\jinja2\environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "<unknown>", line 1, in template
jinja2.exceptions.TemplateSyntaxError: expected token 'end of print statement', got 'e'
@davidism

This comment has been minimized.

Copy link
Member

@davidism davidism commented Oct 16, 2018

Jinja only supports decimal notation. The lexer would need to be updated to recognize scientific and other notations. I'm not sure the added complexity is necessary for templates. Is there a specific reason you need this?

@jason-s

This comment has been minimized.

Copy link
Author

@jason-s jason-s commented Oct 16, 2018

I'm doing some log plots in HTML (base64 small png files, with a Python class LinePlotter I wrote to do the plots using matplotlib). Existing Jinja restrictions force me to use this in decimal notation, which suffers in readability for large magnitude values. Here's some sample content from a template I have:

{% set lineplotter = LinePlotter(width=7, dpi=90, xlimits=[0.01, 10000]) %}
{% for k,title,units,scaling in [
	('R','Resistance (R)','ohms (line-line)',2.0),
	('Lq','Inductance, q-axis (Lq)','mH (line-line)',2000.0),
	('Ld','Inductance, d-axis (Ld)','mH (line-line)',2000.0),
	('Ke','Back-emf (Ke)','Vrms/KRPM (line-line)',1.0/0.007797),
	('Tf','Friction (Tf)','mNm',1000.0),
	('B','Viscous damping (B)','uNm/(rad/s)',1000000.0),
	('J','Inertia (J)','uNm/(rad/s<sup>2</sup>)',1000000.0),
	('tau_e','Electrical time constant L/R','ms',1000.0),
	('tau_m1','Mechanical time constant 2/3 JR/Ke<sup>2</sup>','ms',1000.0)
] %}
{% set values = sample_motors['parameters'][k] * scaling %}

It would be more readable, and easier to avoid future errors during maintenance, for me to use 1.0e6 than 1000000.0, or to use xlimits=[1.0e-2, 1.0e4] than xlimits=[0.01, 10000.0].

These values aren't that large in magnitude, but in the engineering world we occasionally get into the 1e-12 range (pico) or the 1e9/1e12 (giga/tera) range. Decimal literals are prone to misreading/human error in that range.

@davidism

This comment has been minimized.

Copy link
Member

@davidism davidism commented Oct 16, 2018

That seems like processing that should be done in Python, passing the data to render.

@jason-s

This comment has been minimized.

Copy link
Author

@jason-s jason-s commented Oct 16, 2018

I expected you to comment that.

No, it doesn't make sense, because I have parameters that are configured in the template for various situations. Other templates look at other aspects of the data model in different ways that are customizable by using different templates. My Python script is responsible for setting up the data model, and different templates show different views of it. Configuring the range of values shown on a log plot is well within the range of responsibility for the template. Yes, I could move it to a config file, and then have Python handle that part, but I use Jinja all the time for quick little scripting tasks like this, and it seems odd that I can do all these amazing things in Jinja templates (like the for loop in my example) but I can't use scientific notation.

I have workarounds, so it's not blocking me from getting things done, but they're ugly. I'm an engineer; scientific notation is one of my basic tools and I can't use it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

2 participants
You can’t perform that action at this time.