iterate over dict with integers as keys / bug?

61 views
Skip to first unread message

sport4minus

unread,
Apr 29, 2011, 3:29:22 AM4/29/11
to Hyde
hello!

I am trying to set up a portfolio website with a section of events
that are stored in a .yaml file.

hyde 0.8

I wanted to use a structure like

2011:
-
event_title: "one event"
event_location: "a city"

-
event_title: "one event"
event_location: "a city"

2010:
-
event_title: "one event"
event_location: "a city"

-
event_title: "one event"
event_location: "a city"

but there was no way of iterating over that dict, obviously.

first try :

{% for year, eventlist in events %}

throws me "int object not iterable" error.

after some trial and error I flattened the structure of the yaml file

-
event_title: "one event"
event_location: "a city"
year: "2011"

-
event_title: "one event"
event_location: "a city"
year: "2010"

and use

{% for year_groups in events|groupby('year')|sort(True) %}
<h3>{{year_groups.grouper}}</h3>
{% for event in year_groups.list%}
<p> {{event.title}}, {{event.location}}</p>
{% endfor %}
{% endfor %}

which has the desired effect.

note how i cast the year values to strings in the yaml by using "".
if i don't, i get the int / iterating error again.

the other way around, if i use strings for the years in the first
version, i get no results.

is this a general bug, or am i just confusing jinja and django syntax
here?

Lakshmi Vyas

unread,
Apr 29, 2011, 4:07:11 AM4/29/11
to hyde...@googlegroups.com
Hi,

Yes, integer keys are an issue. This works for me though:

https://gist.github.com/948017

Thanks
Lakshmi

> --
> You received this message because you are subscribed to the Google Groups "Hyde" group.
> To post to this group, send email to hyde...@googlegroups.com.
> To unsubscribe from this group, send email to hyde-dev+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/hyde-dev?hl=en.
>

sport4minus

unread,
Apr 29, 2011, 5:30:42 AM4/29/11
to Hyde
Thanks! i'll use it that way!

and also thanks for hyde :D
cheers
Jens

sport4minus

unread,
Apr 29, 2011, 5:53:45 AM4/29/11
to Hyde
well, i tried, now i am getting the error "too many values to unpack".
seems that n your case, the key/value pair is returned as a tuple, but
not in my case.
did i miss some settings?



On Apr 29, 10:07 am, Lakshmi Vyas <lakshmi.v...@gmail.com> wrote:

sport4minus

unread,
Apr 29, 2011, 6:03:11 AM4/29/11
to Hyde
okay, circling in on that one.

i have my events in a events.yaml file which i use by referring to it
in site.yaml under "providers"

this setup throws the error.

if i put the events inline in the meta of the resource, everything
works as you suggested.

seems that there is some processing happening inbetween which treats
the data differently.

should i file this as an issue?

Lakshmi Vyas

unread,
Apr 29, 2011, 3:16:01 PM4/29/11
to hyde...@googlegroups.com
Hi,

Looks like an inconsistency in the way providers are handled. Would really appreciate if you could file an issue. Thanks.

`{% for year, event_list in events.items() %}`

The extra `items()` call should get your code working. Will fix the inconsistency in 0.9

Thanks so much
Lakshmi.

Reply all
Reply to author
Forward
0 new messages