Skip to content
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

Add hierarchy wildcard ** to Resolver.glob() #4

Closed
Odelya opened this issue Mar 28, 2017 · 4 comments
Closed

Add hierarchy wildcard ** to Resolver.glob() #4

Odelya opened this issue Mar 28, 2017 · 4 comments

Comments

@Odelya
Copy link

Odelya commented Mar 28, 2017

Version 2.1.1 - glob returns only the closest children:

d = Node("parent")
child_1 = Node("child_1", parent=d, value="5")
child_2 = Node("child_2", parent=d, value="5")
grandson_1 = Node("child_1", parent=child_1, value="5")
grandson_2 = Node("child_1", parent=child_1, value="5")
r = Resolver("value")
nodes = r.glob(d, "5")

Will return only child_1 and child_2

@c0fec0de
Copy link
Owner

This is the intended behaviour here. The glob() implementation lacks of a hierarchy wildcard like **. This might be implemented in a future release:

r.glob(d, "**/5")

Just use an iterator with a filter, which does the **/5:

nodes = list(PreOrderIter(d, lambda n: hasattr(n, "value") and n.value == "5"))

Or with wildcard matching:

from fnmatch import fnmatchcase
nodes = list(PreOrderIter(d, lambda n: hasattr(n, "value") and fnmatchcase(n.value, "*5")))

I would change this ticket into a feature request for hierarchy wildcard **.

HTH

@Odelya
Copy link
Author

Odelya commented Mar 28, 2017

Yes, I used iterator instead. in that case - what's the difference between glob and iterator?
Would wild card support any descendant without specifying the amount of ** ?

@c0fec0de
Copy link
Owner

The Resolver() eases the tree interaction on a higher level. The iterator gives you more flexibility.

** will match any descendant.

If you just want to have the first three level, you can invoke glob() multiple times and concatenate the result:

nodes = list(glob('5')) + list(glob('*/5')) + list (glob('*/*/5'))

Anyway. An iterator with a maxlevel is more elegant:

nodes = list(PreOrderIter(d, maxlevel=3, filter_=lambda n: hasattr(n, "value") and n.value == "5"))

@c0fec0de c0fec0de changed the title glob returns only closest children Add hierarchy wildcard **to Resolver.glob() Mar 28, 2017
@c0fec0de c0fec0de changed the title Add hierarchy wildcard **to Resolver.glob() Add hierarchy wildcard ** to Resolver.glob() Mar 28, 2017
@moi90
Copy link
Contributor

moi90 commented Jun 30, 2022

This would be very handy, indeed!

** will match any descendant.

I think, it should also allowed to be empty, i.e. match the root.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants