~ubuntu-branches/ubuntu/quantal/pytables/quantal

« back to all changes in this revision

Viewing changes to tables/Atom.py

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2005-11-27 20:25:34 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20051127202534-l8jzyd8357krw40h
Tags: 1.1.1-1ubuntu1
* Sync with Debian:
  + Use python 2.4 as default

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
########################################################################
 
2
#
 
3
#       License: BSD
 
4
#       Created: December 16, 2004
 
5
#       Author:  Ivan Vilata i Balaguer - reverse:com.carabos@ivilata
 
6
#
 
7
#       $Source: /home/ivan/_/programari/pytables/svn/cvs/pytables/pytables/tables/Atom.py,v $
 
8
#       $Id: Atom.py 1009 2005-06-15 13:39:15Z faltet $
 
9
#
 
10
########################################################################
 
11
 
 
12
"""Here are defined some declarative classes for VLArray components
 
13
 
 
14
See *Atom docstrings for more info.
 
15
 
 
16
Classes:
 
17
 
 
18
    Atom, ObjectAtom, VLStringAtom, StringAtom, BoolAtom,
 
19
    IntAtom, Int8Atom, UInt8Atom, Int16Atom, UInt16Atom,
 
20
    TimeAtom, Time32Atom, Time64Atom
 
21
 
 
22
Functions:
 
23
 
 
24
   checkflavor
 
25
 
 
26
Misc variables:
 
27
 
 
28
    __version__
 
29
 
 
30
 
 
31
"""
 
32
 
 
33
import numarray
 
34
import numarray.records as records
 
35
 
 
36
from tables.IsDescription import \
 
37
     Col, BoolCol, StringCol, IntCol, FloatCol, ComplexCol, TimeCol, EnumCol
 
38
 
 
39
 
 
40
 
 
41
__version__ = "$Revision: 1009 $"
 
42
 
 
43
 
 
44
 
 
45
def checkflavor(flavor, dtype):
 
46
    #if dtype == "CharType" or isinstance(dtype, records.Char):
 
47
    if str(dtype) == "CharType":
 
48
        if flavor in ["CharArray", "String"]:
 
49
            return flavor
 
50
        else:
 
51
            raise ValueError, \
 
52
"""flavor of type '%s' must be one of the "CharArray" or "String" values, and you tried to set it to "%s".
 
53
"""  % (dtype, flavor)
 
54
    else:
 
55
        if flavor in ["NumArray", "Numeric", "Tuple", "List"]:
 
56
            return flavor
 
57
        else:
 
58
            raise ValueError, \
 
59
"""flavor of type '%s' must be one of the "NumArray", "Numeric", "Tuple" or "List" values, and you tried to set it to "%s".
 
60
"""  % (dtype, flavor)
 
61
 
 
62
 
 
63
 
 
64
# Class to support variable length strings as components of VLArray
 
65
# It supports UNICODE strings as well.
 
66
class VLStringAtom(IntCol):
 
67
    """ Define an atom of type Variable Length String """
 
68
    def __init__(self):
 
69
        # This special strings will be represented by unsigned bytes
 
70
        IntCol.__init__(self, itemsize=1, shape=1, sign=0)
 
71
        self.flavor = "VLString"
 
72
 
 
73
    def __repr__(self):
 
74
        return "VLString()"
 
75
 
 
76
    def atomsize(self):
 
77
        " Compute the item size of the VLStringAtom "
 
78
        # Always return 1 because strings are saved in UTF-8 format
 
79
        return 1
 
80
 
 
81
 
 
82
class ObjectAtom(IntCol):
 
83
    """ Define an atom of type Object """
 
84
    def __init__(self):
 
85
        IntCol.__init__(self, shape=1, itemsize=1, sign=0)
 
86
        self.flavor = "Object"
 
87
 
 
88
    def __repr__(self):
 
89
        return "Object()"
 
90
 
 
91
    def atomsize(self):
 
92
        " Compute the item size of the Object "
 
