[Qgis-developer] How to use data on disk in this generator

Martin Dobias wonder.sk at gmail.com
Fri Jan 22 04:33:00 EST 2010


On Wed, Jan 20, 2010 at 7:04 PM, Echavarria Gregory, Maria Angelica
<m.echavarriagregory at umiami.edu> wrote:
> I need to mimic what the following generator does:
>
> def  validList(sumComb, GammaPossible, valid):
>    for l in sumComb:
>       for m in sumComb:
>         for n in sumComb:
>                k = [l,m,n]
>                i = 0
>                while i < valid:
>                     C = 0
>                     j = 0
>                     while j < 3:
>                           C = C +((GammaPossible[i][j])*k[j])
>                           j += 1
>                     yield C
>                     i += 1
>
> By not using the list sumComb in memory, but having its elements in a file F writen using F = open("my_list.txt", "w") and where elements are stored with column "/n" separation. I have not been able to find the appropriate syntax for the nested 'for' loops to replace sumComb list by using F.readlines(), str.split(), etc.  Please help me with this little challenge.

What is your memory problem? Handling few millions of items shouldn't
be a problem nowadays.

Anyway, what about using this generator (instead of using directly
'sumComb' list):

def sumCombGen():
  for line in file("my_list.txt"):
    yield float(line)

For each line in the input file it returns parsed floating point
number. To check the generator:

for i in sumCombGen():
  print "item", i


Regards
Martin


More information about the Qgis-developer mailing list