/usr/share/pyshared/h5py/h5a.pyx is in python-h5py 2.0.1-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 | #+
#
# This file is part of h5py, a low-level Python interface to the HDF5 library.
#
# Copyright (C) 2008 Andrew Collette
# http://h5py.alfven.org
# License: BSD (See LICENSE.txt for full license)
#
# $Date$
#
#-
"""
Provides access to the low-level HDF5 "H5A" attribute interface.
"""
include "config.pxi"
# Compile-time imports
from _objects cimport pdefault
from h5t cimport TypeID, typewrap, py_create
from h5s cimport SpaceID
from h5p cimport PropID
from numpy cimport import_array, ndarray, PyArray_DATA
from utils cimport check_numpy_read, check_numpy_write, emalloc, efree
from _proxy cimport attr_rw
# Initialization
import_array()
# === General attribute operations ============================================
# --- create, create_by_name ---
def create(ObjectID loc not None, char* name, TypeID tid not None,
SpaceID space not None, *, char* obj_name='.', PropID lapl=None):
"""(ObjectID loc, STRING name, TypeID tid, SpaceID space, **kwds) => AttrID
Create a new attribute, attached to an existing object.
STRING obj_name (".")
Attach attribute to this group member instead
PropID lapl
Link access property list for obj_name
"""
return AttrID(H5Acreate_by_name(loc.id, obj_name, name, tid.id,
space.id, H5P_DEFAULT, H5P_DEFAULT, pdefault(lapl)))
# --- open, open_by_name, open_by_idx ---
def open(ObjectID loc not None, char* name=NULL, int index=-1, *,
char* obj_name='.', int index_type=H5_INDEX_NAME, int order=H5_ITER_NATIVE,
PropID lapl=None):
"""(ObjectID loc, STRING name=, INT index=, **kwds) => AttrID
Open an attribute attached to an existing object. You must specify
exactly one of either name or idx. Keywords are:
STRING obj_name (".")
Attribute is attached to this group member
PropID lapl (None)
Link access property list for obj_name
INT index_type (h5.INDEX_NAME)
INT order (h5.ITER_NATIVE)
"""
if (name == NULL and index < 0) or (name != NULL and index >= 0):
raise TypeError("Exactly one of name or idx must be specified")
if name != NULL:
return AttrID(H5Aopen_by_name(loc.id, obj_name, name,
H5P_DEFAULT, pdefault(lapl)))
else:
return AttrID(H5Aopen_by_idx(loc.id, obj_name,
<H5_index_t>index_type, <H5_iter_order_t>order, index,
H5P_DEFAULT, pdefault(lapl)))
# --- exists, exists_by_name ---
def exists(ObjectID loc not None, char* name, *,
char* obj_name=".", PropID lapl=None):
"""(ObjectID loc, STRING name, **kwds) => BOOL
Determine if an attribute is attached to this object. Keywords:
STRING obj_name (".")
Look for attributes attached to this group member
PropID lapl (None):
Link access property list for obj_name
"""
return <bint>H5Aexists_by_name(loc.id, obj_name, name, pdefault(lapl))
# --- rename, rename_by_name ---
def rename(ObjectID loc not None, char* name, char* new_name, *,
char* obj_name='.', PropID lapl=None):
"""(ObjectID loc, STRING name, STRING new_name, **kwds)
Rename an attribute. Keywords:
STRING obj_name (".")
Attribute is attached to this group member
PropID lapl (None)
Link access property list for obj_name
"""
H5Arename_by_name(loc.id, obj_name, name, new_name, pdefault(lapl))
def delete(ObjectID loc not None, char* name=NULL, int index=-1, *,
char* obj_name='.', int index_type=H5_INDEX_NAME, int order=H5_ITER_NATIVE,
PropID lapl=None):
"""(ObjectID loc, STRING name=, INT index=, **kwds)
Remove an attribute from an object. Specify exactly one of "name"
or "index". Keyword-only arguments:
STRING obj_name (".")
Attribute is attached to this group member
PropID lapl (None)
Link access property list for obj_name
INT index_type (h5.INDEX_NAME)
INT order (h5.ITER_NATIVE)
"""
if name != NULL and index < 0:
H5Adelete_by_name(loc.id, obj_name, name, pdefault(lapl))
elif name == NULL and index >= 0:
H5Adelete_by_idx(loc.id, obj_name, <H5_index_t>index_type,
<H5_iter_order_t>order, index, pdefault(lapl))
else:
raise TypeError("Exactly one of index or name must be specified.")
def get_num_attrs(ObjectID loc not None):
"""(ObjectID loc) => INT
Determine the number of attributes attached to an HDF5 object.
"""
return H5Aget_num_attrs(loc.id)
cdef class AttrInfo:
cdef H5A_info_t info
property corder_valid:
"""Indicates if the creation order is valid"""
def __get__(self):
return <bint>self.info.corder_valid
property corder:
"""Creation order"""
def __get__(self):
return <int>self.info.corder
property cset:
"""Character set of attribute name (integer typecode from h5t)"""
def __get__(self):
return <int>self.info.cset
property data_size:
"""Size of raw data"""
def __get__(self):
return self.info.data_size
def _hash(self):
return hash((self.corder_valid, self.corder, self.cset, self.data_size))
def get_info(ObjectID loc not None, char* name=NULL, int index=-1, *,
char* obj_name='.', PropID lapl=None,
int index_type=H5_INDEX_NAME, int order=H5_ITER_NATIVE):
"""(ObjectID loc, STRING name=, INT index=, **kwds) => AttrInfo
Get information about an attribute, in one of two ways:
1. If you have the attribute identifier, just pass it in
2. If you have the parent object, supply it and exactly one of
either name or index.
STRING obj_name (".")
Use this group member instead
PropID lapl (None)
Link access property list for obj_name
INT index_type (h5.INDEX_NAME)
Which index to use
INT order (h5.ITER_NATIVE)
What order the index is in
"""
cdef AttrInfo info = AttrInfo()
if name == NULL and index < 0:
H5Aget_info(loc.id, &info.info)
elif name != NULL and index >= 0:
raise TypeError("At most one of name and index may be specified")
elif name != NULL:
H5Aget_info_by_name(loc.id, obj_name, name, &info.info, pdefault(lapl))
elif index >= 0:
H5Aget_info_by_idx(loc.id, obj_name, <H5_index_t>index_type,
<H5_iter_order_t>order, index, &info.info, pdefault(lapl))
return info
# === Iteration routines ======================================================
cdef class _AttrVisitor:
cdef object func
cdef object retval
def __init__(self, func):
self.func = func
self.retval = None
cdef herr_t cb_attr_iter(hid_t loc_id, char* attr_name, H5A_info_t *ainfo, void* vis_in) except 2:
cdef _AttrVisitor vis = <_AttrVisitor>vis_in
cdef AttrInfo info = AttrInfo()
info.info = ainfo[0]
vis.retval = vis.func(attr_name, info)
if vis.retval is not None:
return 1
return 0
cdef herr_t cb_attr_simple(hid_t loc_id, char* attr_name, H5A_info_t *ainfo, void* vis_in) except 2:
cdef _AttrVisitor vis = <_AttrVisitor>vis_in
vis.retval = vis.func(attr_name)
if vis.retval is not None:
return 1
return 0
def iterate(ObjectID loc not None, object func, int index=0, *,
int index_type=H5_INDEX_NAME, int order=H5_ITER_NATIVE, bint info=0):
"""(ObjectID loc, CALLABLE func, INT index=0, **kwds) => <Return value from func>
Iterate a callable (function, method or callable object) over the
attributes attached to this object. You callable should have the
signature::
func(STRING name) => Result
or if the keyword argument "info" is True::
func(STRING name, AttrInfo info) => Result
Returning None continues iteration; returning anything else aborts
iteration and returns that value. Keywords:
BOOL info (False)
Callback is func(STRING name, AttrInfo info), not func(STRING name)
INT index_type (h5.INDEX_NAME)
Which index to use
INT order (h5.ITER_NATIVE)
Index order to use
"""
if index < 0:
raise ValueError("Starting index must be a non-negative integer.")
cdef hsize_t i = index
cdef _AttrVisitor vis = _AttrVisitor(func)
cdef H5A_operator2_t cfunc
if info:
cfunc = cb_attr_iter
else:
cfunc = cb_attr_simple
H5Aiterate2(loc.id, <H5_index_t>index_type, <H5_iter_order_t>order,
&i, cfunc, <void*>vis)
return vis.retval
# === Attribute class & methods ===============================================
cdef class AttrID(ObjectID):
"""
Logical representation of an HDF5 attribute identifier.
Objects of this class can be used in any HDF5 function call
which expects an attribute identifier. Additionally, all ``H5A*``
functions which always take an attribute instance as the first
argument are presented as methods of this class.
* Hashable: No
* Equality: Identifier comparison
"""
property name:
"""The attribute's name"""
def __get__(self):
return self.get_name()
property shape:
"""A Numpy-style shape tuple representing the attribute's dataspace"""
def __get__(self):
cdef SpaceID space
space = self.get_space()
return space.get_simple_extent_dims()
property dtype:
"""A Numpy-stype dtype object representing the attribute's datatype"""
def __get__(self):
cdef TypeID tid
tid = self.get_type()
return tid.py_dtype()
def _close(self):
"""()
Close this attribute and release resources. You don't need to
call this manually; attributes are automatically destroyed when
their Python wrappers are freed.
"""
H5Aclose(self.id)
def read(self, ndarray arr not None):
"""(NDARRAY arr)
Read the attribute data into the given Numpy array. Note that the
Numpy array must have the same shape as the HDF5 attribute, and a
conversion-compatible datatype.
The Numpy array must be writable and C-contiguous. If this is not
the case, the read will fail with an exception.
"""
cdef TypeID mtype
cdef hid_t space_id
space_id = 0
try:
space_id = H5Aget_space(self.id)
check_numpy_write(arr, space_id)
mtype = py_create(arr.dtype)
attr_rw(self.id, mtype.id, PyArray_DATA(arr), 1)
finally:
if space_id:
H5Sclose(space_id)
def write(self, ndarray arr not None):
"""(NDARRAY arr)
Write the contents of a Numpy array too the attribute. Note that
the Numpy array must have the same shape as the HDF5 attribute, and
a conversion-compatible datatype.
The Numpy array must be C-contiguous. If this is not the case,
the write will fail with an exception.
"""
cdef TypeID mtype
cdef hid_t space_id
space_id = 0
try:
space_id = H5Aget_space(self.id)
check_numpy_read(arr, space_id)
mtype = py_create(arr.dtype)
attr_rw(self.id, mtype.id, PyArray_DATA(arr), 0)
finally:
if space_id:
H5Sclose(space_id)
def get_name(self):
"""() => STRING name
Determine the name of this attribute.
"""
cdef int blen
cdef char* buf
buf = NULL
try:
blen = H5Aget_name(self.id, 0, NULL)
assert blen >= 0
buf = <char*>emalloc(sizeof(char)*blen+1)
blen = H5Aget_name(self.id, blen+1, buf)
strout = <bytes>buf
finally:
efree(buf)
return strout
def get_space(self):
"""() => SpaceID
Create and return a copy of the attribute's dataspace.
"""
return SpaceID(H5Aget_space(self.id))
def get_type(self):
"""() => TypeID
Create and return a copy of the attribute's datatype.
"""
return typewrap(H5Aget_type(self.id))
def get_storage_size(self):
"""() => INT
Get the amount of storage required for this attribute.
"""
return H5Aget_storage_size(self.id)
|