Chapter 1. Blocks

Table of Contents
Class: Block
Class: Record
Class: AppBlock
Class: CategoryAppBlock
Class: SortBlock
Class: Resource
Class: PrefBlock
Attributes
Functions

All objects and classes described here are located in the Pyrite.Blocks module. The contents of that module are imported into the base package, so everything described in this chapter can be accessed directly from the Pyrite module as well.

Class: Block

A Block object represents a generic chunk of data which can be stored in a Pyrite-accessible database. The Block class provides support for conversion between an internal representation (a string of raw data) and a more useful Python-friendly representation consisting of named fields.

Attributes

fields (dictionary of sequences)

Definitions of named fields. Each key is the name of a field; the corresponding value is a sequence (tuple or list) containing at least two elements. The first element is a constant specifying the type of the field, and the second element holds the field's default value. Elements beyond the first two are optional, and may be used to specify type-dependent information.

The available field types are:

FLD_UNKNOWN

A field of unknown type.

FLD_ANY

A field that may contain any value.

FLD_BOOLEAN

A boolean field. Its value will be treated as either true or false, no matter what it actually is. (By convention, boolean fields use a value of 1 to signify truth, and either 0 or None to signify falsity.)

FLD_INT

A field containing an integer.

FLD_FLOAT

A field containing a floating-point number.

FLD_TIME

A field containing a date/time value, in the normal Unix format of seconds since the epoch.

FLD_TIME_T

A field containing a date/time value, in the Unix time_t format (represented in Python by a 9-element tuple.

FLD_LIST

A field containing a list of values.

The field types are defined as constants at the module level.

Methods

__init__ (raw=None)

Create a Block object, setting its initial contents to the supplied raw string, then unpack it so the contents can be accessed as named fields.

unpack (raw)

Set the contents of the Block to the supplied string, and unpack it so the contents can be accessed as named fields.

pack

Pack the contents of the Block's named fields into a raw string, and return it.

packfields (format, names)

Pack the named fields into a raw string, according to the supplied format string (which should be in the format used by the standard struct module).

This function is intended as a helper to developers writing a custom pack function.

unpackfields (format, names, raw)

Unpack the supplied raw string according to the supplied format string (in the format used by the struct module), putting the values obtained into a series of named fields.

This function is intended as a helper to developers writing a custom unpack function.

__getitem__ (name), __setitem__ (name, value), get (name, default=None), keys, values, items, has_key (name)

Dictionary-like interface to named fields.

__len__

Pack the contents of the Block's named fields into a raw string, then return the length of that string.

dump (file=sys.stdout, verbose=0)

Print the raw contents of the Block to the specified file, in a hex-dump format. If verbose is true, print the unpacked values of the Block's named fields as well.