Recursively traversing a node and its children

33 views
Skip to first unread message

john lunzer

unread,
Jun 19, 2015, 8:57:23 AM6/19/15
to leo-e...@googlegroups.com
I'm trying to create a generator which traverses a node and its children but I'm having trouble, this is what I have as a non-generator:

def traverse(node):

    g.es(node.h,color='green')

    if node.hasChildren():

        for sub_node in node.children_iter():

            traverse(sub_node)


This function will successfully output the present node and its recursive children when traverse(c.p) is executed.


I've tried to throw some yield statements in but i'm just not getting the result I'm looking for, if I put the yield in the for loop it's only getting the top level children.


I think my grasp of generators just isn't what I thought it was, or maybe recursion with generators is more complicated than I thought.


Any insight would be appreciated.

Edward K. Ream

unread,
Jun 19, 2015, 10:22:45 AM6/19/15
to leo-editor
On Fri, Jun 19, 2015 at 7:57 AM, john lunzer <lun...@gmail.com> wrote:
I'm trying to create a generator which traverses a node and its children but I'm having trouble, this is what I have as a non-generator:

​Leo already has all the generators you will likely need.​
 
​ Use ​
p.self_and_subtree().

EKR

john lunzer

unread,
Jun 19, 2015, 1:12:12 PM6/19/15
to leo-e...@googlegroups.com
Ahh, I figured it did but there is so much stuff in Leo that it's hard to track things down. 

I wonder if adding a "generators" section to Leo scripting documentation would be useful?

Edward K. Ream

unread,
Jun 19, 2015, 2:33:01 PM6/19/15
to leo-editor
On Fri, Jun 19, 2015 at 12:12 PM, john lunzer <lun...@gmail.com> wrote:
I wonder if adding a "generators" section to Leo scripting documentation would be useful?

john lunzer

unread,
Jun 19, 2015, 4:41:11 PM6/19/15
to leo-e...@googlegroups.com
Now I really feel stupid!

Edward K. Ream

unread,
Jun 19, 2015, 8:26:39 PM6/19/15
to leo-e...@googlegroups.com, lun...@gmail.com
On Friday, June 19, 2015 at 3:41:11 PM UTC-5, john lunzer wrote:
Now I really feel stupid!

No harm done.  Leo is quite a mouthful.

EKR
Reply all
Reply to author
Forward
0 new messages