Python-Livecode Cheat Sheet
Comments
Comments allow you to add explanations and annotations to your code.
# this is commented out -- these
# are
// all
/* commented
out */
Variables
Variables are used to to store information, the stored value can be changed or accessed when
you need it.
var = "str" local tVar
var = 1 put "str" into tVar
put 1 into tVar
var["key"] = "val"
put "val" into tVar["key"]
String Processing
These examples show how string values can be manipulated.
# General // General
var = 'a' + var put "a" before tVar
var = var[1:] delete char 1 of tVar
var.replace("_", "-") replace "_" with "-" in tVar
# Regex // Regex
found = re.match('([0-9])', '1') matchText("1", "([0-9])", tN) is true
num = tMatch.group(1) tN is 1
for line in var: filter lines of tVar with regex pattern
if re.match(pattern, line): tPattern
filtered.push(line)
var = '\n'.join(filtered)
Custom Handlers
A custom handler is a function or command that you define yourself.
def foo(param): function foo pParam
# return something end foo
# foo(var) // get foo(tVar)
command bar pParam
end bar
// bar 5
Control Structures
Control structures are used to control what code is executed and how many times.
for x in tVar: repeat for each char tChar in tVar
# do things end repeat
for x in range(5): repeat 10
# do things end repeat
while x < 10: repeat with x = 1 to 10
x -= 1 end repeat
if tVar: repeat while x < 10
elif tOther: subtract 1 from x
else: end repeat
if true then ... else ...
if tVar then
else if tOther then
else
end if
switch tVar
case "a"
break
default
break
end switch
Sorting
These examples show how to sort items and lists.
list = [5, 2, 3, 1, 4] local tList
sorted(list) == [1, 2, 3, 4, 5] put "5,2,3,1,4" into tList
sorted(list, reverse=True) == [5, 4, 3, 2, 1] sort items of tList ascending numeric
-> tList is "1,2,3,4,5"
data = [(6, 1), (8, 3), (2, 2)] sort items of tList descending numeric
sorted(data, key=itemgetter(2)) == [(6, 1), -> tList is "5,4,3,2,1"
(2, 2), (8, 3)]
local tData
put "6,1:8,3:2,2" into tData
set the lineDelimiter to ":"
sort lines of tData ascending numeric by
item 2 of each
-> tData is "6,1:2,2:8,3"
Operators
Operators are ways of combining values such as boolean values, numbers or strings, to
produce other values.
# Logical // Logical
true and false == false true and false is false
true or false == true true or false is true
!false == true not false is true
# String // String
"foo" + "bar" == "foobar" "foo" & "bar" is "foobar"
strs = ['foo','bar'] "foo" && "bar" is "foo bar"
' '.join(strs) == "foo bar" "str" begins with "st"
"str".startswith("st") "str" ends with "g"
"str".endswith("g")
// Chunks
# Chunks char 5 of "str" is "n"
"str"[4:5] == "n" item 3 of "a,b,c" is "c"
word 1 of "hi there" is "hi"
items = "a,b,c".split(",") line 2 of "a" & return & "b" is "b"
items[2] == "c"
// Compound chunks
words = "hi there".split(" ") char 1 of item 1 of line 1 of "a,b,c" is "a"
words[0] == "hi"
lines = "a\nb".split("\n")
lines[2] == "b"
lines = "a,b,c".split("\n")
items = lines[1].split(",")
items[1][0:1] == "a"
Files & Processes
These examples show how to read from and write to files and processes.
open(tPath).read() get url("file:/" & tPath)
open(tPath).write("") put "" into url("file:/" & tPath)
process = subprocess.Popen([tProc], open process tProc
stdout=subprocess.PIPE) read from process tProc for 5
while True: close process tProc
process.wait()
data = process.stdout.read(5)
if data:
break
Array Processing
These examples show how array values can be manipulated.
# Split / combine // Split / combine
var = "a,b,c".split(",") put "a,b,c" into tVar
var[1] is "b" split tVar by ","
','.join(var) tVar[2] is "b"
var == "a,b,c" combine tVar with ","
tVar is "a,b,c"
# Iteration
for key in array: // Iteration
# do something with array[key] repeat for each key tKey in tArray
-- Do something with tArray[tKey]
# Length end repeat
len(array)
repeat for each element tElement in tArray
end repeat
// Length
the number of elements in tArray
User Input / Notification
These examples show how to pop up information dialogs, or prompts for user input.
dlg = wx.TextEntryDialog(None, "What is ask "What is your name?"
your name?", defaultValue=default_value) put it into tName
dlg.ShowModal()
name = dlg.GetValue() answer "Something"
dlg.Destroy()
dlg = wx.MessageDialog(None,
"Something", caption, wx.OK)
result = dlg.ShowModal()
dlg.Destroy()