-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request - Node Tree from list of delimited strings #153
Comments
You mean like this?
|
That looks like it'll work. I think I long ago found a work around to my issue above but I was hoping that this could become a more easily used feature in future releases. That way you don't have to write your own function to do it, even if it is a pretty simple (in hindsight) function. |
Something more general that I would like would be a function to turn a list like this: l = [('Europe", "Italy", "Rome"),
('Europe", "Italy", "Milan"),
('Europe", "France", "Paris")] into a tree:
The reason is that I have many hierarchies stored in csv files and with pandas and such a function I can easily convert them to trees. |
Here are sample implementations for my ideas above: def from_rows(rows, node_factory=anytree.Node, root_name="root"):
created_nodes = {}
root = node_factory(root_name)
for row in rows:
parent_node = root
for depth, col in enumerate(row):
if (depth, col) in created_nodes:
node = created_nodes[depth, col]
else:
node = node_factory(col)
node.parent = parent_node
created_nodes[depth, col] = node
parent_node = node
return root
def to_rows(root, str_factory=str, skip_root=True):
index = 1 if skip_root else 0
for leaf in root.leaves:
yield [str_factory(node) for node in leaf.path[index:]] Update 2024-01-06: |
I've got some files I'm trying to parse into a tree (a FEM assembly tree actually) and need to take a list of lists and create a tree form it. Because of the way I get the raw input that I'm parsing (TCL script) all the lists have the full path, much of which is repeated in each list. I'm sure I could get this working eventually without a 'batch node creator' but I wanted to throw this out there anyways for consideration that such a batch node creator be added in the future.
Below is a (hopefully understandable) minimum example copied and pasted from a markdown export of a Jupyter notebook. I think this request might be similar to others made previously and if so feel free to close this one and/or link it to other issues.
Sorry for the book-length post!!
Make the Tree Manually w/ anytree
Parse the TCL Script Output File
Looks something like this (
~
delimited; contents pasted here so as to not includeExampleTree_featureRequest_raw.txt
):Read in the raw TCL Output File
Parse the content into list of lists
content
Desired Feature
A way to 'batch create' a node tree. Some command that will take in a list of delimited node-childNodes-etc. and create a valid Node object from it.
Example:
The above should produce the same result as doing it by hand:
Related Issues
Open
Closed
Node.path_to
method?Semi-Related Issues
Open
Closed
children=[...]
stuffThe text was updated successfully, but these errors were encountered: