|
List Info
Thread: Printing data from python dictionary
|
|
| Printing data from python dictionary |

|
2007-12-16 09:42:10 |
Hello.
I have a little problem, pleas, help me.
I have data in this structure:
sorted = {'16-10-2007': [<Mikropost "4" -
16-10-2007>], '16-12-2007':
[<Mikropost "2" - 16-12-2007>, <Mikropost
"1" - 16-12-2007>],
'16-08-2007': [<Mikropost "3" -
16-08-2007>]}
Is python-dictionary, this data gets template.
I need print this data as follows:
16-10-2007
- Mikropost "4" - 16-10-2007
16-12-2007
- Mikropost "2" - 16-12-2007
- Mikropost "1" - 16-12-2007
16-08-2007
- Mikropost "3" - 16-08-2007
But:
<ul>
<py:for each="item in sorted">
<li>$</li>
</py:for>
</ul>
Print only sorted.keys()
<py:for each="item in sorted.values()">
print only item values and key
of item is not available :(
Pleas help and sorry for my bad english ...
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Genshi" group.
To post to this group, send email to genshi googlegroups.com
To unsubscribe from this group, send email to
genshi-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/genshi?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Printing data from python dictionary |

|
2007-12-16 12:05:37 |
Hi Martin
On Dec 16, 2007 12:42 PM, martin.stiborsky gmail.com
<martin.stiborsky gmail.com> wrote:
>
> Hello.
> I have a little problem, pleas, help me.
>
> I have data in this structure:
>
> sorted = {'16-10-2007': [<Mikropost "4" -
16-10-2007>], '16-12-2007':
> [<Mikropost "2" - 16-12-2007>,
<Mikropost "1" - 16-12-2007>],
> '16-08-2007': [<Mikropost "3" -
16-08-2007>]}
>
> Is python-dictionary, this data gets template.
> I need print this data as follows:
>
> 16-10-2007
> - Mikropost "4" - 16-10-2007
>
> 16-12-2007
> - Mikropost "2" - 16-12-2007
> - Mikropost "1" - 16-12-2007
>
> 16-08-2007
> - Mikropost "3" - 16-08-2007
>
> But:
> <ul>
> <py:for each="item in sorted">
> <li>$</li>
> </py:for>
> </ul>
> Print only sorted.keys()
>
> <py:for each="item in sorted.values()">
print only item values and key
> of item is not available :(
Try this:
<py:for each="key, values in
sorted.iteritems()">
<py:for each="value in values">
....
</py:for>
</py:for>
.iteritems() returns an iterator over the (key, value) items
of the dictionary
[]s
Roger
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Genshi" group.
To post to this group, send email to genshi googlegroups.com
To unsubscribe from this group, send email to
genshi-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/genshi?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Printing data from python dictionary |

|
2007-12-16 12:08:14 |
martin.stiborsky gmail.com wrote:
> <py:for each="item in sorted.values()">
print only item values and key
> of item is not available :(
Tip 1: Test with simple print statements, then translate to
Genshi.
Tip 2: Use sorted.items().
Tip 3: Use a nested loop with an inner <ul> for the
values.
Tip 4: The name "sorted" does not make sense since
dicts are not sorted.
Hope this helps.
-- Chris
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Genshi" group.
To post to this group, send email to genshi googlegroups.com
To unsubscribe from this group, send email to
genshi-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/genshi?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Printing data from python dictionary |

|
2007-12-16 14:41:46 |
Roger Demetrescu: thanks for help and know-how, it works
great
Christoph Zwerschke: ok, thanks. Data are
"sorted", Python script sort
select from database in agreement with create_date of item,
it makes
group of item which have identical day of create. Sorry for
my creepy
english
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Genshi" group.
To post to this group, send email to genshi googlegroups.com
To unsubscribe from this group, send email to
genshi-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/genshi?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Printing data from python dictionary |

|
2007-12-16 18:49:26 |
On Dec 16, 2007 5:41 PM, martin.stiborsky gmail.com
<martin.stiborsky gmail.com> wrote:
>
> Roger Demetrescu: thanks for help and know-how, it
works great
Your are welcome
> Christoph Zwerschke: ok, thanks. Data are
"sorted", Python script sort
> select from database in agreement with create_date of
item, it makes
> group of item which have identical day of create. Sorry
for my creepy
> english
I have to agree with Christoph...
Even if you are filling a dictionary in a
"create_date" order, it
doesn't mean you will retrieve its
keys and values in the same order.
>From python 2.5 help on "3.8 Mapping Types -- dict
" :
"""
Keys and values are listed in an arbitrary order which is
non-random,
varies across Python implementations, and depends on the
dictionary's
history of insertions and deletions. If items(), keys(),
values(),
iteritems(), iterkeys(), and itervalues() are called with
no
intervening modifications to the dictionary, the lists will
directly
correspond. This allows the creation of (value, key) pairs
using
zip(): "pairs = zip(a.values(), a.keys())". The
same relationship
holds for the iterkeys() and itervalues() methods:
"pairs =
zip(a.itervalues(), a.iterkeys())" provides the same
value for pairs.
Another way to create the same list is "pairs = [(v, k)
for (k, v) in
a.iteritems()]".
"""
Now let's do some demonstration here, using python shell:
==============
>>> d = dict()
>>> for x in range(5):
d[x] = x
>>> print d.keys()
[0, 1, 2, 3, 4]
==============
It seems we are wrong, right ? That's because we are filling
few
data... and they are all numeric...
Now lets try again:
==============
>>> d = dict()
>>> for x in 'a', 'b', 'c', 'd', 'e':
d[x] = x
>>> print d.keys()
['a', 'c', 'b', 'e', 'd']
==============
See ? Trying to loop by its keys() or iteritems() or items()
would
bring data in different order..
So, how could you do a safe loop by a dictionary key/value,
"sorted"
by its key ?
=========================
>>> order = d.keys()
>>> order
['a', 'c', 'b', 'e', 'd']
>>> order.sort() # sort in-place
>>> order
['a', 'b', 'c', 'd', 'e']
>>> for x in order:
print d[x],
a b c d e
=========================
Now, as Christoph suggested, translate it to Genshi... ;)
[]s
Roger
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Genshi" group.
To post to this group, send email to genshi googlegroups.com
To unsubscribe from this group, send email to
genshi-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/genshi?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Printing data from python dictionary |

|
2007-12-19 08:29:48 |
Roger Demetrescu : thanks you and other, my mistake in
thinking.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Genshi" group.
To post to this group, send email to genshi googlegroups.com
To unsubscribe from this group, send email to
genshi-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/genshi?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Printing data from python dictionary |

|
2007-12-19 11:39:36 |
My solutions ( complete code ) :
Python code ( howewer, not elegant, but functional): http://www.pastebin.
cz/show/2731
Genshi code: http://www.pastebin.
cz/show/2732
Running application using this code is here: http://www.s
tibi.org/blog/all_microposts
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Genshi" group.
To post to this group, send email to genshi googlegroups.com
To unsubscribe from this group, send email to
genshi-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/genshi?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Printing data from python dictionary |

|
2007-12-19 14:02:52 |
Hei Martin
On Dec 19, 2007 3:39 PM, martin.stiborsky gmail.com
<martin.stiborsky gmail.com> wrote:
>
> My solutions ( complete code ) :
>
> Python code ( howewer, not elegant, but functional): http://www.pastebin.
cz/show/2731
> Genshi code: http://www.pastebin.
cz/show/2732
Much better..
> Running application using this code is here: http://www.s
tibi.org/blog/all_microposts
If you don't mind, I have some comments / suggestions...
;)
1) Why are you binding microposts, unsorted_microposts and
sorted_data
to "self" ?
Can't they be just local variables in you method ? If they
aren't
going to be used by any
other method from your class, I'd suggest you get rid of
those "self."
predicates...
And if they indeed will be used by other methods, make sure
you are
accessing those instances member thread-safely...
2) Personally, I'd change the block (note that I've already
removed
the "self." strings):
====
if not item_day in unsorted_microposts.keys():
unsorted_microposts[item_day] = [item]
else:
unsorted_microposts[item_day].append(item)
====
Into:
====
unsorted_microposts.set_default(item_day,
[]).append(item)
====
3) Doing a reverse sorting of your data wouldn't be more
interesting ?
sorted_data.sort(reverse=True)
[]s
Roger
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Genshi" group.
To post to this group, send email to genshi googlegroups.com
To unsubscribe from this group, send email to
genshi-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/genshi?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Printing data from python dictionary |

