Skip to content

Add keep_attrs to reduction methods #141

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

Merged
merged 4 commits into from
May 22, 2014
Merged

Add keep_attrs to reduction methods #141

merged 4 commits into from
May 22, 2014

Conversation

jhamman
Copy link
Member

@jhamman jhamman commented May 21, 2014

fixes #138

This is a much cleaner version of #139.

# Test dropped attrs
vm = self.va.mean()
self.assertEqual(len(vm.attrs), 0)
self.assertTrue(utils.dict_equal(vm.attrs, OrderedDict()))
Copy link
Member

Choose a reason for hiding this comment

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

I would just use assertEqual here, which works perfectly fine for dictionaries (you don't need utils.dict_equal unless you have numpy arrays instead your dicts).

Copy link
Member Author

Choose a reason for hiding this comment

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

Good to know. Thanks.

@jhamman jhamman mentioned this pull request May 21, 2014
@@ -1027,6 +1031,11 @@ def reduce(self, func, dimension=None, **kwargs):
of summarized data and the indicated dimension(s) removed.
"""

if keep_attrs:
Copy link
Member

Choose a reason for hiding this comment

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

Probably better to move it a closer to where attrs is used (at the end of the function).

If you like, you could make it a one-liner:

attrs = self.attrs if keep_attrs else {}

(The attributes dictionary is always copied, so it doesn't matter whether you provide it as an OrderedDict or a normal dict.)

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 like it. Done.

@@ -20,6 +20,7 @@
'var2': ['dim1', 'dim2'],
'var3': ['dim3', 'dim1'],
}
_attrs = {'attr1': 'value1', 'attr2': 2929}
Copy link
Member

Choose a reason for hiding this comment

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

This is another style issue (and you can see the existing test code isn't great at this), but in general I would rather not make global variables. It's more self-contained and obvious. In this case, you could simply move _attrs to inside test_reduce_keep_attrs.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, no problem. I'm generally a fan of keeping fixtures as local as possible. In this case, I think I was matching the existing code style.

@shoyer
Copy link
Member

shoyer commented May 21, 2014

Could you update the signature and documentation of methods like mean, sum, etc?

@shoyer
Copy link
Member

shoyer commented May 22, 2014

The to update the signature of the reduce methods, you would need to update the function inside the _reduce_method class method.

@jhamman
Copy link
Member Author

jhamman commented May 22, 2014

Ok, is this what you mean? Sorry - wasn't tracking with you there.

@shoyer
Copy link
Member

shoyer commented May 22, 2014

Yes, thanks!

shoyer added a commit that referenced this pull request May 22, 2014
Add keep_attrs to reduction methods
@shoyer shoyer merged commit b386872 into pydata:master May 22, 2014
@jhamman jhamman deleted the keep_attrs branch May 22, 2014 00:35
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.

keep attrs when reducing xray objects
2 participants