earray2.py :  » Database » PyTables » tables-2.1.2 » examples » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Database » PyTables 
PyTables » tables 2.1.2 » examples » earray2.py
#!/usr/bin/env python

""" Small example that shows how to work with extendeable arrays of
different types, strings included. """

import sys
from numpy import *
from tables import *

# Open a new empty HDF5 file
filename = "earray2.h5"
fileh = openFile(filename, mode = "w")
# Get the root group
root = fileh.root

# Create an string atom
a = StringAtom(itemsize=1)
# Use it as a type for the enlargeable array
hdfarray = fileh.createEArray(root, 'array_c', a, (0,), "Character array")
hdfarray.append(array(['a', 'b', 'c']))
# The next is legal:
hdfarray.append(array(['c', 'b', 'c', 'd']))
# but these are not:
#hdfarray.append(array([['c', 'b'], ['c', 'd']]))
#hdfarray.append(array([[1,2,3],[3,2,1]], dtype=uint8).reshape(2,1,3))

# Create an atom
a = UInt16Atom()
hdfarray = fileh.createEArray(root, 'array_e', a, (2,0,3),
                              "Unsigned short array")

# Create an enlargeable array
a = UInt8Atom()
hdfarray = fileh.createEArray(root, 'array_b', a, (2,0,3),
                              "Unsigned byte array", Filters(complevel = 1))

# Append an array to this table
hdfarray.append(array([[1,2,3],[3,2,1]], dtype=uint8).reshape(2,1,3))
hdfarray.append(array([[1,2,3],[3,2,1],[2,4,6],[6,4,2]],
                      dtype=uint8).reshape(2,2,3)*2)
# The next should give a type error:
#hdfarray.append(array([[1,0,1],[0,0,1]], dtype=Bool).reshape(2,1,3))

# Close the file
fileh.close()

# Open the file for reading
fileh = openFile(filename, mode = "r")
# Get the root group
root = fileh.root

a = root.array_c.read()
print "Character array -->",repr(a), a.shape
a = root.array_e.read()
print "Empty array (yes, this is suported) -->",repr(a), a.shape
a = root.array_b.read(step=2)
print "Int8 array, even rows (step = 2) -->",repr(a), a.shape

print "Testing iterator:",
#for x in root.array_b.iterrows(step=2):
for x in root.array_b:
    print "nrow-->", root.array_b.nrow
    print "Element-->",x

print "Testing getitem:"
for i in range(root.array_b.shape[0]):
    print "array_b["+str(i)+"]", "-->", root.array_b[i]
# The nrows counts the growing dimension, which is different from the
# first index
for i in range(root.array_b.nrows):
    print "array_b[:,"+str(i)+",:]", "-->", root.array_b[:,i,:]
print "array_c[1:2]", repr(root.array_c[1:2])
print "array_c[1:3]", repr(root.array_c[1:3])
print "array_b[:]", root.array_b[:]

print repr(root.array_c)
# Close the file
fileh.close()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.