93
        # Always return 1 because strings are saved in UInt8 format
 
94
        return 1
 
95
 
 
96
 
 
97
 
 
98
#class Atom(IsDescription.BaseCol):
 
99
class Atom(Col):
 
100
    """ Define an Atomic object to be used in VLArray objects """
 
101
 
 
102
    def __init__(self, dtype="Float64", shape=1, flavor="NumArray"):
 
103
        Col.__init__(self, dtype, shape)
 
104
        self.flavor = checkflavor(flavor, self.type)
 
105
 
 
106
    def __repr__(self):
 
107
        if self.type == "CharType" or isinstance(self.type, records.Char):
 
108
            if self.shape == 1:
 
109
                shape = [self.itemsize]
 
110
            else:
 
111
                shape = list(self.shape)
 
112
                shape.append(self.itemsize)
 
113
            shape = tuple(shape)
 
114
        else:
 
115
            shape = self.shape
 
116
 
 
117
        return "Atom(dtype=%r, shape=%s, flavor=%r)" % (
 
118
            self.stype, shape, self.flavor)
 
119
 
 
120
    def atomsize(self):
 
121
        " Compute the size of the atom type "
 
122
        atomicsize = self.itemsize
 
123
        if isinstance(self.shape, tuple):
 
124
            for i in self.shape:
 
125
                if i > 0:  # To deal with EArray Atoms
 
126
                    atomicsize *= i
 
127
        else:
 
128
            atomicsize *= self.shape
 
129
        return atomicsize
 
130
 
 
131
 
 
132
class StringAtom(StringCol, Atom):
 
133
    """ Define an atom of type String """
 
134
    def __init__(self, shape=1, length=None, flavor="CharArray"):
 
135
        StringCol.__init__(self, length=length, shape=shape)
 
136
        self.flavor = checkflavor(flavor, self.type)
 
137
    def __repr__(self):
 
138
        return "StringAtom(shape=%s, length=%s, flavor=%r)" % (
 
139
            self.shape, self.itemsize, self.flavor)
 
140
 
 
141
 
 
142
class BoolAtom(BoolCol, Atom):
 
143
    """ Define an atom of type Bool """
 
144
    def __init__(self, shape=1, flavor="NumArray"):
 
145
        BoolCol.__init__(self, shape=shape)
 
146
        self.flavor = checkflavor(flavor, self.type)
 
147
    def __repr__(self):
 
148
        return "BoolAtom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
 
149
 
 
150
 
 
151
class IntAtom(IntCol, Atom):
 
152
    """ Define an atom of type Integer """
 
153
    def __init__(self, shape=1, itemsize=4, sign=1, flavor="NumArray"):
 
154
        IntCol.__init__(self, shape=shape, itemsize=itemsize, sign=sign)
 
155
        self.flavor = checkflavor(flavor, self.type)
 
156
    def __repr__(self):
 
157
        if numarray.array(0, self.type) - numarray.array(1, self.type) < 0:
 
158
            sign = 1
 
159
        else:
 
160
            sign = 0
 
161
        return "IntAtom(shape=%s, itemsize=%s, sign=%s, flavor=%r)" % (
 
162
            self.shape, self.itemsize, sign, self.flavor)
 
163
 
 
164
class Int8Atom(IntAtom):
 
165
    """ Define an atom of type Int8 """
 
166
    def __init__(self, shape=1, flavor="NumArray"):
 
167
        IntAtom.__init__(self, shape=shape, itemsize=1, sign=1, flavor=flavor)
 
168
    def __repr__(self):
 
169
        return "Int8Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
 
170
 
 
171
class UInt8Atom(IntAtom):
 
172
    """ Define an atom of type UInt8 """
 
173
    def __init__(self, shape=1, flavor="NumArray"):
 
174
        IntAtom.__init__(self, shape=shape, itemsize=1, sign=0, flavor=flavor)
 
175
    def __repr__(self):
 
176
        return "UInt8Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
 
