-
Notifications
You must be signed in to change notification settings - Fork 280
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
Clarify some utility functions (yt.funcs
) with more_itertools
#2893
Conversation
a724329
to
71b9b3f
Compare
yt.funcs
) with more_itertools
In general I like this; I was initially opposed but this is indeed cleaner. Will review this. Every time one of these refactors pops up it makes me long for the opportunity to deeply refactor the field system. |
Yeah I mean I started this as an exercise to learn about more_itertools but in the end I find it's a good (small) step forward towards the fields refactor. :) |
15eadfe
to
c088865
Compare
@yt-fido test this please |
Hm. The doc build seems to be idle (previous run timed out too). @Xarthisius, do you have any clue what's happening here ? |
No idea, here's the error:
and
|
I think it's due to |
@yt-fido test this please |
c102147
to
9317d23
Compare
9317d23
to
8adface
Compare
c16b4ab
to
ebf0243
Compare
def ensure_tuple(obj): | ||
""" | ||
This function ensures that *obj* is a tuple. Typically used to convert | ||
This function ensures that *obj* is a tuple. Typically used to convert | ||
scalar, list, or array arguments specified by a user in a context where | ||
we assume a tuple internally | ||
""" | ||
if isinstance(obj, tuple): | ||
return obj | ||
elif isinstance(obj, (list, np.ndarray)): | ||
return tuple(obj) | ||
else: | ||
return (obj,) | ||
return tuple(always_iterable(obj)) |
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.
since we have this update of ensure_tuple() that does return tuple(always_iterable(obj))
, is it worth adding back the ensure_list()
functionality that acts as a sanitizer and and use that in the code for consistency?
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.
actually my plan was to get rid of both. As far as I recall, ensure_tuple
is only ever used to sanitize Dataset.perdiodicity
attributes within the code base, and what's more is that I think it's only ever done in the Boxlib frontend, so I would rather just get this one down that reintroduce its sibling. Does this sound fair to you ?
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.
(sorry about not replying to your initial comment on this btw, it slipped my mind so it seems.)
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.
(sorry about not replying to your initial comment on this btw, it slipped my mind so it seems.)
Not a problem! I just wanted to make sure it didn't get lost!
I would rather just get this one down that reintroduce its sibling. Does this sound fair to you ?
I think that's totally fine. 🙂 I was just curious for consistency reasons.
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'll remove ensure_tuple
in a followup PR !
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.
SGTM!
@neutrinoceros just a note--I should have been following this more closely, but |
Thanks for reporting back. I honestly had no idea the content of yt.funcs could be a dependence downstream. I am sorry about this breakage. It should be easier to manage transitional deprecations and removals in the future now (see https://github.com/yt-project/yt/pulls). |
What do you think would be a good solution to remedy this @jzuhone ? Should we add it back in with a deprecation warning? |
PR Summary
Refactor some internals using
more_itertools
(which is already a transitive dependency).remove
yt.funcs.ensure_list
this function has two use cases
ensure_list
is bit clunky and requires maintenance on our part, the more powerfulmore_itertools.always_iterable
does the job.lambda x: list(always_iterable(x))
When we specifically need to iterate transparently over a arbitrary number of fields,
always_iterable
doesn't work out of the box since a single 2-tuple should be treated as a single field, but it's easy to configure, so I added a simpleyt.funcs.iter_fields
function:rename
yt.funcs.iterable
This function's name is confusing because it doesn't indicate that it's really a checker that returns a boolean (
is_iterable
would be clearer in that sense). On top of this it is also wrong because it actually only checks thatlen(obj)
is valid, while some iterables have no length (e.g. infinite generators).I renamed the existing function to
is_sequence
.other associated renames
yt.funcs.validate_iterable
->validate_sequence
yt.geometry.coordinates.coordinate_handler.validate_sequence_width
->validate_sequence_width
rework
yt.funcs.ensure_tuple
andyt.funcs.just_one
Each can be simplified as combination of two functions from
more_itertools
.Additionally,
ensure_tuple
is now only ever used to validateds.periodicity
, so there is some overlapping with #2868 and the functionality could be moved to theDataset
class directly.This PR refactors the one place that
ensure_tuple
was used differently, namelyyt.frontends.enzo_p.misc.nested_dict_get
.I also fixed a bug (and added a test for it) in this function while I was at it.
PR Checklist
iterable
tois_sequence
iter_fields
functionensure_list
calls with eitheriter_fields
where an iterator is needed,list(iter_fields())
where we actually need a list of fields, andlist(always_iterable())
elsewhere.ensure_list
ensure_tuple
calls withtuple(always_iterable())
(or update ensure_tuple to the same effect ?)just_one
black --check yt/
isort . --check --diff
flake8 yt/
flynt yt/ --fail-on-change --dry-run -e yt/extern