Working With Different Datatypes
You'll encounter lots of different datatypes when scripting in Python.
This guide can help you through the snags of working with some of the more complex types. The University Video shows how you can work with colors and dates in the scripting language.
Numbers
Working with numbers is very easy in Python, and requires no special considerations. You can use the built-in function int() to attempt to coerce values to integers, and float() to coerce values to floating-point values. Both will throw ValueError if the coercion fails.
If you are new to programming, the following might throw you off. Python, like nearly all programming languages, uses integer division when dividing two integers. This means that 1/2 will result in 0. This is because both 1 and 2 are integers, so the result of the division must be an integer. The result of 0.5 gets truncated to 0. If either operand is a float, the result will be a float, so 1/2.0 will result in 0.5.
Strings
Strings are used frequently in scripting. Strings can be defined using double quotes or single quotes. Learning how to use String Formatting is a very useful technique. You can user the built-in function str() to coerce values into strings.
# print the time
hour
=
5
minute
=
30
print
"Hello, it is "
+
str
(hour)
+
":"
+
str
(minute)
Colors
Working with colors in Python is remarkably easy. You can simply use any tuple of 3 or 4 integers to represent a color in RGB or RGBA. For example, to set a label's text color to red, you can simple do something like this:
label
=
event.source
label.foreground
=
(
255
,
0
,
0
)
Dates
Dates are one of the trickier datatypes to deal with in scripting. It turns out that it is easier to deal with dates using the Java classes java.util.Date and java.util.Calendar than it is to use Python's time module.
Creating Dates
To create an arbitrary date, you can use the java.util.Calendar class. It has various functions to alter the calendar fields, like Calendar.HOUR, Calendar.MONTH, and so on. After you're done manipulating the Calendar, you can use its getTime() function to retrieve the Date represented by the calendar. It also has a handy set() function that takes the common parameters of a Date. The one major "gotcha" here is that January is month zero, not month one. For example:
from
java.util
import
Calendar
cal
=
Calendar.getInstance()
# set year, month, day, hour, minute, second in one call
# This sets it to Feb 25th, 1:05:00 PM, 2010
cal.
set
(
2010
,
1
,
25
,
13
,
5
,
0
)
myDate
=
cal.getTime()
Date Arithmetic
Often you'll have a Date object from a component like the Popup Calendar and want to alter it programmatically. Say, subtracting 8 hours from it, or something like that. The java.util.Calendar class is used for this as well. Following the example above, this code would subtract 8 hours from the variable myDate.
from
java.util
import
Calendar
cal
=
Calendar.getInstance()
cal.setTime(myDate)
cal.add(Calendar.HOUR,
-
8
)
myNewDate
=
cal.getTime()
Date Formatting
To format a date as a String, you can use the system function system.db.dateFormat. This function uses a format string to give it a hint as to how you want your date formatted. The format string is full of various placeholders that will display different parts of the date. These are case-sensitive! The most common placeholders are:
y |
Year |
M |
Month |
d |
Day |
E |
Day of Week |
a |
am/pm marker |
H |
Hour of day (0-23) |
h |
Hour in am/pm (1-12) |
m |
Minute |
s |
Second |
S |
Millisecond |
z |
Time zone |
These placeholders can be repeated for a different effect. For example, M will give you 1-12, MM will give you 01-12, MMM will give you Jan-Dec, MMMM will give you January-December. Here are some examples:
from
java.util
import
Date
now
=
Date()
# creates a new date, for right now
# Common format for databases
print
system.db.dateFormat(now,
"yyyy-MM-dd H:mm:ss"
)
# Nice human-readable format for just the date
print
system.db.dateFormat(now,
"MMM d, yyyy"
)
# Formating just the time in am/pm style
print
system.db.dateFormat(
"h:mm a"
)