177
 
 
178
class Int16Atom(IntAtom):
 
179
    """ Define an atom of type Int16 """
 
180
    def __init__(self, shape=1, flavor="NumArray"):
 
181
        IntAtom.__init__(self, shape=shape, itemsize=2, sign=1, flavor=flavor)
 
182
    def __repr__(self):
 
183
        return "Int16Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
 
184
 
 
185
class UInt16Atom(IntAtom):
 
186
    """ Define an atom of type UInt16 """
 
187
    def __init__(self, shape=1, flavor="NumArray"):
 
188
        IntAtom.__init__(self, shape=shape, itemsize=2, sign=0, flavor=flavor)
 
189
    def __repr__(self):
 
190
        return "UInt16Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
 
191
 
 
192
class Int32Atom(IntAtom):
 
193
    """ Define an atom of type Int32 """
 
194
    def __init__(self, shape=1, flavor="NumArray"):
 
195
        IntAtom.__init__(self, shape=shape, itemsize=4, sign=1, flavor=flavor)
 
196
    def __repr__(self):
 
197
        return "Int32Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
 
198
 
 
199
class UInt32Atom(IntAtom):
 
200
    """ Define an atom of type UInt32 """
 
201
    def __init__(self, shape=1, flavor="NumArray"):
 
202
        IntAtom.__init__(self, shape=shape, itemsize=4, sign=0, flavor=flavor)
 
203
    def __repr__(self):
 
204
        return "UInt32Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
 
205
 
 
206
class Int64Atom(IntAtom):
 
207
    """ Define an atom of type Int64 """
 
208
    def __init__(self, shape=1, flavor="NumArray"):
 
209
        IntAtom.__init__(self, shape=shape, itemsize=8, sign=1, flavor=flavor)
 
210
    def __repr__(self):
 
211
        return "Int64Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
 
212
 
 
213
class UInt64Atom(IntAtom):
 
214
    """ Define an atom of type UInt64 """
 
215
    def __init__(self, shape=1, flavor="NumArray"):
 
216
        IntAtom.__init__(self, shape=shape, itemsize=8, sign=0, flavor=flavor)
 
217
    def __repr__(self):
 
218
        return "UInt64Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
 
219
 
 
220
 
 
221
class FloatAtom(FloatCol, Atom):
 
222
    """ Define an atom of type Float """
 
223
    def __init__(self, shape=1, itemsize=8, flavor="NumArray"):
 
224
        FloatCol.__init__(self, shape=shape, itemsize=itemsize)
 
225
        self.flavor = checkflavor(flavor, self.type)
 
226
    def __repr__(self):
 
227
        return "FloatAtom(shape=%s, itemsize=%s, flavor=%r)" % (
 
228
            self.shape, self.itemsize, self.flavor)
 
229
 
 
230
class Float32Atom(FloatAtom):
 
231
    """ Define an atom of type Float32 """
 
232
    def __init__(self, shape=1, flavor="NumArray"):
 
233
        FloatAtom.__init__(self, shape=shape, itemsize=4, flavor=flavor)
 
234
    def __repr__(self):
 
235
        return "Float32Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
 
236
 
 
237
class Float64Atom(FloatAtom):
 
238
    """ Define an atom of type Float64 """
 
239
    def __init__(self, shape=1, flavor="NumArray"):
 
240
        FloatAtom.__init__(self, shape=shape, itemsize=8, flavor=flavor)
 
241
    def __repr__(self):
 
242
        return "Float64Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
 
243
 
 
244
 
 
245
class ComplexAtom(ComplexCol, Atom):
 
246
    """ Define an atom of type Complex """
 
247
    def __init__(self, shape=1, itemsize=16, flavor="NumArray"):
 
248
        ComplexCol.__init__(self, shape=shape, itemsize=itemsize)
 
249
        self.flavor = checkflavor(flavor, self.type)
 
250
    def __repr__(self):
 
