473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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/mess.../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)

Feb 5 '07 #1
3 1303
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/mess.../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
Feb 5 '07 #2
En Mon, 05 Feb 2007 16:47:43 -0300, metaperl <me******@gmail.comescribi:

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.

--
Gabriel Genellina

Feb 6 '07 #3
On Feb 5, 3:08 pm, Jussi Salmela <tiedon_j...@hotmail.comwrote:
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/mess.../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'>
very slick! thanks!
... print r.next()
f

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

HTH,
It does!
Jussi

Feb 6 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: dover | last post by:
For the code, outputfile << *p << endl; Someone suggests: Don't use endl here; it is flushing output stream every time. Use plain '\n'. What's the difference of using "endl" and "\n" above?...
4
by: Rob Freundlich | last post by:
I have some servlet-generated tabular data that I need to present, so I'm using an HTML Table. In some cases, it can be quite large. I'm flushing the servlet output every N lines to push the data...
7
by: clusardi2k | last post by:
Hello, I have a shared drive on SGI, Linux, and Windows. A second call to fopen doesn't create the file if it has been deleted. I would like to use fopen for its pointer return value to...
15
by: Jiří Paleček | last post by:
Hello, I know the rules for const handling in C++, but I'd like to ask what is the "right" way to use them, eg. when is it appropriate to make a member function const? This came across this...
10
by: Pete | last post by:
I have the following code: <built-in method close of file object at 0xb7cc76e0> The file "temp.html" is created, but it doesn't look like the page at www.python.org. I'm guessing there are...
9
by: Xian | last post by:
Is there a right and proper (tm) way to end a line? Thinking about portability and the mess that the 'wrong' line endings can cause. E.g. std::cout << "Hello World!\n"; or std::cout << "Hello...
5
by: arnuld | last post by:
this is from mentioned section. i did not understand some things here: it means "flushing the buffer" and "writing to output device" are SAME thing, these are just 2 different names for the...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
9
by: andrew.smith.cpp | last post by:
hi, whts the difference between the std::endl or "\n" ? because both do the same work Thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.