CardExchange Solutions Documentation

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Current »

The Functions tab in the expressions window shows a single text box where you can type any global definitions you want to use in your Python scripts. When typing here, you need to follow the Python syntax (see http://www.python.org). Normally, you will use this space to define custom functions to be used in expressions.


An example shown before was the function we defined to determine the background color

def getcolor(flag):
if flag == 'A':
return 'Red'
elif flag == 'B':
return 'Green'
else:
return 'Blue'

It is also possible to define global variables, that is, variables that can be used in expressions or functions, but that will not be listed under the visible items. For example, we could change the above function definition to

colorA = 'Red'
colorB = 'Green'
colorC = 'Blue'

def getcolor(flag):
if flag == 'A':
return colorA
elif flag == 'B':
return colorB
else:
return colorC

To get a taste of the level of complexity you can achieve with custom functions, see the below definition of a function that converts a string to name casing. It uses the regular-expressions module of Python (see http://www.python.org for more information).

import re

def namecase(s):
return re.sub('\w+', capitalizematch, s)

def capitalizematch(m):
return capitalize(m.group(0))

def capitalize(s):
if len(s) > 1:
return s[:1].upper() + s[1:].lower()
elif len(s) == 1:
return s.upper()
else:
return s

With the name-case function, you can convert a string like 'jOHn SMith' into 'John Smith'.


  • No labels