Recursively enumerate all members of `root`. Similar to the Python library function `os.path.walk`. Traverses the tree of Python objects starting with `root`, depth first. Parent-child relationships in the tree are defined by membership in modules or classes. The function `visit` is called
(root, visit)
| 64 | |
| 65 | |
| 66 | def traverse(root, visit): |
| 67 | """Recursively enumerate all members of `root`. |
| 68 | |
| 69 | Similar to the Python library function `os.path.walk`. |
| 70 | |
| 71 | Traverses the tree of Python objects starting with `root`, depth first. |
| 72 | Parent-child relationships in the tree are defined by membership in modules or |
| 73 | classes. The function `visit` is called with arguments |
| 74 | `(path, parent, children)` for each module or class `parent` found in the tree |
| 75 | of python objects starting with `root`. `path` is a string containing the name |
| 76 | with which `parent` is reachable from the current context. For example, if |
| 77 | `root` is a local class called `X` which contains a class `Y`, `visit` will be |
| 78 | called with `('Y', X.Y, children)`). |
| 79 | |
| 80 | If `root` is not a module or class, `visit` is never called. `traverse` |
| 81 | never descends into built-in modules. |
| 82 | |
| 83 | `children`, a list of `(name, object)` pairs are determined by |
| 84 | `tf_inspect.getmembers`. To avoid visiting parts of the tree, `children` can |
| 85 | be modified in place, using `del` or slice assignment. |
| 86 | |
| 87 | Cycles (determined by reference equality, `is`) stop the traversal. A stack of |
| 88 | objects is kept to find cycles. Objects forming cycles may appear in |
| 89 | `children`, but `visit` will not be called with any object as `parent` which |
| 90 | is already in the stack. |
| 91 | |
| 92 | Traversing system modules can take a long time, it is advisable to pass a |
| 93 | `visit` callable which blacklists such modules. |
| 94 | |
| 95 | Args: |
| 96 | root: A python object with which to start the traversal. |
| 97 | visit: A function taking arguments `(path, parent, children)`. Will be |
| 98 | called for each object found in the traversal. |
| 99 | """ |
| 100 | _traverse_internal(root, visit, [], '') |
nothing calls this directly
no test coverage detected