Literal strings can be typed in using either double quotes or single quotes. This can be handy when your string contains one quote or the other. You can also use the backslash character to escape special characters including these quotes. See the Strings page for more information.
Numbers can just be typed in normally, like
42
or 3.14159
. Adding a decimal point differentiates an Integer from a Float. More information on Numeric types can be found on the Numeric Types page.
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) #(red,green,blue)
Additionally, the system.gui.color function allows you to pick a color in a similar fashion:
newColor = system.gui.color(255,0,0)
Python offers a variety of sequence types: most notably Lists and Tuples. These are ordered collections, meaning they are indexed and the sorted order is maintained. More information on these types can be found on the Lists and Tuples page.
newList = [1,2]
newTuple = (2,5,7)
A Dictionary is a very useful type that holds a set of key-value pairs. Unlike sequences, they are not ordered, so there is no need to sort them. Instead you give each Value in the dictionary a Key, which handles as a reference to Value. You may have used these in other languages and know them as hashmaps, maps, associative memories, or associative arrays. More information on Dictionaries can be found on the Dictionaries page.
newDictionary = {"itemName":5}
JSON
JSON stands for JavaScript Object Notation. While it is not a datatype, it is a way of defining data in a human readable format, and is commonly used in many applications. It comes from the Javascript programming language, but it is language independent. Each JSON object contains lists and objects. A list is a series of ordered values separated by commas that is commonly used within Python. The object works like a dictionary, using any number of name/value pairs, where each value could be any basic datatype, a list, or even another object with its own name/value pairs. Because JSON is just a way of defining data, it can be used in many different programming languages, including Python. This makes it a useful tool for defining data in a way that humans can easily read.
Ignition also has two scripting functions that allow you to convert between a JSON string and a native Python object: system.util.jsonEncode and system.util.jsonDecode.
Official Documentation
For more information on JSON, see http://www.json.org.
A Dataset is a multidimensional collection of values, stored in a manner similar to how values on a spreadsheet appear. Python does not natively have a Dataset type. Instead these datasets were created for use inside of Ignition. There are two types of datasets:
- Dataset: Sometimes called a "Standard Dataset", this type is commonly used on many Vision Components, such as the Power Table or Chart, to display multiple values simultaneously.
- PyDataset: Short for "Python Dataset", these datasets act in a manner very similar to a Python Sequence when it comes to accessing specific values, or iteration (see Lists and Tuples). Ignition's built-in system functions that interact with the database typically return a PyDataset.
While there are two types of datasets, you can easily convert one type of dataset to the other. Additionally, you can easily create a dataset from a script.
header = ["The Only Column"]
rows = [[1],[2],[3]]
myDataset = system.dataset.toDataSet(header, rows)
Dates and times can be created in Python with the datetime
and time
libraries. However, the Ignition's built-in system.date functions can also be used instead without having to import either library.
currentTime = system.date.now()
print "The current time is: %s" % currentTime