"flushing"/demanding generator contents - implications for injection of control

M

metaperl

For this program:

def reverse(data):
for index in range(len(data)-1, -1, -1):
yield data[index]

r = reverse("golf")

for char in r:
print char


I'm wondering if the line:

r = reverse("golf")

"demands" the contents of the function reverse() all at once and if I
must write

for char in reverse("golf"):
print char

if I want the results streamed instead of generated complely.

** CONTEXT **

The simple example above is not what I am really doing. My real
program parses very large
data files using pyparsing. Pyparsing can generate incremental/yielded
results with no problem:

http://pyparsing.wikispaces.com/message/view/home/248539#248852

but because I believe in injection of control (pushing data around as
opposed to having
the target pull it), I get the parse and then inject it into the
generator:

parse = parsing.parse(fp.read())
txt = textgen.generate(self.storage.output, patent_key,
parse, f.basename(), debug=False)
 
J

Jussi Salmela

metaperl kirjoitti:
For this program:

def reverse(data):
for index in range(len(data)-1, -1, -1):
yield data[index]

r = reverse("golf")

for char in r:
print char


I'm wondering if the line:

r = reverse("golf")

"demands" the contents of the function reverse() all at once and if I
must write

for char in reverse("golf"):
print char

if I want the results streamed instead of generated complely.

** CONTEXT **

The simple example above is not what I am really doing. My real
program parses very large
data files using pyparsing. Pyparsing can generate incremental/yielded
results with no problem:

http://pyparsing.wikispaces.com/message/view/home/248539#248852

but because I believe in injection of control (pushing data around as
opposed to having
the target pull it), I get the parse and then inject it into the
generator:

parse = parsing.parse(fp.read())
txt = textgen.generate(self.storage.output, patent_key,
parse, f.basename(), debug=False)
I don't know, I'm guessing:

... r = reverse("golf")
... type(r)
<type 'generator'>
... print r.next()
f

So r is not the string 'flog', it is the generator producing it

HTH,
Jussi
 
G

Gabriel Genellina

For this program:

def reverse(data):
for index in range(len(data)-1, -1, -1):
yield data[index]

r = reverse("golf")

for char in r:
print char


I'm wondering if the line:

r = reverse("golf")

"demands" the contents of the function reverse() all at once and if I
must write

for char in reverse("golf"):
print char

if I want the results streamed instead of generated complely.

reverse is a generator; it executes one step at a time. That's the whole
point of generators.
 
M

metaperl

metaperl kirjoitti:
For this program:
def reverse(data):
for index in range(len(data)-1, -1, -1):
yield data[index]
r = reverse("golf")
for char in r:
print char
I'm wondering if the line:
r = reverse("golf")
"demands" the contents of the function reverse() all at once and if I
must write
for char in reverse("golf"):
print char
if I want the results streamed instead of generated complely.
** CONTEXT **
The simple example above is not what I am really doing. My real
program parses very large
data files using pyparsing. Pyparsing can generate incremental/yielded
results with no problem:

but because I believe in injection of control (pushing data around as
opposed to having
the target pull it), I get the parse and then inject it into the
generator:
parse = parsing.parse(fp.read())
txt = textgen.generate(self.storage.output, patent_key,
parse, f.basename(), debug=False)

I don't know, I'm guessing:

... r = reverse("golf")
... type(r)
<type 'generator'>

very slick! thanks!
... print r.next()
f

So r is not the string 'flog', it is the generator producing it

HTH,

It does!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top