Skip to content

Commit d209ef8

Browse files
committed
#137 Update runtime codes for DataArray creation
1 parent b6eefff commit d209ef8

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

xarray_dataclasses/dataarray.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,23 +104,21 @@ def asdataarray(
104104
dataoptions = DataOptions(xr.DataArray)
105105

106106
model = DataModel.from_dataclass(dataclass)
107-
item = next(iter(model.data.values()))
108-
dataarray = dataoptions.factory(item(reference))
107+
dataarray = dataoptions.factory(model.data_vars[0](reference))
109108

110-
for item in model.coord.values():
111-
if item.name in dataarray.dims:
112-
dataarray.coords[item.name] = item(dataarray)
109+
for entry in model.coords:
110+
if entry.name in dataarray.dims:
111+
dataarray.coords[entry.name] = entry(dataarray)
113112

114-
for item in model.coord.values():
115-
if item.name not in dataarray.dims:
116-
dataarray.coords[item.name] = item(dataarray)
113+
for entry in model.coords:
114+
if entry.name not in dataarray.dims:
115+
dataarray.coords[entry.name] = entry(dataarray)
117116

118-
for item in model.attr.values():
119-
dataarray.attrs[item.name] = item()
117+
for entry in model.attrs:
118+
dataarray.attrs[entry.name] = entry()
120119

121-
if model.name:
122-
item = next(iter(model.name.values()))
123-
dataarray.name = item()
120+
if model.names:
121+
dataarray.name = model.names[0]()
124122

125123
return dataarray
126124

@@ -213,12 +211,12 @@ def shaped(
213211
214212
"""
215213
model = DataModel.from_dataclass(cls)
216-
name, item = next(iter(model.data.items()))
214+
key, entry = model.data_vars_items[0]
217215

218216
if isinstance(shape, dict):
219-
shape = tuple(shape[dim] for dim in item.type["dims"])
217+
shape = tuple(shape[dim] for dim in entry.dims)
220218

221-
return asdataarray(cls(**{name: func(shape)}, **kwargs))
219+
return asdataarray(cls(**{key: func(shape)}, **kwargs))
222220

223221
@overload
224222
@classmethod

0 commit comments

Comments
 (0)