Forum: Poser Python Scripting


Subject: .sorry to bug pythonese about visual basic again ... but ... how to make multidi

elenorcoli opened this issue on Jun 09, 2007 · 5 posts


adp001 posted Sat, 09 June 2007 at 10:38 PM

Do we talk about VB or Python?

For Python there is NumPy (or numarray for older Python versions), specialiced on ultrafast math with arrays (use Google and look for a version compatible with your Python version).

Don't know if something similar is available for .NET (By the way: I'm using C# on windows from time to time, but for most things Python is simpler; C# is faster sometimes if no specialized library for Python is available).

You'll run into variable type trouble as soon as you try to do arithmetic with a string (what is read from a file is string or byte-string). But Python has a lot of fast conversion functions. One way to go is this:

...
first,data = line.split(None,2)

with this the line is splitted into 2 parts. The first character (a "v" for example) and the rest of the line (into data, the 3 remaining values as one string).

If the first parameter for split is , Python assumes whitespaces (tabs, blanks, lineend-characters).

To split the data into numeric types we can use:

x,y,z = map(float, data.split() )

map() is a function that "maps" the second sequence-parameter (the members of an array as a result from split()) to the function named as first parameter (float() is a function). So x,y,z is filled with the (float) values contained in data.

Because you know what kind of data will come in and that all data has to be computed in some way, you may use another function insteed of float(). Python knows round(value,dezimalplaces) for example. This function returns shortened the way you need it. But the value has to be a float allready. So you can't use it as a parameter for map(). One way out is to write a user-defined function:

def strRound(value,decimalplaces):
    return round(float(value),decimalplaces)

After this you may write:

x,y,z = map(strRound, data.split())

To compare 2 array-members with each other, you can go the simple way:

ar0 = [1,2,3]
ar1 = [1,2,3]
ar2 = [2,3,4]

ar0==ar1 is True
ar0==ar2 is False

For more complicated comparisons and arraymanipulations you may consider to write user-defined functions.

To walk through an array (to call one of your functions, for example), simply write:

for member in myarray: print myFunction(member)

That's all.