Chapter 4: Library Reference
"Tenho pensamentos que, se pudesse revelá-los e
fazê-los viver, acrescentariam nova luminosidade às estrelas,
nova beleza ao mundo e maior amor ao coração dos homens."
—Fernando Pessoa, in "O Eu Profundo"
PyTables implements several classes to represent
the different nodes in the object tree. They are named
File, Group, Leaf,
Table and Array. Another one is
responsible to build record objects from a subclass user
declaration, and performs field and type checks; its name is
IsDescription. An important function, called
openFile is responsible to create, open or append
to files. In addition, a few utility functions are defined to
guess if the user supplied file is a PyTables or
HDF5 file. These are called
isPyTablesFile and isHDF5. Finally,
several first-level variables are also available to the user
that informs about PyTables version, file format
version or underlying libraries (as for example
HDF5) version number.
Let's start discussing the first-level variables and
functions available to the user, then the methods in the
classes defined in PyTables.
4.1 tables variables and
functions
4.1.1 Global variables
- __version__
- The PyTables
version number.
- HDF5Version
- The underlying HDF5 library version number.
- ExtVersion
- The Pyrex extension types
version. This might be useful when reporting
bugs.
4.1.2 Global functions
- openFile(filename, mode='r', title='', trMap={},
rootUEP="/")
- Open a PyTables file and returns a File
object.
- filename
- The name of the file
(supports environment variable expansion). It is
suggested that it should have any of
".h5", ".hdf" or
".hdf5" extensions, although this is
not mandatory.
- mode
- The mode to open the file. It
can be one of the following:
- 'r'
- read-only; no data can be
modified.
- 'w'
- write; a new file is created
(an existing file with the same name is
deleted).
- 'a'
- append; an existing file is
opened for reading and writing, and if the file does
not exist it is created.
- 'r+'
- is similar to 'a', but the
file must already exist.
- title
- If filename is new, this will
set a title for the root group in this file. If
filename is not new, the title will be read from
disk, and this will not have any effect.
- trMap
- A dictionary to map names in
the object tree Python namespace into different HDF5
names in file namespace. The keys are the Python
names, while the values are the HDF5 names. This is
useful when you need to name HDF5 nodes with invalid
or reserved words in Python.
- rootUEP
- The root User Entry
Point. This is a group in the HDF5 hierarchy which
will be taken as the starting point to create the
object tree. The group has to be named after its
HDF5 name and can be a path. If it does not exist, a
RuntimeError is issued. Use this if you
do not want to build the entire object tree, but rather
only a subtree.
- isHDF5(filename)
- Determines whether
filename is in the HDF5 format or not. When successful,
returns a positive value, for TRUE, or 0 (zero), for
FALSE. Otherwise returns a negative value. To this
function to work, it needs a closed file.
- isPyTablesFile(filename)
- Determines
whether a file is in the PyTables format.
When successful, returns the format version string, for
TRUE, or 0 (zero), for FALSE. Otherwise returns a
negative value. To this function to work, it needs a
closed file.
4.2 The IsDescription class
This class is in fact a so-called metaclass
object. There is nothing special on this fact, except that
their subclasses attributes are transformed during its
instantiation phase, and new methods for instances are
defined based on the values of the class attributes.
It is designed to be used as an easy, yet meaningful way to
describe the properties of Table objects
through the use of classes that inherit properties from
it. In order to define such an special class, you have to
declare it as descendant of IsDescription, with
many attributes as columns you want in your table. The name
of these attributes will become the name of the columns,
while its values are the properties of the columns that are
obtained through the use of the Col class
constructor. See the section 4.3 for instructions on how
define the properties of the table columns.
Then, you can pass an instance of this object to the
Table constructor, where all the information it
contains will be used to define the table structure. See
the section 3.3 for an example
on how that works.
4.3 The Col class and its descendants
The Col class is used as a mean to declare the
different properties of a column of a table. In addition, a
series of descendant classes are offered in order to make
these column descriptions easier to the user. In general, it
is recommended to use these descendants classes, as they are
meaningful when found in the middle of the code.
The only public method accessible in these classes is the
constructor itself.
- Col(dtype="Float64", shape=1, dflt=None, pos=None)
- Define properties for a Table column.
- dtype
- The data type for the
column. See the appendix A for a
relation of data types supported in a IsDescription class
declaration. The type description is accepted both in
string format and as numarray data type.
- shape
- An integer or a tuple, that
specifies the number of dtype items for each
element (or shape, for multidimensional elements) of
this column. For CharType columns, the
first dimension is used as the length of the character
strings. For this kind of objects, the use of
StringCol subclass is recommended.
- dflt
- The default value for elements
of this column. If the user does not supply a value
for an element while filling a table, this default
value will be written to disk. If the user supplies an
scalar value for a multidimensional column, this value
is automatically broadcasted to all the
elements in the column cell. If dflt is not
supplied, a appropriate zero value (or null
string) will be chosen by default.
- pos
- By default, columns are disposed in
memory following an alphanumerical order of the column
names. In some situations, however, it is convenient to
impose a user defined ordering. pos parameter
allows the user to force the wanted disposition.
- StringCol(length=None, dflt=None, shape=1, pos=None)
- Define a column to be of CharType
type. The length parameter sets the length of
the strings. The meaning of the other parameters are like
in the Col class.
- IntCol(dflt=0, shape=1, itemsize=4, sign=1, pos=None)
- Define a column to be of IntXXType type,
depending on the value of itemsize. The
itemsize parameter sets the number of bytes
of the integers in the column and the default is 4
bytes. sign determines if the integers are
signed or not. The meaning of the other parameters are
like in the Col class.
This class has several descendants:
- Int8Col(dflt=0, shape=1, pos=None)
- Define a column as an Int8 type.
- UInt8Col(dflt=0, shape=1, pos=None)
- Define a column as an UInt8
type.
- Int16Col(dflt=0, shape=1, pos=None)
- Define a column as an Int16 type.
- UInt16Col(dflt=0, shape=1, pos=None)
- Define a column as an UInt16
type.
- Int32Col(dflt=0, shape=1, pos=None)
- Define a column as an Int32 type.
- UInt32Col(dflt=0, shape=1, pos=None)
- Define a column as an UInt32
type.
- Int64Col(dflt=0, shape=1, pos=None)
- Define a column as an Int64 type.
- UInt64Col(dflt=0, shape=1, pos=None)
- Define a column as an UInt64
type.
- FloatCol(dflt=0, shape=1, itemsize=8, pos=None)
- Define a column to be of FloatXXType
type, depending on the value of itemsize. The
itemsize parameter sets the number of bytes
of the floats in the column and the default is 8 bytes
(double precision). The meaning of the other parameters
are like in the Col class.
This class has two descendants:
- Float32Col(dflt=0.0, shape=1, pos=None)
- Define a column as a Float32
type.
- Float64Col(dflt=0.0, shape=1, pos=None)
- Define a column as a Float64
type.
4.4 The File class
This class is returned when a PyTables file is
opened with the openFile() function. It has
methods to create, open, flush and close
PyTables files. Also, File class
offer methods to traverse the object tree, as well as to
create, rename and delete nodes. One of its attributes
(rootUEP) represents the user entry
point to the object tree attached to the file.
Next, we will discuss the attributes and methods for File
class4).
4.4.1 File instance
variables
- filename
- Filename opened.
- isopen
- It takes the value 1 if the
underlying file is open. 0 otherwise.
- mode
- Mode in which the filename was
opened.
- title
- The title of the root group in
file.
- rootUEP
- The UEP (User Entry Point)
group in file (see ??).
- trMap
- This is a dictionary that maps
node names between python and HDF5 domain names. Its
initial values are set from the trMap parameter
passed to the openFile() function. You can
change its contents after a file is opened and
the new map will take effect over any new object added
to the tree.
- objects
- Dictionary with all objects
(groups or leaves) on tree.
- groups
- Dictionary with all object
groups on tree.
- leaves
- Dictionary with all object
leaves on tree.
4.4.2 File methods
createGroup(where, name, title='')
Create a new Group instance with name name in
where location.
- where
- The parent group where the new
group will hang. where parameter can be a path
string (for example
"/Particles/TParticle1"), or another Group
instance.
- name
- The name of the new group.
- title
- A description for this
group.
createTable(where, name,
description, title='', compress=0, complib = 'zlib',
expectedrows=10000)
Create a new Table instance with name
name in where location.
- where
- The parent group where the
new table will hang. where parameter can be
a path string (for example
"/Particles/TParticle1"), or Group
instance.
- name
- The name of the new table.
- description
- An instance of a
user-defined class (derived from the
IsDescription class) where table fields
are defined. However, in certain situations, it is
more handy to allow this description to be supplied
as a dictionary (for example, when you do not know
beforehand which structure will have your table). In
such a cases, you can pass the description as a
dictionary as well. See section 3.3 for an example
of use. Finally, a RecArray object from
the numarray package is also accepted,
and all the information about columns and other
metadata is used as a basis to create the
Table object. Moreover, if the
RecArray has actual data this is also
injected on the newly created Table
object.
- title
- A description for this object.
- compress
- Specifies a compress level
for data. The allowed range is 0-9. A value of 0
disables compression. The default is that
compression is disabled, that balances between
compression effort and CPU consumption.
- complib
- Specifies the compression
library to be used. Right now, "zlib"
(default), "lzo" and "ucl"
values are supported. See section 5.2 for some advice
on which library is better suited to your needs.
- expectedrows
- An user estimate of the
number of records that will be on table. If not
provided, the default value is appropriate for tables
until 1 MB in size (more or less, depending on the
record size). If you plan to save bigger tables you
should provide a guess; this will optimize the HDF5
B-Tree creation and management process time and memory
used. See section 5.3
for a detailed justification of that issue.
createArray(where, name,
object, title='')
Create a new Array instance with name
name in where location.
- where
- The parent group where the
new array will hang. where parameter can be
a path string (for example
"/Particles/TParticle1"), or
Group instance.
- name
- The name of the new
array.
- object
- The regular array to be
saved. Currently accepted values are: lists, tuples,
scalars (int and float), strings and
(multidimensional) Numeric and
NumArray arrays (including
CharArrays string arrays). However,
these objects must be regular (i.e. they cannot be
like, for example, [[1,2],2]). Also,
objects that has some of its dimension equal to zero
are not supported (this will be solved when
unlimited arrays will be implemented).
- title
- A description for this
object.
getNode(where, name='',
classname='')
Returns the object node name under
where location.
- where
- Can be a path string or
Group instance. If where
doesn't exists or has not a child called
name, a ValueError error is
raised.
- name
- The object name desired. If
name is a null string (''), or not
supplied, this method assumes to find the object in
where.
- classname
- If supplied, returns only
an instance of this class name. Allowed names in
classname are: 'Group',
'Leaf', 'Table' and
'Array'. Note that these values are
strings.
getAttrNode(where,
attrname, name='' )
Returns the attribute attrname under
where.name location.
- where
- Can be a path string or
Group instance. If where
doesn't exists or has not a child called
name, a ValueError error is
raised.
- attrname
- The name of the attribute
to get.
- name
- The node name desired. If
name is a null string (''), or not
supplied, this method assumes to find the object in
where.
setAttrNode(where,
attrname, attrvalue, name='')
Sets the attribute attrname with value
attrvalue under where.name location.
- where
- Can be a path string or
Group instance. If where
doesn't exists or has not a child called
name, a ValueError error is
raised.
- attrname
- The name of the attribute
to set on disk.
- attrvalue
- The value of the
attribute to set. Only strings attributes are
supported natively right now. However, you can
always use (c)Pickle so as to serialize
any object you want save therein.
- name
- The node name desired. If
name is a null string (''), or not
supplied, this method assumes to find the object in
where.
listNodes(where,
classname='')
Returns a list with all the object nodes (Group or
Leaf) hanging from where. The list is
alphanumerically sorted by node name.
- where
- The parent group. Can be a
path string or Group instance.
- classname
- If a classname
parameter is supplied, the iterator will return only
instances of this class (or subclasses of it). The
only supported classes in classname are
'Group', 'Leaf',
'Table' and 'Array'. Note
that these values are strings.
removeNode(where, name = "",
recursive=0)
Removes the object node
name under where location.
- where
- Can be a path string or
Group instance. If where
doesn't exists or has not a child called
name, a LookupError error is
raised.
- name
- The name of the node to be
removed. If not provided, the where node is
changed.
- recursive
- If not supplied, the
object will be removed only if it has no
children. If supplied with a true value, the object
and all its descendants will be completely
removed.
renameNode(where, newname,
name)
Rename the object node name under
where location.
- where
- Can be a path string or
Group instance. If where
doesn't exists or has not a child called
name, a LookupError error is
raised.
- newname
- Is the new name to be
assigned to the node.
- name
- The name of the node to be
changed. If not provided, the where node is
changed.
walkGroups(where='/')
Iterator that returns the list of Groups (not
Leaves) hanging from where. If where
is not supplied, the root object is taken as origin. The
returned Group list is in a top-bottom order, and
alphanumerically sorted when they are at the same level.
- where
- The origin group. Can be a
path string or Group instance.
flush()
Flush all the leaves in the object tree.
close()
Flush all the leaves in object tree and close the file.
4.4.3 File special
methods
Following are described the methods that automatically
trigger actions when a File instance is
accessed in a special way (e.g.,
fileh("/detector") will cause a call to
group.__call__("/detector")).
__call__(where="/",
classname="")
Recursively iterate over the children in the
File instance. It takes two parameters:
- where
- If supplied, the iteration
starts from this group.
- classname
- (String) If
supplied, only instances of this class are
returned.
Example of use:
# Recursively print all the nodes hanging from '/detector'
print "Nodes hanging from group '/detector':"
for node in h5file("/detector"):
print node
__iter__()
Iterate over the children on the File
instance. However, this does not accept parameters. This
iterator is recursive.
Example of use:
# Recursively list all the nodes in the object tree
print "All nodes in the object tree:"
for node in h5file:
print node
4.5 The Group class
Instances of this class are a grouping structure containing
instances of zero or more groups or leaves, together with
supporting metadata.
Working with groups and leaves is similar in many ways to
working with directories and files, respectively, in a Unix
filesystem. As with Unix directories and files, objects in
the object tree are often described by giving their full (or
absolute) path names. This full path can be specified either
as string (like in '/group1/group2') or as a
complete object path written in the Pythonic fashion known
as natural name schema (like in
file.root.group1.group2) and discussed in the
section 1.2.
A collateral effect of the natural naming schema
is that you must be aware when assigning a new attribute
variable to a Group object to not collide with existing
children node names. For this reason and to not pollute the
children namespace, it is explicitly forbidden to assign
"normal" attributes to Group instances, and the only ones
allowed must start with some reserved prefixes, like
"_f_" (for methods) or "_v_" (for
instance variables) prefixes. Any attempt to assign a new
attribute that does not starts with these prefixes, will
raise a NameError exception.
Other effect is that you cannot use reserved Python names
or other non-allowed python names (like for example "$a" or
"44") as node names. You can, however, make use of a
translation map dictionary in the
File.openfile() method (see section ??) so as to use non valid Python
names as node names in the file.
4.5.1 Group instance
variables
- _v_title
- A description for this group.
- _v_name
- The name of this group.
- _v_hdf5name
- The name of this group in
HDF5 file namespace.
- _v_pathname
- A string representation of the group location
in tree.
- _v_parent
- The parent Group instance.
- _v_rootgroup
- Pointer to the root group object.
- _v_file
- Pointer to the associated File object.
- _v_childs
- Dictionary with all nodes
(groups or leaves) hanging from this instance.
- _v_groups
- Dictionary with all node
groups hanging from this instance.
- _v_leaves
- Dictionary with all node
leaves hanging from this instance.
- _v_attrs
- The associated
AttributeSet instance (see 4.10).
4.5.2 Group methods
This class define the
__setattr__,
__getattr__ and
__delattr__ and
they work as normally intended. So, you can access, assign
or delete childs to a group by just using the next
constructs:
# Add a Table child instance under group with name "tablename"
group.tablename = Table(recordDict, "Record instance")
table = group.tablename # Get the table child instance
del group.tablename # Delete the table child instance
Caveat: The following
methods are documented for completeness, and they can be
used without any problem. However, you should use the
high-level counterpart methods in the File
class, because these are most used in documentation and
examples, and are a bit more powerful than ones those
exposed here.
- _f_join(name)
- Helper method to correctly concatenate a name child object
with the pathname of this group.
- _f_rename(newname)
- Change the name of this group to newname.
- _f_remove(recursive=0)
- Remove this
object. If recursive is true, force the removal
even if this group has children.
- _f_getAttr(attrname)
- Gets the HDF5
attribute attrname of this group.
- _f_setAttr(attrname, attrvalue)
- Sets
the attribute attrname of this group to the
value attrvalue. Only string values are
allowed.
- _f_listNodes(classname='')
- Returns a
list with all the object nodes hanging from
this instance. The list is alphanumerically sorted by
node name. If a classname parameter is
supplied, it will only return instances of this class
(or subclasses of it). The supported classes in
classname are 'Group',
'Leaf', 'Table' and
'Array'.
- _f_walkGroups()
- Iterator that returns
the list of Groups (not Leaves) hanging from
self. The returned Group list is in a
top-bottom order, and alphanumerically sorted when they
are at the same level.
- _f_close()
- Close this group, making it
and its children unaccessible in the object tree.
4.5.3 Group special
methods
Following are described the methods that automatically
trigger actions when a Group instance is
accessed in a special way (e.g.,
group("Table") will cause a call to
group.__call__("Table")).
__call__(classname="",
recursive=0)
Iterate over the childs in the Group
instance. It takes two parameters:
- classname
- (String) If
supplied, only instances of this class are
returned.
- recursive
- (Integer) If
false, only childs hanging immediately after the group
are returned. If true, a recursion over all the groups
hanging from it is performed.
Example of use:
# Recursively print all the arrays hanging from '/'
print "Arrays the object tree '/':"
for array in h5file.root(classname="Array", recursive=1):
print array
__iter__()
Iterate over the childs on the group instance. However,
this does not accept parameters. This iterator is not
recursive.
Example of use:
# Non-recursively list all the nodes hanging from '/detector'
print "Nodes in '/detector' group:"
for node in h5file.root.detector:
print node
4.6 The Leaf class
This is a helper class useful to place common functionality
of all Leaf objects. It is also useful for classifying
purposes. A Leaf object is an end-node, that is, a node that
can hang directly from a group object, but that is not a
group itself. Right now this set is composed by
Table and Array objects. In fact,
Table and Array classes inherit
functionality from this class using the mix-in
technique.
The public variables and methods that Table
and Array inherits from Leaf are
listed below.
4.6.1 Leaf instance
variables
- name
- The Leaf node name in Python
namespace.
- hdf5name
- The Leaf node name in HDF5
namespace.
- title
- The Leaf title.
- shape
- The shape of the associated data
in the Leaf.
- byteorder
- The byteorder of
the associated data of the Leaf.
- attrs
- The associated
AttributeSet instance (see 4.10).
4.6.2 Leaf methods
- rename(newname)
- Change the name of this leaf to newname.
- remove()
- Remove this
leaf.
- getAttr(attrname)
- Gets the HDF5
attribute attrname of this leaf.
- setAttr(attrname, attrvalue)
- Sets
the attribute attrname of this leaf to the
value attrvalue. Only string values are
allowed.
- flush()
- Flush the leaf buffers.
- close()
- Flush the leaf buffers and
close the HDF5 dataset.
4.7 The Table class
Instances of this class represents table objects in the
object tree. It provides methods to create new tables or
open existing ones, as well as methods to read/write data
and metadata from/to table objects in the file.
Data can be read from or written to tables by accessing to
an special object that hangs from Table. This
object is an instance of the Row class (see
4.8). See the tutorial
sections chapter 3 on how to use the
Row interface.
Please note that this object inherits all the public
attributes and methods that Leaf has.
4.7.1 Table instance
variables
- description
- The metaobject describing
this table
- row
- The Row instance for
this table (see 4.8).
- nrows
- The number of rows in this table.
- colnames
- The field names for the table (list).
- coltypes
- The data types for the table fields (dictionary).
- colshapes
- The shapes for the table fields (dictionary).
4.7.2 Table methods
iterrows(start=None,
stop=None, step=None)
Returns an iterator yielding Row instances built from
rows in table. If a range is supplied (i.e. some of the
start, stop or step
parameters are passed), only the appropriate rows are
returned. Else, all the rows are returned.
- start
- Sets the starting row to
return data. A value of 0 means the first row. It
accepts negative values meaning that the count
starts from the end.
- stop
- Sets the last row to be
returned to stop - 1, i.e. the end point is
omitted (in the Python range
tradition). It accepts, likewise start,
negative values. A special value of 0 means the last
row.
- step
- When step is given, it
specifies the increment. Negative values are not
allowed right now.
read(self, start=None, stop=None,
step=None, field=None, flavor=None)
Returns the actual data in Table. If
field is not supplied, it returns the data as a
RecArray object table.
- start
- Sets the starting row to
return data. A value of 0 means the first row. It
accepts negative values meaning that the count
starts from the end.
- stop
- Sets the last row to be
returned to stop - 1, i.e. the end point is
omitted (in the Python range
tradition). It accepts, likewise start,
negative values. A special value of 0 means the last
row.
- step
- When step is given, it
specifies the increment. Negative values are not
allowed right now.
- field
- If specified, only the column
field is returned as a
NumArray object. If this is not
supplied, all the fields are selected and a
RecArray is returned.
- flavor
- When a field in table is
selected, passing a flavor parameter make
an additional conversion to happen in the default
NumArray object. flavor must
have any of the next values: Numeric,
Tuple or List.
removeRows(start=None,
stop=None)
Removes a range of rows in the table. If only
start is supplied, this row is to be
deleted. If a range is supplied, i.e. both the
start and stop parameters are passed,
all the rows in the range are removed5). A step
parameter is not supported yet.
- start
- Sets the starting row to
be removed. It accepts negative values meaning that
the count starts from the end. A value of 0 means
the first row.
- stop
- Sets the last row to be
removed to stop - 1, i.e. the end point is
omitted (in the Python range
tradition). It accepts, likewise start,
negative values. A special value of 0 means the last
row.
4.7.3 Table special
methods
Following are described the methods that automatically
trigger actions when a Table instance is
accessed in a special way (e.g.,
table["var2"] will cause a call to
table.__getitem__("var2")).
__call__(start=None,
stop=None, step=None)
It returns the same iterator than
Table.iterrows(start, stop, step). It is,
therefore, a shorter way to call it.
Example of use:
result = [ row['var2'] for row in table(step=4)
if row['var1'] <= 20 ]
Which is equivalent to:
result = [ row['var2'] for row in table.iterrows(step=4)
if row['var1'] <= 20 ]
__iter__()
It returns the same iterator than
Table.iterrows(0,0,1). However, this does not
accept parameters.
Example of use:
result = [ row['var2'] for row in table
if row['var1'] <= 20 ]
Which is equivalent to:
result = [ row['var2'] for row in table.iterrows()
if row['var1'] <= 20 ]
__getitem__(key)
It takes different actions depending on the
type of the key parameter:
- key is an
Integer
- The corresponding
table row is returned as a
RecArray.Record object.
- key is a
Slice
- The row slice
determined by key is returned as a
RecArray object.
- key is a
String
- The key
is interpreted as a column name of the table,
and, if it exists, it is read and returned as a
NumArray or CharArray object
(whatever is appropriate).
Example of use:
record = table[4]
recarray = table[4:1000:2]
narray = table["var2"]
Which is equivalent to:
record = table.read(start=4)[0]
recarray = table.read(start=4, stop=1000, step=2)
narray = table.read(field="var2")
4.8 The Row class
This class is used to fetch and set values on the table
fields. It works very much like a dictionary, where the keys
are the field names of the associated table and the values
are the values of those fields in a specific row.
This object turns out to actually be an extension type, so
you won't be able to access their documentation
interactively. Neither you won't be able to access it's
internal attributes (they are not directly accessible from
Python), although that accessors (i.e. methods that
return an internal attribute) has been defined for the most
important variables.
4.8.1 Row
methods
- append()
- Once you
have filled the proper fields for the current row, calling
this method actually commit this data to the disk
(actually data is written to the output buffer).
- nrow()
- Accessor that returns the current
row in the table. It is useful to know which row is being
dealt with in the middle of a loop.
4.9 The Array
class
Represents an array on file. It provides methods to create
new arrays or open existing ones, as well as methods to
write/read data and metadata to/from array objects in the
file.
Caveat: All
Numeric and numarray data types
are supported except those that corresponds to complex data
types6). See numarray manual () to know more about the supported
data types, or see appendix A.
Please note that this object inherits all the public
attributes and methods from Leaf.
4.9.1 Array instance
variables
- type
- The type class of the represented
array.
- flavor
- The string object representation
for this array. It can be any of "NumArray",
"CharArray", "Numeric",
"List", "Tuple", "String",
"Int" or "Float" values.
4.9.2 Array
methods
Note that, as this object has not internal I/O buffers,
there is no point in calling flush() method inherited from
Leaf.
- read()
- Read the
array from disk and return it as a NumArray
(default) object, or if possible, with the original
flavor that it was saved. The supported flavors
are: NumArray, CharArray,
Numeric, List,
Tuple, String,
Int or Float. Note that as
long as this method is not called, the actual array data
is resident on disk, not in memory.
4.10 The AttributeSet
class
Represents the set of attributes of a node (Leaf or
Group). It provides methods to create new attributes, open,
rename or delete existing ones.
Like in Group instances,
AttributeSet instances use a special feature
called natural naming, i.e. you can access the
attributes on disk like if they were normal
AttributeSet attributes. This offers the user
a very convenient way to access (but also set and delete)
node attributes by simply specifying them like a
normal attribute class.
Caveat: All Python data types
are supported. The scalar ones (i.e. String, Int and Float)
are mapped directly to the HDF5 counterparts, so you can
correctly visualize them with any HDF5 tool. However, the
rest of the data types and more general objects are
serialized using cPickle, so you will be able
to correctly retrieve them only from a Python-aware HDF5
library. Hopefully, the list of supported native attributes
will be extended to multidimensional arrays sometime in the
future.
4.10.1 AttributeSet instance
variables
- _v_node
- The parent node instance.
- _v_attrnames
- List with all attribute
names.
- _v_attrnamessys
- List with system attribute
names.
- _v_attrnamesuser
- List with user attribute
names.
4.10.2 AttributeSet
methods
Note that this class define the
__setattr__,
__getattr__ and
__delattr__ and
they work as normally intended. So, you can access, assign
or delete attributes on disk by just using the next
constructs:
leaf.attrs.myattr = "string attr" # Set the attribute myattr
attrib = leaf.attrs.myattr # Get the attribute myattr
del leaf.attrs.myattr # Delete the attribute myattr
- _f_list(attrset = "user")
- Return the list of attributes of the parent
node.
- attrset
- Selects the attribute set
to be returned. An "user" value
returns only the user attributes. This is the
default. "sys" returns only the
system (some of which are read-only)
attributes. "readonly" returns the
system read-only attributes. "all"
returns both the system and user
attributes.
- _f_rename(oldattrname,
newattrname)
- Rename an attribute.