Component Scripting Help

I was pointed to a .proj file yesterday concerning csv manipulation, and the program is exactly what I’m looking for. I understand what the program is doing with the buttons, but if I were to want to use this idea again for a project of my own, I’m not sure I completely understand the scripting on one of the buttons. I was wondering if someone could help me out and maybe hit on some major points of this script or even point me to a walkthrough on how this process is started from the ground up. I’ve already been referring to the ignition handbook and the ‘official python help guide’ but I’m looking for something more straightforward.

I’m looking for a little help on the first three lines (where is it importing these things from?)
Right before the main for loop starts dataOut is defined as [], what does that mean? It’s an open ended dataset?
In the main body of the Code I understand that it is translating the text area into a csv format, but what does rowOut and dataOut do at the end of the body?

This is the Code:

[code]import StringIO
import csv
from java.text import SimpleDateFormat as SDF

dataIn=StringIO.StringIO(event.source.parent.getComponent(‘Text Area’).text)
reader=csv.reader(dataIn, delimiter=’,’, quotechar="’")

dataOut=[]
headerFlag=1
typeFlag=0
for row in reader:
element=[r.strip() for r in row]
if headerFlag==1:
headers=element
headerFlag=0
typeFlag=1
elif typeFlag==1:
types=element
typeFlag=0
print types
else:
rowList=list(element)
rowOut=[]
for i in range(len(element)):
type=types[i].strip()
if type==“Integer”:
dataPoint=(int(rowList[i]))
elif type==“Float”:
print rowList[i]
dataPoint=(float(rowList[i]))
elif type==“String”:
if str(rowList[i])[:2]==“u’”:
dataPoint=unicode(str(rowList[i])[2:-1])
else:
dataPoint=(str(rowList[i]))
elif type==“Boolean”:
dataPoint=((rowList[i]==‘True’))
elif type==“Date”:
inputFormat=SDF(“EEE MMM dd HH:mm:ss Z yyyy”)
dataPoint=SDF.parse(inputFormat,str(rowList[i]))
rowOut.append(dataPoint)
dataOut.append(rowOut)

event.source.parent.getComponent(‘Table’).data=system.dataset.toDataSet(headers,dataOut)[/code]

This may not be the best place to get this sort of help, but I figured this would be the best place to start! Thanks in Advance for the help!

Hi Alex,

To explain the first three lines, Python/Jython and Java (and a lot of others, but this is what we use here! :mrgreen: ) are very modular languages. There is a standard (for lack of a better word) set of instructions that you can use all the time. Then there are libraries, that extend (give more) functionality. To use them, they must be imported. Look at it as loading the library into memory along with your script. As soon as the script is done, it all goes away. So if you use a library, you will need to import it each time. Maybe that sounds a bit redundant. Sorry about that.

StringIO is a Pyhton library that lets you treat strings like files. If you read csv from a file, then you don’t really need this.
csv is a Python library to support-- you guessed it-- csv.
SimpleDateFormat is a Java library that provides a quick way to format dates from strings and vice versa. Since we use Jython, a java implementation of Python, we also get use of all the Java libraries as well! 8)

[] is an empty list. The data portion of a dataset can be expressed as a list of lists.

Gotta line call to take care of. More to come…

Thank you! Is there a database somewhere that I can look through these libraries?

I think this list applies to what is available to us in Ignition.

http://www.jython.org/docs/library/indexprogress.html