Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
raise TraitError('The klass attribute must be a class'
' not: %r' % klass)
if default_value is Empty:
default_value = klass()
elif default_value is not None and default_value is not Undefined:
default_value = klass(default_value)
super(XarrayType, self).__init__(default_value=default_value, allow_none=allow_none, **kwargs)
def make_dynamic_default(self):
if self.default_value is None or self.default_value is Undefined:
return self.default_value
else:
return self.default_value.copy()
class Dataset(XarrayType):
"""An xarray dataset trait type."""
info_text = 'an xarray dataset'
def __init__(self, default_value=Empty, allow_none=False, dtype=None, **kwargs):
if 'klass' not in kwargs and self.klass is None:
import xarray as xr
kwargs['klass'] = xr.Dataset
super(Dataset, self).__init__(
default_value=default_value, allow_none=allow_none, dtype=dtype, **kwargs)
class DataArray(XarrayType):
"""An xarray dataarray trait type."""
def validate(self, obj, value):
if value is None and not self.allow_none:
self.error(obj, value)
if value is None or value is Undefined:
return super(XarrayType, self).validate(obj, value)
try:
value = self.klass(value)
except (ValueError, TypeError) as e:
raise TraitError(e)
return super(XarrayType, self).validate(obj, value)
class Dataset(XarrayType):
"""An xarray dataset trait type."""
info_text = 'an xarray dataset'
def __init__(self, default_value=Empty, allow_none=False, dtype=None, **kwargs):
if 'klass' not in kwargs and self.klass is None:
import xarray as xr
kwargs['klass'] = xr.Dataset
super(Dataset, self).__init__(
default_value=default_value, allow_none=allow_none, dtype=dtype, **kwargs)
class DataArray(XarrayType):
"""An xarray dataarray trait type."""
info_text = 'an xarray dataarray'
dtype = None
def __init__(self, default_value=Empty, allow_none=False, dtype=None, **kwargs):
if 'klass' not in kwargs and self.klass is None:
import xarray as xr
kwargs['klass'] = xr.DataArray
super(DataArray, self).__init__(
default_value=default_value, allow_none=allow_none, dtype=dtype, **kwargs)
self.dtype = dtype