Repository navigation
ENH: add support for NEP 35 array constructors (frombuffer, fromfile, fromiter, fromstring and fromfunction) with like=Quantity(...)) - #17128
Conversation
|
Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.
|
|
👋 Thank you for your draft pull request! Do you know that you can use |
…le`, `fromiter`, `fromstring` and `fromfunction`) with `like=Quantity(...))`
a04daf2 to
38de071
Compare
fromfile, fromiter, fromstring and fromfunction) with like=Quantity(...))frombuffer, fromfile, fromiter, fromstring and fromfunction) with like=Quantity(...))
| zero_arg = (0,) * len(shape) | ||
| try: | ||
| out_unit = function(*zero_arg).unit | ||
| except Exception: | ||
| out_unit = UNIT_FROM_LIKE_ARG |
There was a problem hiding this comment.
note to reviewers: I initially thought of doing something similar for fromiter but changed my mind after realizing that consuming an iterator could be destructive in some cases (generators).
mhvk
left a comment
There was a problem hiding this comment.
Looks good! One comment just about worth doing, the others optional.
|
|
||
| @function_helper | ||
| def fromfunction(function, shape, *, dtype=float, **kwargs): | ||
| zero_arg = (0,) * len(shape) |
There was a problem hiding this comment.
I guess this is one function where having it dispatched would be easier... But I like your solution, except I think it should be zero_arg = np.zeros(len(shape), dtype), so that the functions gets passed the right dtype (*zero_arg will still work).
| zero_arg = (0,) * len(shape) | ||
| try: | ||
| out_unit = function(*zero_arg).unit | ||
| except Exception: | ||
| out_unit = UNIT_FROM_LIKE_ARG |
| assert arr.unit == u.s | ||
|
|
||
| def test_fromiter(self): | ||
| it = (i * j for i, j in zip(range(3), range(3), strict=True)) |
There was a problem hiding this comment.
Why this very complicated iterator with two range - could just be it = (i * i for i in range(3))
There was a problem hiding this comment.
I think I wanted to write something different originally but didn't catch this because it doesn't really matter to the test. Anyway, no reason to overcomplicate things indeed !
mhvk
left a comment
There was a problem hiding this comment.
Thanks, looks all good now!
Description
Ref #17124