def require_dataset(self, name, shape, dtype, exact=False, **kwds):
""" Open a dataset, creating it if it doesn't exist.
If keyword "exact" is False (default), an existing dataset must have
the same shape and a conversion-compatible dtype to be returned. If
True, the shape and dtype must match exactly.
Other dataset keywords (see create_dataset) may be provided, but are
only used if a new dataset is to be created.
Raises TypeError if an incompatible object already exists, or if the
shape or dtype don't match according to the above rules.
"""
with phil:
if not name in self:
return self.create_dataset(name, *(shape, dtype), **kwds)
dset = self[name]
if not isinstance(dset, Dataset):
raise TypeError("Incompatible object (%s) already exists" % dset.__class__.__name__)
if not shape == dset.shape:
raise TypeError("Shapes do not match (existing %s vs new %s)" % (dset.shape, shape))
if exact:
if not dtype == dset.dtype:
raise TypeError("Datatypes do not exactly match (existing %s vs new %s)" % (dset.dtype, dtype))
elif not numpy.can_cast(dtype, dset.dtype):
raise TypeError("Datatypes cannot be safely cast (existing %s vs new %s)" % (dset.dtype, dtype))
return dset
评论列表
文章目录