blackducksoftware / hub-rest-api-python

HUB REST API Python bindings
Apache License 2.0
89 stars 104 forks source link

Why using list comprehensions to fetch from generators? #200

Closed thaljef closed 2 years ago

thaljef commented 2 years ago

At lines 123 and 133 in get_upstream_copyrights.txt a list comprehension is used to fetch all values from the generator returned by get_resource. Why is this? Wouldn't list() accomplish the exact same thing and be easier to read?

gsnyder2007 commented 2 years ago

The generator in get_resource handles paging of results from the REST API so that the caller does not have to include the scaffolding code to manage paging. This makes the client code much simpler than it was with the old bindings using HubInstance.

So, if the calling code needs ALL the results it needs to iterate over the generator until all the results have been accumulated. One way to do this is with a comprehension, and its arguably the best way. If you find a better method please submit a PR.

thaljef commented 2 years ago

So, if the calling code needs ALL the results it needs to iterate over the generator until all the results have been accumulated. One way to do this is with a comprehension, and its arguably the best way.

My question is what makes it the best way. Is it somehow faster or use less memory than calling list()? Or is it somehow more idiomatic or "pythonic"? Sorry I wasn't clear on that.

OffBy0x01 commented 2 years ago

@thaljef looking at the underlying implementation, technically casting to list directly will be faster as it more directly translates to C. This difference might standout on endpoints that return many pages of results i.e. component-vulnerabilities but it is unlikely to make any noticeable difference on version endpoints.

That being said, if you feel strongly enough about this perhaps you could create a PR for it?