251
        return "ComplexAtom(shape=%s, itemsize=%s, flavor=%r)" % (
 
252
            self.shape, self.itemsize, self.flavor)
 
253
 
 
254
class Complex32Atom(ComplexAtom):
 
255
    """ Define an atom of type Complex32 """
 
256
    def __init__(self, shape=1, flavor="NumArray"):
 
257
        ComplexAtom.__init__(self, shape=shape, itemsize=8, flavor=flavor)
 
258
    def __repr__(self):
 
259
        return "Complex32Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
 
260
 
 
261
class Complex64Atom(ComplexAtom):
 
262
    """ Define an atom of type Complex64 """
 
263
    def __init__(self, shape=1, flavor="NumArray"):
 
264
        ComplexAtom.__init__(self, shape=shape, itemsize=16, flavor=flavor)
 
265
    def __repr__(self):
 
266
        return "Complex64Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
 
267
 
 
268
 
 
269
class TimeAtom(TimeCol, Atom):
 
270
    """ Define an atom of type Time """
 
271
    def __init__(self, shape=1, itemsize=8, flavor="NumArray"):
 
272
        TimeCol.__init__(self, shape=shape, itemsize=itemsize)
 
273
        self.flavor = checkflavor(flavor, self.type)
 
274
    def __repr__(self):
 
275
        return "TimeAtom(shape=%s, itemsize=%s, flavor=%r)" % (
 
276
            self.shape, self.itemsize, self.flavor)
 
277
 
 
278
class Time32Atom(TimeAtom):
 
279
    """ Define an atom of type Time32 """
 
280
    def __init__(self, shape=1, flavor="NumArray"):
 
281
        TimeAtom.__init__(self, shape=shape, itemsize=4, flavor=flavor)
 
282
    def __repr__(self):
 
283
        return "Time32Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
 
284
 
 
285
class Time64Atom(TimeAtom):
 
286
    """ Define an atom of type Time64 """
 
287
    def __init__(self, shape=1, flavor="NumArray"):
 
288
        TimeAtom.__init__(self, shape=shape, itemsize=8, flavor=flavor)
 
289
    def __repr__(self):
 
290
        return "Time64Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
 
291
 
 
292
 
 
293
 
 
294
class EnumAtom(EnumCol, Atom):
 
295
 
 
296
    """
 
297
    Description of an atom of an enumerated type.
 
298
 
 
299
    Instances of this class describe the atom type used by an array to
 
300
    store enumerated values.  Those values belong to an enumerated type.
 
301
 
 
302
    The meaning of the ``enum`` and ``dtype`` arguments is the same as
 
303
    in `EnumCol`.  The ``shape`` and ``flavor`` arguments have the usual
 
304
    meaning of other `Atom` classes (the ``flavor`` applies to the
 
305
    representation of concrete read values).
 
306
 
 
307
    Enumerated atoms also have ``stype`` and ``type`` attributes with
 
308
    the same values as in `EnumCol`.
 
309
 
 
310
    Save for the default, position and indexed attributes, examples from
 
311
    the `Enum` class hold (changing `EnumCol` by `EnumAtom`, of course).
 
312
    """
 
313
 
 
314
    def __init__(self, enum, dtype='UInt32', shape=1, flavor='NumArray'):
 
315
        EnumCol.__init__(self, enum, None, dtype=dtype, shape=shape)
 
316
        self.flavor = checkflavor(flavor, self.type)
 
317
 
 
318
 
 
319
    def _setDefault(self, dflt):
 
320
        # Atoms do not need default values.
 
321
        self.dflt = None
 
322
 
 
323
 
 
324
    def __repr__(self):
 
325
        return ('EnumAtom(%s, dtype=\'%s\', shape=%s, flavor=%r)'
 
326
                % (self.enum, self.type, self.shape, self.flavor))
 
327
 
 
328
 
 
329
 
 
330
## Local Variables:
 
331
## mode: python
 
332
## py-indent-offset: 4
 
333
## tab-width: 4
 
334
## fill-column: 72
 
335
## End: