MCPcopy Index your code
hub / github.com/pydata/xarray / decode

Method decode

xarray/backends/zarr.py:172–210  ·  view source on GitHub ↗
(
        cls, value: int | float | str | bytes | list, dtype: str | np.dtype[Any]
    )

Source from the content-addressed store, hash-verified

170
171 @classmethod
172 def decode(
173 cls, value: int | float | str | bytes | list, dtype: str | np.dtype[Any]
174 ):
175 if dtype == "string":
176 # zarr V3 string type
177 return str(value)
178 elif dtype == "bytes":
179 # zarr V3 bytes type
180 if not isinstance(value, str | bytes):
181 raise TypeError(
182 f"Failed to decode fill_value: expected str or bytes for dtype {dtype}, got {type(value).__name__}"
183 )
184 return base64.standard_b64decode(value)
185 np_dtype = np.dtype(dtype)
186 if np_dtype.kind == "f":
187 if not isinstance(value, str | bytes):
188 raise TypeError(
189 f"Failed to decode fill_value: expected str or bytes for dtype {np_dtype}, got {type(value).__name__}"
190 )
191 return struct.unpack("<d", base64.standard_b64decode(value))[0]
192 elif np_dtype.kind == "c":
193 # complex - decode each component from base64, matching float decoding
194 if not (isinstance(value, list | tuple) and len(value) == 2):
195 raise TypeError(
196 f"Failed to decode fill_value: expected a 2-element list for dtype {np_dtype}, got {type(value).__name__}"
197 )
198 real = struct.unpack("<d", base64.standard_b64decode(value[0]))[0]
199 imag = struct.unpack("<d", base64.standard_b64decode(value[1]))[0]
200 return complex(real, imag)
201 elif np_dtype.kind == "b":
202 return bool(value)
203 elif np_dtype.kind in "iu":
204 if not isinstance(value, int | float | np.integer | np.floating):
205 raise TypeError(
206 f"Failed to decode fill_value: expected int or float for dtype {np_dtype}, got {type(value).__name__}"
207 )
208 return int(value)
209 else:
210 raise ValueError(f"Failed to decode fill_value. Unsupported dtype {dtype}")
211
212
213def encode_zarr_attr_value(value):

Callers 5

check_attrFunction · 0.45
_read_attributesFunction · 0.45
encodeMethod · 0.45
open_store_variableMethod · 0.45
_decode_stringFunction · 0.45

Calls 2

typeFunction · 0.85
dtypeMethod · 0.45

Tested by

no test coverage detected