|
2007-12-19 16:15:10 |
> If you don't mind, I have some comments /
suggestions... ;)
Of course.
> 1) Why are you binding microposts, unsorted_microposts
and sorted_data
> to "self" ?
So, i have little mess in some areas of Python, self is not
necessary
in this case, thanks for question.
> 2) Personally, I'd change the block (note that I've
already removed
> the "self." strings):
>
> ====
> if not item_day in
unsorted_microposts.keys():
> unsorted_microposts[item_day] =
[item]
> else:
>
unsorted_microposts[item_day].append(item)
> ====
>
I don't understand this.
> Into:
>
> ====
>
unsorted_microposts.set_default(item_day, []).append(item)
> ====
>
> 3) Doing a reverse sorting of your data wouldn't be
more interesting ?
Yes, absolutely, this has been in contemplation.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Genshi" group.
To post to this group, send email to genshi googlegroups.com
To unsubscribe from this group, send email to
genshi-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/genshi?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Printing data from python dictionary |

|
2007-12-20 06:42:05 |
On Dec 19, 2007 8:15 PM, martin.stiborsky gmail.com
<martin.stiborsky gmail.com> wrote:
> > 2) Personally, I'd change the block (note that
I've already removed
> > the "self." strings):
> >
> > ====
> > if not item_day in
unsorted_microposts.keys():
> >
unsorted_microposts[item_day] = [item]
> > else:
> >
unsorted_microposts[item_day].append(item)
> > ====
> >
>
> I don't understand this.
> > Into:
> >
> > ====
> >
unsorted_microposts.set_default(item_day, []).append(item)
> > ====
You can read "x = dictionary.set_default(key,
default_value)" as:
.........
if key not in dictionary.keys():
dictionary[key] = default_value
x = dictionary[key]
.........
In your code, default_value will be an empty list... it
means your "x"
will always be a list... so you could safely call
x.append(something)...
Since your "x" list will be used just once, you
can turn it into:
dictionary.set_default(key, []).append(something)
Take a look at your python help on "set_default"
method from dictionary.
Hope that helps...
[]s
Roger
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Genshi" group.
To post to this group, send email to genshi googlegroups.com
To unsubscribe from this group, send email to
genshi-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/genshi?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
[1-10]
|
|