472,145 Members | 1,618 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

transfer undefined class methods to attribute's method [Numeric array]

I am constructing a "placeholder array" to mimick Numeric arrays. I have a
3D regular structured domain, so I can describe the axis with 3 1D arrays
x[], y[] and z[]. All the variables defined on this field (temperature,
velocity) are 3D-fields, and to treat everything the same, I want to have
x,y and z also as a 3d-array. However, as one field is typically 100 Mb, I
do not want to create these arrays, but instead define a class that mimicks
this.

I have succeeded in doing this (see code below), but now I want to transfer
any unknown class methods to the in-line created variable, something like:

def process_unknownmethod(self, method, args)
exec(self[:].method(args))

How do I do this in python?

TIA, Maarten
Here's the class (sorry for the long class)

from Numeric import *

class PlaceHolder:
def __init__(self, axis, dims, arr):
self.axis = axis
self.arr = arr
self.dims = dims

def __interpretkey(self, axis, key):
if type(key) == int:
sl = slice(key, key+1,1)
else:
# interpret the slice object
start = key.start
if (start == None):
if not (key.stop == None):
start = key.stop
else:
start == 0
if start < 0:
if start > - self.dims[axis]:
start = self.dims[axis] + start
else:
start = 0

stop = key.stop
if stop == None or stop == sys.maxint:
stop = self.dims[axis]
if stop < 0:
if stop > - self.dims[axis]:
stop = self.dims[axis] + stop
else:
stop = 0

step = key.step
if not step: step = 1

sl = slice(start, stop, step)

return sl

def __getitem__(self, key):
sl = []

# if it's only one argument, make it into a tuple
if not ((type(key) == tuple) or (type(key) == list)):
key = (key,)

# determine ranges
for i, rng in enumerate(key):
sl.append(self.__interpretkey(i, rng))

# extend for missing dimensions
for i in range(len(key), len(self.dims)):
sl.append(slice(0, self.dims[i], 1))

# swap values for indices
sl[0], sl[self.axis] = sl[self.axis], sl[0]
base = self.arr[sl[0]]

# create an array
for n, i in enumerate(sl):
l = int(abs(float(i.stop - i.start)) / abs(i.step)+0.5)
if n > 0:
base = add.outer(base, zeros(l))

base = swapaxes(base, 0, self.axis)

# remove the dimensions of length 1

rdims = []
for i in range(rank(base)):
if base.shape[i] <= 1:
rdims.append(i)

rdims.sort()
rdims.reverse()

for i in rdims:
base = add.reduce(base, i)

return base
# example starts here

a = arange(10.)
y = PlaceHolder(1, [10, 10, 10], a)

print y[1, :, 10]
print y[1, 3]

--
================================================== =================
Maarten van Reeuwijk Thermal and Fluids Sciences
Phd student dept. of Multiscale Physics
www.ws.tn.tudelft.nl Delft University of Technology
Jul 18 '05 #1
0 1525

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Fernando Rodriguez | last post: by
4 posts views Thread by Maarten van Reeuwijk | last post: by
2 posts views Thread by Owen Woo | last post: by
8 posts views Thread by Joe | last post: by
4 posts views Thread by Terry Olsen | last post: by
reply views Thread by emin.shopper | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.