1
########################################################################
4
# Created: December 16, 2004
5
# Author: Ivan Vilata i Balaguer - reverse:com.carabos@ivilata
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 $
10
########################################################################
12
"""Here are defined some declarative classes for VLArray components
14
See *Atom docstrings for more info.
18
Atom, ObjectAtom, VLStringAtom, StringAtom, BoolAtom,
19
IntAtom, Int8Atom, UInt8Atom, Int16Atom, UInt16Atom,
20
TimeAtom, Time32Atom, Time64Atom
34
import numarray.records as records
36
from tables.IsDescription import \
37
Col, BoolCol, StringCol, IntCol, FloatCol, ComplexCol, TimeCol, EnumCol
41
__version__ = "$Revision: 1009 $"
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"]:
52
"""flavor of type '%s' must be one of the "CharArray" or "String" values, and you tried to set it to "%s".
55
if flavor in ["NumArray", "Numeric", "Tuple", "List"]:
59
"""flavor of type '%s' must be one of the "NumArray", "Numeric", "Tuple" or "List" values, and you tried to set it to "%s".
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 """
69
# This special strings will be represented by unsigned bytes
70
IntCol.__init__(self, itemsize=1, shape=1, sign=0)
71
self.flavor = "VLString"
77
" Compute the item size of the VLStringAtom "
78
# Always return 1 because strings are saved in UTF-8 format
82
class ObjectAtom(IntCol):
83
""" Define an atom of type Object """
85
IntCol.__init__(self, shape=1, itemsize=1, sign=0)
86
self.flavor = "Object"
92
" Compute the item size of the Object "
93
# Always return 1 because strings are saved in UInt8 format
98
#class Atom(IsDescription.BaseCol):
100
""" Define an Atomic object to be used in VLArray objects """
102
def __init__(self, dtype="Float64", shape=1, flavor="NumArray"):
103
Col.__init__(self, dtype, shape)
104
self.flavor = checkflavor(flavor, self.type)
107
if self.type == "CharType" or isinstance(self.type, records.Char):
109
shape = [self.itemsize]
111
shape = list(self.shape)
112
shape.append(self.itemsize)
117
return "Atom(dtype=%r, shape=%s, flavor=%r)" % (
118
self.stype, shape, self.flavor)
121
" Compute the size of the atom type "
122
atomicsize = self.itemsize
123
if isinstance(self.shape, tuple):
125
if i > 0: # To deal with EArray Atoms
128
atomicsize *= self.shape
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)
138
return "StringAtom(shape=%s, length=%s, flavor=%r)" % (
139
self.shape, self.itemsize, self.flavor)
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)
148
return "BoolAtom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
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)
157
if numarray.array(0, self.type) - numarray.array(1, self.type) < 0:
161
return "IntAtom(shape=%s, itemsize=%s, sign=%s, flavor=%r)" % (
162
self.shape, self.itemsize, sign, self.flavor)
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)
169
return "Int8Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
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)
176
return "UInt8Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
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)
183
return "Int16Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
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)
190
return "UInt16Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
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)
197
return "Int32Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
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)
204
return "UInt32Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
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)
211
return "Int64Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
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)
218
return "UInt64Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
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)
227
return "FloatAtom(shape=%s, itemsize=%s, flavor=%r)" % (
228
self.shape, self.itemsize, self.flavor)
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)
235
return "Float32Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
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)
242
return "Float64Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
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)
251
return "ComplexAtom(shape=%s, itemsize=%s, flavor=%r)" % (
252
self.shape, self.itemsize, self.flavor)
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)
259
return "Complex32Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
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)
266
return "Complex64Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
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)
275
return "TimeAtom(shape=%s, itemsize=%s, flavor=%r)" % (
276
self.shape, self.itemsize, self.flavor)
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)
283
return "Time32Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
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)
290
return "Time64Atom(shape=%s, flavor=%r)" % (self.shape, self.flavor)
294
class EnumAtom(EnumCol, Atom):
297
Description of an atom of an enumerated type.
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.
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).
307
Enumerated atoms also have ``stype`` and ``type`` attributes with
308
the same values as in `EnumCol`.
310
Save for the default, position and indexed attributes, examples from
311
the `Enum` class hold (changing `EnumCol` by `EnumAtom`, of course).
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)
319
def _setDefault(self, dflt):
320
# Atoms do not need default values.
325
return ('EnumAtom(%s, dtype=\'%s\', shape=%s, flavor=%r)'
326
% (self.enum, self.type, self.shape, self.flavor))
332
## py-indent-offset: 4