PyTables User's Guide: Hierarchical datasets in Python - Release 1.3.3
Prev | Chapter 4. Library Reference | Next

# 4.17. Helper classes

In this section are listed classes that does not fit in any other section and that mainly serve for ancillary purposes.

## 4.17.1. The Filters class

This class is meant to serve as a container that keeps information about the filter properties associated with the enlargeable leaves, that is `Table`, `EArray` and `VLArray` as well as `CArray`.

The public variables of `Filters` are listed below:

complevel

  The compression level (0 means no compression).

complib

  The compression filter used (in case of compressed dataset).

shuffle

  Whether the shuffle filter is active or not.

fletcher32

  Whether the fletcher32 filter is active or not.

There are no `Filters` public methods with the exception of the constructor itself that is described next.

### 4.17.1.1. Filters(complevel=0, complib="zlib", shuffle=1, fletcher32=0)

The parameters that can be passed to the `Filters` class constructor are:

complevel

  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"`, `"ucl"` and `"bzip2"` values are supported. See section 5.3 for some advice on which library is better suited to your needs.

shuffle

  Whether or not to use the shuffle filter present in the `HDF5` library. This is normally used to improve the compression ratio (at the cost of consuming a little bit more CPU time). A value of 0 disables shuffling and 1 makes it active. The default value depends on whether compression is enabled or not; if compression is enabled, shuffling defaults to be active, else shuffling is disabled.

fletcher32

  Whether or not to use the fletcher32 filter in the HDF5 library. This is used to add a checksum on each data chunk. A value of 0 disables the checksum and it is the default.

Of course, you can also create an instance and then assign the ones you want to change. For example:

    import numarray as na
    from tables import *

    fileh = openFile("test5.h5", mode = "w")
    atom = Float32Atom(shape=(0,2))
    filters = Filters(complevel=1, complib = "lzo")
    filters.fletcher32 = 1
    arr = fileh.createEArray(fileh.root, 'earray', atom, "A growable array",
                             filters = filters)
    # Append several rows in only one call
    arr.append(na.array([[1., 2.],
                         [2., 3.],
                         [3., 4.]], type=na.Float32))

    # Print information on that enlargeable array
    print "Result Array:"
    print repr(arr)

    fileh.close()

This enforces the use of the `LZO` library, a compression level of 1 and a fletcher32 checksum filter as well. See the output of this example:

    Result Array:
    /earray (EArray(3L, 2), fletcher32, shuffle, lzo(1)) 'A growable array'
      type = Float32
      shape = (3L, 2)
      itemsize = 4
      nrows = 3
      extdim = 0
      flavor = 'numarray'
      byteorder = 'little'

## 4.17.2. The IndexProps class

You can use this class to set/unset the properties in the indexing process of a `Table` column. To use it, create an instance, and assign it to the special attribute `_v_indexprops` in a table description class (see 4.16.1) or dictionary.

The public variables of `IndexProps` are listed below:

auto

  Whether an existing index should be updated or not after a table append operation.

reindex

  Whether the table columns are to be re-indexed after an invalidating index operation.

filters

  The filter settings for the different `Table` indexes.

There are no `IndexProps` public methods with the exception of the constructor itself that is described next.

### 4.17.2.1. IndexProps(auto=1, reindex=1, filters=None)

The parameters that can be passed to the `IndexProps` class constructor are:

auto

  Specifies whether an existing index should be updated or not after a table append operation. The default is enable automatic index updates.

reindex

  Specifies whether the table columns are to be re-indexed after an invalidating index operation (like for example, after a `Table.removeRows` call). The default is to reindex after operations that invalidate indexes.

filters

  Sets the filter properties for `Column` indexes. It has to be an instance of the `Filters` (see section 4.17.1) class. A `None` value means that the default settings for the `Filters` object are selected.

## 4.17.3. The Index class

This class is used to keep the indexing information for table columns. It is actually a descendant of the `Group` class, with some added functionality.

It has no methods intended for programmer's use, but it has some attributes that maybe interesting for him.

### 4.17.3.1. Index instance variables

column

  The column object this index belongs to.

type

  The type class for the index.

itemsize

  The size of the atomic items. Specially useful for columns of `CharType` type.

nelements

  The total number of elements in index.

dirty

  Whether the index is dirty or not.

filters

  The `Filters` (see section 4.17.1) instance for this index.

## 4.17.4. The Enum class

Each instance of this class represents an enumerated type. The values of the type must be declared exhaustively and named with strings, and they might be given explicit concrete values, though this is not compulsory. Once the type is defined, it can not be modified.

There are three ways of defining an enumerated type. Each one of them corresponds to the type of the only argument in the constructor of `Enum`:

- Sequence of names: each enumerated value is named using a string, and its order is determined by its position in the sequence; the concrete value is assigned automatically:

      >>> boolEnum = Enum(['True', 'False'])

- Mapping of names: each enumerated value is named by a string and given an explicit concrete value. All of the concrete values must be different, or a `ValueError` will be raised.

      >>> priority = Enum({'red': 20, 'orange': 10, 'green': 0})
      >>> colors = Enum({'red': 1, 'blue': 1})
      Traceback (most recent call last):
        ...
      ValueError: enumerated values contain duplicate concrete values: 1

- Enumerated type: in that case, a copy of the original enumerated type is created. Both enumerated types are considered equal.

      >>> prio2 = Enum(priority)
      >>> priority == prio2
      True

Please, note that names starting with `_` are not allowed, since they are reserved for internal usage:

    >>> prio2 = Enum(['_xx'])
    Traceback (most recent call last):
      ...
    ValueError: name of enumerated value can not start with ``_``: '_xx'

The concrete value of an enumerated value is obtained by getting its name as an attribute of the `Enum` instance (see `__getattr__()`) or as an item (see `__getitem__()`). This allows comparisons between enumerated values and assigning them to ordinary Python variables:

    >>> redv = priority.red
    >>> redv == priority['red']
    True
    >>> redv > priority.green
    True
    >>> priority.red == priority.orange
    False

The name of the enumerated value corresponding to a concrete value can also be obtained by using the `__call__()` method of the enumerated type. In this way you get the symbolic name to use it later with `__getitem__()`:

    >>> priority(redv)
    'red'
    >>> priority.red == priority[priority(priority.red)]
    True

(If you ask, the `__getitem__()` method is not used for this purpose to avoid ambiguity in the case of using strings as concrete values.)

### 4.17.4.1. Special methods

__getitem__(name)

  Get the concrete value of the enumerated value with that `name`.

  The `name` of the enumerated value must be a string. If there is no value with that `name` in the enumeration, a `KeyError` is raised.

__getattr__(name)

  Get the concrete value of the enumerated value with that `name`.

  The `name` of the enumerated value must be a string. If there is no value with that `name` in the enumeration, an `AttributeError` is raised.

__contains__(name)

  Is there an enumerated value with that `name` in the type?

  If the enumerated type has an enumerated value with that `name`, `True` is returned. Otherwise, `False` is returned. The `name` must be a string.

  This method does not check for concrete values matching a value in an enumerated type. For that, please use the `__call__()` method.

__call__(value, *default)

  Get the name of the enumerated value with that concrete `value`.

  If there is no value with that concrete value in the enumeration and a second argument is given as a `default`, this is returned. Else, a `ValueError` is raised.

  This method can be used for checking that a concrete value belongs to the set of concrete values in an enumerated type.

__len__()

  Return the number of enumerated values in the enumerated type.

__iter__()

  Iterate over the enumerated values.

  Enumerated values are returned as `(name, value)` pairs in no particular order.

__eq__(other)

  Is the `other` enumerated type equivalent to this one?

  Two enumerated types are equivalent if they have exactly the same enumerated values (i.e. with the same names and concrete values).

__repr__()

  Return the canonical string representation of the enumeration. The output of this method can be evaluated to give a new enumeration object that will compare equal to this one.

Prev | Home | Next
Declarative classes | Up | Optimization tips