I'm trying to write a 'flatten' generator which, when give a
generator/iterator that can yield iterators, generators, and other data
types, will 'flatten' everything so that it in turns yields stuff by
simply yielding the instances of other types, and recursively yields the
stuff yielded by the gen/iter objects.
To do this, I need to determine (as fair as I can see), what are
generator and iterator objects. Unfortunately:
>>iter("abc")
<iterator object at 0x61d90>
>>def f(x):
.... for s in x: yield s
....
>>f
<function f at 0x58230>
>>f.__class__
<type 'function'>
So while I can identify iterators, I can't identify generators by class.
Is there a way to do this? Or perhaps another (better) way to achieve
this flattening effect? itertools doesn't seem to have anything that
will do it.
Thanks,
Ken 4 1584
Kenneth McDonald wrote:
I'm trying to write a 'flatten' generator which, when give a
generator/iterator that can yield iterators, generators, and other data
types, will 'flatten' everything so that it in turns yields stuff by
simply yielding the instances of other types, and recursively yields the
stuff yielded by the gen/iter objects.
To do this, I need to determine (as fair as I can see), what are
generator and iterator objects. Unfortunately:
>>iter("abc")
<iterator object at 0x61d90>
>>def f(x):
... for s in x: yield s
...
>>f
<function f at 0x58230>
>>f.__class__
<type 'function'>
So while I can identify iterators, I can't identify generators by class.
Is there a way to do this? Or perhaps another (better) way to achieve
this flattening effect? itertools doesn't seem to have anything that
will do it.
Thanks,
Ken
>>def f(x):
.... for s in x: yield s
....
>>f([1])
<generator object at 0x01388E18>
?
- Paddy.
Kenneth McDonald wrote:
I'm trying to write a 'flatten' generator which, when give a
generator/iterator that can yield iterators, generators, and other data
types, will 'flatten' everything so that it in turns yields stuff by
simply yielding the instances of other types, and recursively yields the
stuff yielded by the gen/iter objects.
To do this, I need to determine (as fair as I can see), what are
generator and iterator objects. Unfortunately:
>>iter("abc")
<iterator object at 0x61d90>
>>def f(x):
... for s in x: yield s
...
>>f
<function f at 0x58230>
>>f.__class__
<type 'function'>
So while I can identify iterators, I can't identify generators by class.
But f is not a generator, it's a function returning generator:
>>def f():
.... print "Hello"
.... yield 1
....
>>iter(f)
Traceback (most recent call last):
File "<input>", line 1, in ?
TypeError: iteration over non-sequence
>>iter(f())
<generator object at 0x016C7238>
>>type(f())
<type 'generator'>
>>>
Notice, there is no side effect of calling f function.
-- Leo
Kenneth McDonald schrieb:
To do this, I need to determine (as fair as I can see), what are
Is there a way to do this? Or perhaps another (better) way to achieve
this flattening effect? itertools doesn't seem to have anything that
will do it.
As others have pointed out, there is a proper test for generator
objects; you are apparently interested in finding out whether a
function will produce a generator when called.
To do that, use the following code
def is_generator_fu nction(f):
return (f.func_code.co _flags & 0x20) != 0
Here, 0x20 is the numeric value of CO_GENERATOR (also available
through compiler.consts .CO_GENERATOR).
Regards,
Martin
Kenneth McDonald wrote:
I'm trying to write a 'flatten' generator which, when give a
generator/iterator that can yield iterators, generators, and other data
types, will 'flatten' everything so that it in turns yields stuff by
simply yielding the instances of other types, and recursively yields the
stuff yielded by the gen/iter objects.
To do this, I need to determine (as fair as I can see), what are
generator and iterator objects. Unfortunately:
>>iter("abc")
<iterator object at 0x61d90>
>>def f(x):
... for s in x: yield s
...
>>f
<function f at 0x58230>
>>f.__class__
<type 'function'>
So while I can identify iterators, I can't identify generators by class.
Is there a way to do this? Or perhaps another (better) way to achieve
this flattening effect? itertools doesn't seem to have anything that
will do it.
Thanks,
Ken
Unfortunately, nothing is as easy as it may seem:
>>def is_generator(f) :
.... return f.func_code.co_ flags & CO_GENERATOR != 0
....
>>def f(x):
.... for s in x: yield s
....
>>is_generator( f)
True
>># But look at the following: def f2(x):
.... def g(y):
.... for s in y: yield s
.... return g(x)
....
>>f2([1,2,3])
<generator object at 0x013DEC88>
>>is_generator( f2)
False
>>>
;-)
- Paddy. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Francis Avila |
last post by:
A little annoyed one day that I couldn't use the statefulness of
generators as "resumable functions", I came across Hettinger's PEP 288
(http://www.python.org/peps/pep-0288.html, still listed as open, even
though it's at least a year old and Guido doesn't seem very hot on the
idea). I'm not too sure of its ideas on raising exceptions in
generators from outside (although it looks like it might be convenient
in some cases), but being able...
|
by: Emmanuel |
last post by:
Hi,
I run across this problem, and couldn't find any solution (python 2.2.2)
:
Code :
===========
from __future__ import generators
>>> class titi:
|
by: Joh |
last post by:
hello,
i'm trying to understand how i could build following consecutive sets
from a root one using generator :
l =
would like to produce :
, , , ,
|
by: simonwittber |
last post by:
>>> gen = iterator()
>>> gen.next
<method-wrapper object at 0x009D1B70>
>>> gen.next
<method-wrapper object at 0x009D1BB0>
>>> gen.next
<method-wrapper object at 0x009D1B70>
>>> gen.next
<method-wrapper object at 0x009D1BB0>
>>> gen.next is gen.next
|
by: Jerzy Karczmarczuk |
last post by:
I thought that the following sequence
gl=0
def gen(x):
global gl
gl=x
yield x
s=gen(1)
| |
by: vbgunz |
last post by:
I am afraid that this is the first time in which I would probably need
something explained to me as if I were a little child. I am having a
hard time getting this through my thick skull. What in the world is
wrong with this!?
''' ########################################################### '''
def generatorFunction(sequence=):
for item in sequence:
yield item
|
by: Paul Rubin |
last post by:
As I understand it, generators are supposed to run til they hit a
yield statement:
import time
def f():
print 1
time.sleep(3)
for i in range(2,5):
yield i
|
by: Bernhard Mulder |
last post by:
I have something like the following (using Python 2.5 syntax):
class adapt(object):
def __init__(self):
self.iterator = self.generator()
self.n = 0
def generator(self):
while True:
while True:
|
by: python |
last post by:
Is there an elegant way to unget a line when reading from a file/stream
iterator/generator?
By "unget" I mean a way to push back a line into the source stream and
backtrack the iterator/generator one step?
The only alternative I can see is to put my line reading in a while-True
loop (vs. a for-loop) that manually calls my file/stream
iterator/generator's .next() method while manually handling the
StopIteration exception. Doesn't sound...
|
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: 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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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: 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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |