IronPython Mini Cookbook
This topic presents simple counterparts between IronPython and VBScript. It does not provide a full tutorial on IronPython syntax. Because IronPython is a Python implementation, you can consult Python documentation for additional information.
Comments
|
VBScript |
IronPython |
|
Comments start with a single quote: ' comment |
Comments start with a hash: # comment |
Assigning/Creating Variables
|
VBScript |
IronPython |
|
Declare with a Dim: Dim doc Assignment then needs a Set instruction: Set doc = app.GetActiveProject() |
No Set syntax. Simply create and assign: doc = app.GetActiveProject() |
Create Lists/Arrays
|
VBScript |
IronPython |
|
Declare as array of String with 11 indices, from 0 through 10: Dim myArray(0 to 10) as String myArray(0) = "Hello" myArray(1) = "bye" Declare an array with no size: Dim array2() as String Re-dimension an array once size is known: ReDim array2(0 to 2) as String array2(0) = "this" array2(1) = "also" |
Declare an empty array: myEmptyArray = [] Declare an array and initialize it with 5 ints: myInitedArray = [ 1, 2, 3, 4, 5] Python lists can have items of any type and there is no pre-declaration. Declare an array and init with mixed types: mixed = ["hello", 1 ,2 ["nested"]] Append to an array: mixed.append( 3.5 ) |
Create Dictionaries/Maps
|
VBScript |
IronPython |
|
Declare with a Dim: Dim dict Use the CreateObject function with ProgID Scripting.Dictionary: Set dict = CreateObject _ ("Scripting.Dictionary") Add items using the object, key, item syntax: dObject.Add key, item |
An IronPython dictionary is a collection of name value pairs. Just like arrays, there is no restriction on the keys or the values. For purposes of Ansys EM scripting, however, all keys must be strings Delimiters are curly braces. Use a colon between the key and the value. Separate key value pairs with a comma: myDict = { "a" : 1, "b" : "hello there", "c" : [ 1, 2, "abc"] } |
Boolean Values
|
VBScript |
IronPython |
|
Boolean literals are in lower case: true false |
The first letter is capitalized: True False |
Converting Numbers to Strings and Vice Versa
|
VBScript |
IronPython |
|
Use CInt, CDbl, CBool, CLng to convert the string representation to the number representation. Use IsNumber to check before conversion: Dim nStr = "100" Dim n = CInt(nStr) Use CStr to convert a number to its string representation: Dim v, vStr v = 100 vStr = CStr(v) |
Use integer() or float() or double() functions to cast a string CONTAINING the string representation of whatever you are casting to: strInt = "3" intVal = int(strVal) floatVal = float(strVal) Invoke the str() function with the int/float values as needed. You can alternately use the string formatting method listed below: strVal = str(42) strVal = str(42.345) |
String Formatting/Concatenation
|
VBScript |
IronPython |
|
String concatenation uses the & operator: Dim allStr, str1 str1 = " how are you" allStr = "Hello " & " There" & str1 There seems to be no direct string formatting function in VBScript. Ssing string concatenation or using Replace are the two built-in options: Dim fmt = "{1} climbs stalk {2}" Dim str = Replace(fmt, "{1}", "jack") str = Replace(str, "{2"}, 10) |
If you have two strings, you can always concatenate them using the '+' operator: str1 = "hello" str2 = "world" str12 = str1 + " " + str2 If you have different types (for instance a string and an int), you must use the string formatting commands. When formatting multiple arguments, they must be entered as a tuple ( item1, item2, ): num = 10 str3 = "%s climbs stalk %d" % ("jack", num) str4 = "%d stalks" % num |
Looping over Lists
|
VBScript |
IronPython |
|
Dim myArray(0 to 2) as String myArray(0) = "alpha" myArray(1) = "bravo" myArray(2) = "charlie"
For Each i in myArray Print i Next |
vals = [1, 3, 3.456]
def process(val): return 2*val
for i in vals: print i print " -> " process(i) |
Looping over a Range
|
VBScript |
IronPython |
|
To loop over a range, specify start, end, and step: For i = 0 To 10 Step 1 Print i Next |
for i in range(0, 10): print i |