Cheatography

Python for Spike users Cheat Sheet (DRAFT) by seamus2389

This cheat sheet provides a overview of the aspects of the Python coding langauge useful to users of the Spike prototyping software

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Data Types

 String 'Hello', "­234­5", 'This is a string' Integer (Int) 2, 5, -7 Double (float) 2.1, 5.0, -7.778 Boolean True, False

Assignment operator

 x = y The value of y is assigned to the variable x

Comparsion operator

 x == y x is equal to y x != y x is not equal to y x < y x is less than y x > y x is greater than y x <= y x is less than or equal x >= y x is greater than or equal

Identity operators

 x is y x references the same data as y x is not y x does not reference the same data as y

Logical operators

 x == y And x > 0 If both the expres­sions are True then the ‘and’ operator returns True x== y Or x > 0 If either of the expres­sions are True then the ‘or’ operator returns True
See lesson 5 of Basics of Python for Spike users in the helpfiles

Variable Scope/­all­owable context

 command global interp­reter (work)unit cfb/em(eu) command procedural step/t­ran­sition unit NO YES YES - S88 em NO YES NO workUnit NO YES YES - S95 EU NO YES NO
See the 'Variable Scope' section of the help files.

Arithmetic Operators

 x + y add x - y subtract x * y multiply x / y divide x ** y x ^ y x % y modulus abs(x) absolute

Potential pitfalls

 object compar­sion* while* The while loop statement repeatedly executes a target statement as long as a given condition is true. However if while loops are used inappr­opr­iately they will trash CPU perfor­mance. In most cases the 'if' statement, waitFor function and timers would be more apprio­pate. rounding errors* Double values can only be approp­riate 'True' v 'true' In Spike, 'True' refers to a boolean value while 'true' is the name of an object
*See the potential progra­mming pitfalls section of the help files for more detail

Useful Spike functions

 opc('N­ame­OfO­bject') Access an object in the opc model opc('N­ame­OfO­bject'). The '.' operator provides access to all commands and properties of the object unit. Provides access to the commands and properties of the current unit unit.Suppor­tModule Provides access to unit parame­ters, timer and counters unit.Messa­geM­odule Provides access to create messages, prompts and call work instru­ctions em. Provides access to the commands and properties of the current equipment module info() Prints a string variable to the console tab str() Converts an object into a string waitFo­r(c­ond­ition met) Wait for the value of given property on given module to equal given condition sleep(­dur­ation) Put current script to sleep for duration seconds resetM­odu­les() Reset parent associated modules firstScan do something when running a script the first time

Useful code snippets

 \$creat­etimer Recomm­ended method of creating a timer \$creat­eco­unter Recomm­ended method of creating a counter
\$ calls various pre-cr­eated code snippets which can be viewed in the snippets folder of the Type Explorer

Statements

 If Statement if condition met: do Something elif other condition met: do Something Else else: do Something Else While Loop while condition is met: do Something For Loop For variable in collection: do Something with variable