home / github / issue_comments

Menu
  • Search all tables
  • GraphQL API

issue_comments: 612076605

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/3959#issuecomment-612076605 https://api.github.com/repos/pydata/xarray/issues/3959 612076605 MDEyOklzc3VlQ29tbWVudDYxMjA3NjYwNQ== 14808389 2020-04-10T15:23:08Z 2020-04-10T15:56:08Z MEMBER

you could emulate the availability of the accessors by checking your variables in the constructor of the accessor using ```python dataset_types = { frozenset("variable1", "variable2"): "type1", frozenset("variable2", "variable3"): "type2", frozenset("variable1", "variable3"): "type3", }

def _dataset_type(ds): data_vars = frozenset(ds.data_vars.keys()) return dataset_types[data_vars]

@xr.register_dataset_accessor("type1") class Type1Accessor: def init(self, ds): if _dataset_type(ds) != "type1": raise AttributeError("not a type1 dataset") self.dataset = ds though now that we have a "type" registry, we could also have one accessor, and pass a `kind` parameter to your `analyze` function:python def analyze(self, kind="auto"): analyzers = { "type1": _analyze_type1, "type2": _analyze_type2, }

if kind == "auto":
    kind = self.dataset_type
return analyzers.get(kind)(self.dataset)

```

If you just wanted to use static code analysis using e.g. mypy, consider using TypedDict. I don't know anything about mypy, though, so I wasn't able to get it to accept Dataset objects instead of dict. If someone actually gets this to work, we might be able to provide a xarray.typing module to allow something like (but depending on the amount of code needed, this could also fit in the Cookbook docs section): ```python from xarray.typing as DatasetType, Coordinate, ArrayType, Int64Type, FloatType

class Dataset1(DatasetType): longitude : Coordinate[ArrayType[Float64Type]] latitude : Coordinate[ArrayType[Float64Type]]

temperature : ArrayType[Float64Type]

def function(ds : Dataset1): # ... return ds ``` and have the type checker validate the structure of the dataset.

{
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  597475005
Powered by Datasette · Queries took 79.561ms · About: xarray-datasette