I use Jinja2 as a website template engine and all the helper functions used in the templates that I implemented as macros, but for one. This is the Python code:
def arrow_class_from_deg(angle): if angle is None: return '' arrow_directions = [ (0, 'n'), (45, 'ne'), (90, 'e'), (135, 'se'), (180, 's'), (225, 'sw'), (270, 'w'), (315, 'nw'), (360, 'n') ] return min(arrow_directions, key=lambda (ang, _): abs(ang - angle))[1]
It returns the CSS class for the arrow that is closest to the specified corner. This function is used (and will be used) only in templates, so it makes sense to implement it in templates, namely in a macro. However, trying to do this, I noticed that Jinja2 does not seem to support Python lambdas. This is true, and if so, what is the best way to write this function (I hope the loop is not needed here)?
source share