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

MAINT: avoid ambiguous list indexing, closes #1413 #1420

Merged
merged 2 commits into from Aug 28, 2018

Conversation

kohr-h
Copy link
Member

@kohr-h kohr-h commented Aug 24, 2018

We had a few places where we explicitly created index lists that shouldn't be lists. The unit tests unearthed a whole bunch of them, but there might be more.
I guess we can go ahead with this (assuming the tests go green) and fix other places that trigger warnings later on.

Copy link
Member

@aringh aringh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

Copy link
Member

@adler-j adler-j left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two minor things, fix and go.

Great catch

indices = (slice(None),) * 2
indices += tuple(n // 2 for n in self.space.shape[2:])
indices = tuple(
[slice(None)] * 2 + [n // 2 for n in self.space.shape[2:]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to create a list, the generator is enough

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't do * 2 or + with a generator, that would require chaining and stuff. This is the simplest implementation, I think.

Copy link
Member

@adler-j adler-j Aug 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use parentheses

Edit: I'm stupid, nope.

@@ -742,13 +748,15 @@ def _apply_padding(lhs_arr, rhs_arr, offset, pad_mode, direction):
bdry_slc_l[axis] = left_slc
bdry_slc_r = list(working_slc)
bdry_slc_r[axis] = right_slc
bdry_slc_l, bdry_slc_r = map(tuple, [bdry_slc_l, bdry_slc_r])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just write one per line

bdry_slc_l = tuple(bdry_slc_l)
bdry_slc_r = tuple(bdry_slc_r)

Slight DRY violation, but it makes the code much more readable and maintainable for non-pythonistas.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do the cases with 2, but the ones with 4 just feel wrong to put in 4 lines. If the functional version is not great, I can also do list comprehension:

# Old
lhs_slc_l, rhs_slc_l, lhs_slc_r, rhs_slc_r = map(
    tuple, [lhs_slc_l, rhs_slc_l, lhs_slc_r, rhs_slc_r])
# New
lhs_slc_l, rhs_slc_l, lhs_slc_r, rhs_slc_r = [
    tuple(lst) for lst in [lhs_slc_l, rhs_slc_l, lhs_slc_r, rhs_slc_r]
]

Or I just write a little inline helper

def to_tuple(*args):
    return tuple(tuple(arg) for arg in args)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For 3 or more, I'm fine with the map approach, its just that for 2 it's barely any reduction.

@kohr-h
Copy link
Member Author

kohr-h commented Aug 28, 2018

Alright, merging after CI.

@kohr-h
Copy link
Member Author

kohr-h commented Aug 28, 2018

Build issue with anaconda and python 2.7 here as well, merging anyway.

@kohr-h kohr-h merged commit a5ca237 into odlgroup:master Aug 28, 2018
@kohr-h kohr-h deleted the issue-1413__tuple_indexing branch August 28, 2018 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants