2010/7/12 Gerald Britton <[email protected]>:
> First off, why are you using an array?  Not that it's bad or anything,
> but how does it help you where a simple list would not?
>
> Second, what would be nice is if liststore.insert would take an
> interable.  Baring that you could simplify a little like this:
>
> keys = ['v1', 'v1', ..., 'vn']
> for d in data:
>    selt.liststore.insert(0, tuple(d[k] for k in keys))
>
> whether it is worth it or not depends on how many keys you have.

Here's another way, assuming that there
are not too many keys to be looked up:

append = self.liststore.append    # lookup the method once
from operator import itemgetter
v1 = itemgetter('v1')                   # special getter for 'v1'
v2 = itemgetter('v2')                   # ...
v3 = itemgetter('v3')
...
for d in data:
   append((v1(d), v2(d), v3(d), ...))

Yet another approach:  If you can use OrderedDict (new in Python 2.7
and 3.1), you can do this:

append = self.liststore.append
for d in data:
  append(d.values())

>
> 2010/7/12 Pietro Battiston <[email protected]>:
>> Il giorno lun, 12/07/2010 alle 17.48 +0200, Cornelius Kölbel ha scritto:
>>> Dear list,
>>>
>>> I got an array of dictionaries, that I want to add to a GTKListStore,
>>> that is displayed an a treeview.
>>>
>>> This is very slow. I already replaced the liststore.append by
>>> liststore.insert, which is much much faster.
>>> But still, filling the 10.000 values takes about 50 seconds.
>>>
>>> Is there any cool mapping function, to push the array to the liststore?
>>> I used a for loop to iterate over the array...
>>> I also tried while an to pop the array elements...
>>>
>>> It is something like this:
>>>
>>> data: array of dictionaries
>>>
>>> data = array( { 'v1' : 'something', 'v2': 'something else' } ,
>>>               { 'v1' : 'another',   'v2': 'something completely diff' }
>>>               )
>>>
>>>
>>> for d in data:
>>>    self.liststore.insert( 0, ( d.get("v1"), d.get("v2") ....))
>>>
>>>
>>> ...is there a better way than doing a for loop?
>>> ...or a way to not have to interate over the 10.000 dicts?
>>>
>>> ...or is there a cool reading on performance tuning pyton?
>>
>>
>>
>> I really don't know, but... already read
>> http://faq.pygtk.org/index.py?req=show&file=faq13.043.htp
>> ?
>>
>> Pietro
>>
>> _______________________________________________
>> pygtk mailing list   [email protected]
>> http://www.daa.com.au/mailman/listinfo/pygtk
>> Read the PyGTK FAQ: http://faq.pygtk.org/
>
>
>
> --
> Gerald Britton
>



-- 
Gerald Britton
_______________________________________________
pygtk mailing list   [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/

Reply via email to