Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31867

Re: Recursive Generator Error?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.03; 'elif': 0.04; 'output': 0.04; 'say,': 0.05; 'subject:Error': 0.07; 'python': 0.09; 'output,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'structure,': 0.09; 'terry': 0.09; 'def': 0.10; ';-)': 0.11; 'discarded.': 0.16; 'expect,': 0.16; 'grouped': 0.16; 'hint:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'specified.': 0.16; 'tables,': 0.16; 'threw': 0.16; 'wrote:': 0.17; 'yield': 0.17; 'jan': 0.18; 'written': 0.20; 'elements': 0.23; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'replace': 0.27; 'header:X-Complaints-To:1': 0.28; 'hash': 0.29; "skip:' 10": 0.30; 'basic': 0.30; 'lists': 0.31; 'code': 0.31; 'print': 0.32; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'version': 0.34; 'entered': 0.34; 'pm,': 0.35; 'table': 0.35; 'subject:?': 0.35; 'there': 0.35; 'received:org': 0.36; 'but': 0.36; "didn't": 0.36; 'anything': 0.36; 'one,': 0.37; 'does': 0.37; 'item': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'visit': 0.64; 'harder': 0.65; 'received:fios.verizon.net': 0.84; 'wish.': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Recursive Generator Error?
Date Sun, 21 Oct 2012 19:59:39 -0400
References <d1dbc8a7-1ec6-474c-96d7-b95a066125dd@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120824 Thunderbird/15.0
In-Reply-To <d1dbc8a7-1ec6-474c-96d7-b95a066125dd@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2608.1350863992.27098.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1350863992 news.xs4all.nl 6942 [2001:888:2000:d::a6]:44634
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31867

Show key headers only | View raw


On 10/21/2012 7:29 PM, David wrote:
> I have a tree-like data structure, the basic elements are hash tables,
> and they are grouped into lists, like [[{'a':1},[{'b':2}]]].
> And I want to flat the lists and visit hash table one by one, like {'a':1}, {'b':2}.
> But my program didn't work as I wish. When it entered the 2nd
> flat_yield, it threw a GeneratorExit. Is there anything wrong?

1. The Python version is not specified.
2. You used 2.x; in 3.3 the code does exactly what I would expect, which 
is to say, nothing. No output, no error, no traceback ;-)
3. The traceback is missing from this post.

> #- - - - - - - - - -
> def flat_yield(tbl_list):
>      for t in tbl_list:
>          if type(t) == type({}):
>              yield t
>          elif type(t) == type([]):
>              flat_yield(t)

4. Think harder about what that expression does.

> a = [[{'a':1},[{'b':2}]]]
> for i in flat_yield(a):
>      print i

Hint: it calls flat_yield, which returns a generator, which is then 
discarded. You might have well written 'pass'.

Solution: use the recursively called generator and recursively yield 
what it yields. Replace 'flat_yield(t)' with

             for item in flat_yield(t):
                 yield item

and the output is what you want.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Recursive Generator Error? David <zhushenli@gmail.com> - 2012-10-21 16:29 -0700
  Re: Recursive Generator Error? Terry Reedy <tjreedy@udel.edu> - 2012-10-21 19:59 -0400
    Re: Recursive Generator Error? David <zhushenli@gmail.com> - 2012-10-21 17:40 -0700
    Re: Recursive Generator Error? David <zhushenli@gmail.com> - 2012-10-21 17:40 -0700
      Re: Recursive Generator Error? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-22 02:36 +0000

csiph-web