Re: Recursive structures

2004-12-20 Thread bearophileHUGS
Leif K-Brooks: >http://python.org/doc/current/lib/module-pprint.html#l2h-749 Thank you very much, I see that the function is already there, even with the same name :-) I've seen that it doesn't work for all my tests, like this one with n = 3000: from pprint import isrecursive from time import cl

Re: Recursive structures

2004-12-20 Thread Leif K-Brooks
[EMAIL PROTECTED] wrote: While using some nested data structures, I've seen that I'd like to have a function that tells me if a given data structure contains one or more cyclic references (a way to recognise a cycle in a graph is to do a depth-first search, marking vertices along the way. An alread

Re: Recursive structures

2004-12-20 Thread Steven Bethard
Scott David Daniels wrote: if mytype not in AVOIDITER: try: for item in obj: walks(item, seen) except TypeError: pass try: for key, value in obj.items(): walks(key, seen) # Key might be object w/ hash method

Re: Recursive structures

2004-12-20 Thread bearophileHUGS
Thank you very much Thomas Güttler for you quick answer, but I think your program doesn't contain an algorithm to spot cycles (like the usual cyclic graph algorithm). In my first post there was an assert to spot this problem: l = [0] m = [l, l] print m print isrecursive(m) Gives: [[0], [0]] 1 m

Re: Recursive structures

2004-12-20 Thread Scott David Daniels
Thomas Guettler wrote: code to do the test The following transformation of his code shows how exceptions can be used to make code read more clearly. There are a few issues (such as AVOIDITER) to decide on, and usually you are inquiring about your own data structures (which you'll know more about).

Re: Recursive structures

2004-12-20 Thread Thomas Guettler
Am Mon, 20 Dec 2004 04:22:24 -0800 schrieb bearophileHUGS: > I've seen that I'd like to > have a function that tells me if a given data structure contains one or > more cyclic references Hi, does this help you? from types import * def isrecursive(obj, dict=None): if dict==None: dic

Re: Recursive structures

2004-12-20 Thread Fredrik Lundh
<[EMAIL PROTECTED]> wrote: > (This is a repost from another python newsgroup). > While using some nested data structures, I've seen that I'd like to > have a function that tells me if a given data structure contains one or > more cyclic references (a way to recognise a cycle in a graph is to do >