Using .NET Collection Classes and Interfaces in Python Scripts

Some of the API functions specified above use .Net collection classes and interfaces, that is, Array class, IList interface, IEnumerable interface, and IDictionary interface. The following section describes how to work with the .Net collection objects in Python scripts.

.NET Array, IEnumerable, and IList objects can be indexed and iterated over as if they were Python lists. You can also check for membership using 'in'. To get .Net Array and IList sizes you can use Python's len or .Net Count.

Example:

Getting size:

arraySize = doubleDataArray.Count

arraySize = len(doubleDataArray)

 

listSize = sweepsNamesList.Count

listSize = len(sweepsNamesList)

Iterating:

for sweep in sweepsNamesList:

print sweep

for in in xrange(listSize)

print sweepsNamesList[i]

Checking for membership:

if 'Time' in sweepsNamesList:

doThis()

else:

doThat()

For .NET IDictionary, the same as for Array and IList, you can get size with len or Count and check for membership of the keys using in'. Getting values for the keys also works the same way as in Python dict.

Example

Getting size:

varValuesSize = varValues.Count

varValuesSize = len(varValues)

Checking for membership:

if 'offset' in varValues:

print varValues['offset']

Getting value:

if 'offset' in varValues:

offsetValue = varValues['offset']

As for iteration .NET Dictionary is different from Python dict. While iterating, Python dict will return keys, .Net Dictionary will return .Net KeyValuePair.

Example:

Iterating:

for .Net IDictionary:

for varPair in varValues: #varPair is of .Net KeyValuePair type

varName = varPair.Key

varValue = varPair.Value

 

for python dict:

for varName in varValues:

varValue = varValues[varName]

You can use Python types instead of .Net types if you prefer. For this you need to cast .Net Array and .Net iList to a Python list type and .Net Dictionary to a Python dict type.

Casting should not be used for data arrays - it can be extremely costly for the memory usage as well as time consuming.

Example:

aPythonList = list(dotNetArray)

aPythonList = list(dotNetList)

aPythonDict = dict(dotNetDictionary)

Related Topics 

User-Defined Outputs: Python Script API