This works exactly as you would expect::
from time import sleep
def foo(on='ABC'):
for e in list(on):
sleep(1)
yield e
When I run this on the command line It takes about 3 seconds to
complete and the first letter is shown after 1 second.
But, how do I wrap the function somewhere else::
from time import sleep
def foo(on):
for e in list(on):
sleep(1)
yield e
def wrapper(x):
if x < 0:
return foo('ABC')
else:
return foo('XYZ')
When I run this, variable three letters are shown and it takes 3
seconds for the whole thing to complete. The problem is that the whole
iteration is glogged up in the wrapper() function because the first
letter is shown after 3 seconds and then all letters are shown at the
same time.
How do I wrap functions that return iterators? ...if possible. 4 1971 pe*****@gmail.c om wrote: from*time*impor t*sleep def*foo(on): for*e*in*list(o n):
Just
for e in on:
#...
is better. The list() constructor defeats much of the laziness you gain by
using a generator.
sleep(1) yield*e
def*wrapper(x): if*x*<*0: return*foo('ABC ') else: return*foo('XYZ ')
When I run this, variable three letters are shown and it takes 3 seconds for the whole thing to complete. The problem is that the whole iteration is glogged up in the wrapper() function because the first letter is shown after 3 seconds and then all letters are shown at the same time.
How do I wrap functions that return iterators? ...if possible.
The code you presented is OK. Your diagnosis is probably the result of a of
a misconstrued test case. Something like
for s in wrapper(1):
print s,
seems to work like you described because the text is buffered until the line
is complete. Try
for s in wrapper(1):
print s
or
import sys
for s in wrapper(1):
print s,
sys.stdout.flus h() # explicitly flush the buffer
instead.
Peter pe*****@gmail.c om wrote: from*time*impor t*sleep def*foo(on): for*e*in*list(o n):
Just
for e in on:
#...
is better. The list() constructor defeats much of the laziness you gain by
using a generator.
sleep(1) yield*e
def*wrapper(x): if*x*<*0: return*foo('ABC ') else: return*foo('XYZ ')
When I run this, variable three letters are shown and it takes 3 seconds for the whole thing to complete. The problem is that the whole iteration is glogged up in the wrapper() function because the first letter is shown after 3 seconds and then all letters are shown at the same time.
How do I wrap functions that return iterators? ...if possible.
The code you presented is OK. Your diagnosis is probably the result of a of
a misconstrued test case. Something like
for s in wrapper(1):
print s,
seems to work like you described because the text is buffered until the line
is complete. Try
for s in wrapper(1):
print s
or
import sys
for s in wrapper(1):
print s,
sys.stdout.flus h() # explicitly flush the buffer
instead.
Peter
On 18 Nov 2005 05:08:39 -0800, "pe*****@gmail. com" <pe*****@gmail. com> wrote: This works exactly as you would expect::
from time import sleep def foo(on='ABC'): for e in list(on): sleep(1) yield e
When I run this on the command line It takes about 3 seconds to complete and the first letter is shown after 1 second. But, how do I wrap the function somewhere else::
from time import sleep def foo(on): for e in list(on): sleep(1) yield e
def wrapper(x): if x < 0: return foo('ABC') else: return foo('XYZ')
When I run this, variable three letters are shown and it takes 3 seconds for the whole thing to complete. The problem is that the whole iteration is glogged up in the wrapper() function because the first letter is shown after 3 seconds and then all letters are shown at the same time.
How do I wrap functions that return iterators? ...if possible.
Make the wrapper itself an iterable? E.g., is this the effect you wanted? from time import sleep def foo(on):
... for e in on:
... sleep(1)
... yield e
... def wrapper(x):
... if x < 0:
... for e in foo('ABC'): yield e
... else:
... for e in foo('XYZ'): yield e
... wrapper(-1)
<generator object at 0x02EF3D8C> import sys for c in wrapper(-1): sys.stdout.writ e(c); sys.stdout.flus h()
...
ABC>>> for c in wrapper(+1): sys.stdout.writ e(c); sys.stdout.flus h()
...
XYZ>>>
Regards,
Bengt Richter
On 18 Nov 2005 05:08:39 -0800, "pe*****@gmail. com" <pe*****@gmail. com> wrote: This works exactly as you would expect::
from time import sleep def foo(on='ABC'): for e in list(on): sleep(1) yield e
When I run this on the command line It takes about 3 seconds to complete and the first letter is shown after 1 second. But, how do I wrap the function somewhere else::
from time import sleep def foo(on): for e in list(on): sleep(1) yield e
def wrapper(x): if x < 0: return foo('ABC') else: return foo('XYZ')
When I run this, variable three letters are shown and it takes 3 seconds for the whole thing to complete. The problem is that the whole iteration is glogged up in the wrapper() function because the first letter is shown after 3 seconds and then all letters are shown at the same time.
How do I wrap functions that return iterators? ...if possible.
Make the wrapper itself an iterable? E.g., is this the effect you wanted? from time import sleep def foo(on):
... for e in on:
... sleep(1)
... yield e
... def wrapper(x):
... if x < 0:
... for e in foo('ABC'): yield e
... else:
... for e in foo('XYZ'): yield e
... wrapper(-1)
<generator object at 0x02EF3D8C> import sys for c in wrapper(-1): sys.stdout.writ e(c); sys.stdout.flus h()
...
ABC>>> for c in wrapper(+1): sys.stdout.writ e(c); sys.stdout.flus h()
...
XYZ>>>
Regards,
Bengt Richter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: km |
last post by:
Hi all,
i didnt understand the purpose of 'yield' keyword and the concept of 'generators' in python. can someone explain me with a small example how generators differ from normal function calls?
kindly enlighten
regards,
KM
|
by: Egil M?ller |
last post by:
Is there any way to create transparent wrapper objects in Python?
I thought implementing __getattribute__ on either the wrapper class or
its metaclass would do the trick, but it does not work for the built
in operators:
class Foo(object):
class __metaclass__(type):
def __getattribute__(self, name):
print "Klass", name
|
by: IntraRELY |
last post by:
I know this isnt really a VB question per say, but is what I am developing
in VB.NET and Excel is the only place that has provided direction. I wanted
to ask the public if perhaps you could lend me a hand a point me into the
right direction if there is someone who has had experiance with this. By all
means if you know a better place to ask, let me know.
There is an excel help file titled YIELD. I will copy any paste the file
below. I am...
|
by: peterbe |
last post by:
This works exactly as you would expect::
from time import sleep
def foo(on='ABC'):
for e in list(on):
sleep(1)
yield e
When I run this on the command line It takes about 3 seconds to
complete and the first letter is shown after 1 second.
|
by: andy.leszczynski |
last post by:
Hi,
I might understand why this does not work, but I am not convinced it
should not - following:
def nnn():
print 'inside'
yield 1
def nn():
| |
by: Mateuszk87 |
last post by:
Hi.
may someone explain "yield" function, please. how does it actually work
and when do you use it?
thanks in forward
mateusz
|
by: Ehsan |
last post by:
hi
coulde any one show me the usage of "yield" keyword specially in this
example:
"""Fibonacci sequences using generators
This program is part of "Dive Into Python", a free Python book for
experienced programmers. Visit http://diveintopython.org/ for the
latest version.
|
by: castironpi |
last post by:
What if I say
oath= yield
or
other= yield
?
|
by: Alex Bryan |
last post by:
Okay, so i don't really understand the Yield thing and i know it is
useful. I've read a few things about it but it is all programming
jargon and so basically it is hard for me to understand. So can anyone
give me a description or link me to a site that has a good definition
and/or examples of it? If you could I would really appreciate it.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |