issue_comments: 242950070
This data as json
html_url | issue_url | id | node_id | user | created_at | updated_at | author_association | body | reactions | performed_via_github_app | issue |
---|---|---|---|---|---|---|---|---|---|---|---|
https://github.com/pydata/xarray/issues/988#issuecomment-242950070 | https://api.github.com/repos/pydata/xarray/issues/988 | 242950070 | MDEyOklzc3VlQ29tbWVudDI0Mjk1MDA3MA== | 1217238 | 2016-08-28T01:15:52Z | 2016-08-28T01:16:13Z | MEMBER | Let me give concrete examples of what this interface could look like. To implement units: ``` python from typing import List, Optional # optional Python 3.5 type annotations @xarray.register_ufunc_variables_attrs_handler def propagate_units(results: List[xarray.Variable], context: xarray.UFuncContext) -> Optional[List[dict]]: if context.func.name in ['add', 'sub']: units_set = set(getattr(arg, 'attrs', {}).get('units') for arg in context.args) if len(units_set) > 1: raise ValueError('not all input units the same: %r' % units_set) units, = units_set return [{'units': units}] else: return [] * len(results) # or equivalently, don't return anything at all ``` Or to (partially) handle
Or to implement
Every time xarray does an operation, we would call all of these registered
Similarly, we would have The downside of this approach is that unlike the way NumPy handles things, this doesn't handle conflicting implementations well. If you try to use two different libraries that register their own global attribute handlers instead of using the context manager (e.g., two different units implementations), things will break, even if the unrelated code paths do not touch. So alternatively to using the registration system, we could support/encourage using a context manager, e.g.,
It's kind of verbose, but certainly useful for libraries that want to be cautious about breaking other code. In general, it's poor behavior for libraries to unilaterally change unrelated code without an explicit opt-in. So perhaps the best approach is to encourage users to always use a context manager, e.g., ``` python import contextlib @contextlib.contextmanager def my_attrs_context(): with xarray.ufunc_variables_attrs_handlers( [always_keep_attrs, add_cell_methods, ...]): yield with my_attrs_context(): result = ds.mean() - 0.5 * (ds.max() - ds.min()) ``` So maybe a subclass based implementation (with a custom attribute like |
{ "total_count": 0, "+1": 0, "-1": 0, "laugh": 0, "hooray": 0, "confused": 0, "heart": 0, "rocket": 0, "eyes": 0 } |
173612265 |