Jinja: Add Bitwise operators

Created on 10 Jul 2013  ·  9Comments  ·  Source: pallets/jinja

For completeness, the bitwise operators should be allowed. Currently, one has to do the following in order to perform a shift on an integer.

{{ (12).__rshift__(other) }}

All 9 comments

Bitwise ops should not be used in templates. Expose functions if you need to.

Wow, that's an opinionated standpoint.

I find myself using jinja with Home Assistant and manipulating RGB colors. I want to render {'r': 255, 'g': 255, 'b': 255} as 0xRRGGBB format but no luck because jinja doesn't come with those handy features.

What is your suggestion to work around this? Implementing functions would indeed work but what if someone wants 0xBBGGRR? Isn't it jinja's job to let the user decide of the rendering?
I agree that a feature like this shouldn't be abused but there are use cases for it.

Sorry if you find this comment harsh, this is not my intention. I face true despair :cry:

You don't need bitwise operators for that. Just use string formatting:

{{ '0x{r:02X}{g:02X}{b:02X}'.format(**color) }}

Thank you, I added a way to convert it back to decimal

{{ '0x{r:02X}{g:02X}{b:02X}'.format(**color) | int(base=16) }}

I notice you use .format instead of the format filter. I'm a bit confused to know what is allowed and what is not allowed in a jinja template. What is the preferred way to do this? When to use one or the other?

Quoting the zen of Python: There should be one-- and preferably only one --obvious way to do it.

Please use IRC or the mailing list for this sort of discussion.

Using the format filter would also be possible:

{{ '0x%(r)02X%(g)02X%(b)02X'|format(**color) }}

The format filter basically just wraps the modulo-operator, enabling Python's old string formatting mechanism. The format method, on the other hand, doesn't even come from jinja2, but is a built-in method of str and unicode objects in Python, and used for the new string formatting mechanism.

In Python, the new mechanism is preferred over the old one, but in jinja2 we like to maintain a stable API. If it wouldn't be for backwards compatibility, I think we might have removed or changed the format filter. I'm not sure what to suggest here, I guess, just use whichever you prefer.

But yeah, this discussion is becoming off-topic, and this probably isn't the right place to continue it.

I'm also in need of bitwise operations - at least comparisons. I have a sensor in HomeAssistant that uses a bitmask value to represent several states in one value. In HomeAssistant I want to split this bitmask value into individual sensor values which would be easy to do with a simple {{ state(sensor.with_bitmaskvalue) & 1 }} as value template for my sensor. Right now it's nearly impossible to do this via a Jinja template - at least according to my research

Sounds like they should expose a custom test such as state(sensor.with_bitmaskvalue) is bitmask 1

sorry for the noise, I just found that they added a custom filter bitwise_and() which does the trick for me

Was this page helpful?
0 / 5 - 0 ratings