-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
nodes: add Node.iterchain()
function
#11801
Conversation
471adac
to
e798d59
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, the only wart in my opinion is that the names are nearly identical, in fact I would say the only difference between them based on the names only, is that one returns a list, other an iterator, but the same contents. I'm sure I won't remember which one returns what and would need to look it up every time.
If we were designing this methods now, I would suggest iter_upwards
and iter_downwards
, both returning iterators. Perhaps it is worth to introduce these two methods, and keep listchain
implemented as list(self.iter_downwards())
just for backward compatibility?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it leaves a bit of a sour taste that listchain and iterchain have different order,
but its hard to name those
perhaps we could call it iterparents() but thats also not the nicest name
|
||
def listchain(self) -> List["Node"]: | ||
"""Return a list of all parent collectors up to and including self, | ||
starting from the root of the collection tree.""" | ||
chain = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
while at it maybe also add
chain = [] | |
chain = [*self.iterchain()] | |
chain.reverse() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On my machine (Python 3.11), for a length 7 chain, the existing implementation takes ~0.4 microseconds and reusing iterchain takes ~0.7 microseconds.
I vaguely recall listchain
showing up in profiles so I decided to keep the faster implementation. If you think it's not worth it, I can change it as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, in that case let's skip until we get better profiles
Thanks for the detail
About |
Ahh of course. But the point about |
e798d59
to
69dba81
Compare
Options I thought of:
I do however think that 3 is somewhat preferable to the |
I agree. We might also decide to always return an |
This is a useful addition to the existing `listchain`. While `listchain` returns top-to-bottom, `iterparents` is bottom-to-top and doesn't require an internal full iteration + `reverse`.
69dba81
to
5bd5b80
Compare
I tried 3 and also 2 and they both end up too complex with the overloads and generator/list mixing, I don't like them. So I kept the separate function as is, but changes its name to I also found another place to use it that I had missed before ( |
This is a useful addition to the existing
listchain
. Whilelistchain
returns top-to-bottom,iterchain
is bottom-to-top and doesn't require an internal full iteration +reverse
.Follow up on #11785 (comment)