in Markup, python

Stuff I learned over the weekend

1. Finding stuff in lists of dicts in Python

If you have a list of dicts in Python like this:

items = [{"id":1, "name": "Rasmus"}, {"id": 2, "name": "Rick"}]

…and want to check if there are any dict items with an id of a specific value (e.g. 2) I first came up with this:

2 in map(lambda x: x['id'], items)

The downside is that it will loop all items which may be inefficient for larger lists. Niklas eventually pointed me at Python’s any() function which I managed to miss while googling for other ways. Thus, a nicer version is:

any(item['id'] == 2 for item in items)

Handy for checking stuff in Django object collections.

2. YUI Grids looks difficult but is easy

I decided to scrap the YAML based layout of a site I made a while ago when I discovered the licensing agreement (requires backlinks for the free version). I turned to YUI Grids which I had a look at a long time ago, but didn’t use because it looked too complex. Maybe it is just the layout of the YUI grids website (massive amounts of text), but it felt complicated at a first glance.

Coming back to YUI Grids this time I skipped the documentation and went straight for the examples. It is simple as soon as you understand grids (yui-g) and units (yui-u). I managed to get the basic layout going in an hour. Thank you YUI team!

3. Django’s template language is heavily opinionated

They really, really don’t want you to write anything but pure template code in there. Which is good, I guess. I only tried doing some basic addition. Sorry.

Write a Comment here on the real web

Comment