LESSON

Basic Python - Dates, Colors, and JSON Strings

Description

Learn how to create and configure Python data types for use in your project.

Video recorded using: Ignition 8.0

Transcript

(open in window)

[00:00] Dates are an important datatype that you can manipulate within scripting. While Python has a built-in way of handling date values, we actually find that it's easier to use Ignition's built-in functions to manage these date values within scripting. Typically, you can retrieve a date value from a component on a window but you can also create a date within scripting using one of those built-in functions. In this case I'm using system.date.now to create the current date. I can then print that current date using the print command and executing that script. The first value here corresponds to the current date and time. Other built-in functions allow me to manipulate that date to become a new value that I want. In this example, I'm calling two different functions that both do similar things. The first is system.date.addMinutes, and the second is system.date.addMonths. Both functions expecting date value that I want to manipulate and then more change it by either a positive or negative amount depending on what type of integer I put into the function. For the case of the add minutes function, I've passed at a 15, meaning I want to add 15 minutes to that date value that I passed in. And we can see that corresponds to the output that we got on the right. The second function is been passed value of negative three meaning I want to subtract three months from that date. Which we can see also corresponds to our output on the right. Other functions allows to compare date values which may be little bit easier to use, rather than these standard operators. In this case, I'm using the function system.date.isAfter which checks to see if the first date that I pass in is after the second date that I pass in. In this example right here, I'm trying to see if my current date that I created earlier is after this second date that I'm passing in right here. As you may have guessed, this first date is after the second date, so when I execute the script I get a value of true. A slight change to my script where I can now add ten minutes instead of subtract them and we get the opposite output. Another datatype that you might find useful is a color. Colors are used to change the color of things that you see on the screen. Again, ignition has some built-in functions that make handling colors little bit easier. In this first three examples on lines 56 through 58, we can see three different examples of the system.gui.color function. Which returns a color value from a string value that we passed in. The string value can take many forms. You'll notice in my first example here, I have the string red list in there which is the name of the color that I want to create. I can also pass it a hex code like in my second example here. Or even a string of RGB values like my third example here. In addition to this string values, I can also pass in three integers like I have done here in this fourth example. Even though each one of these examples is passing in a different value for the color, you'll notice when I execute them all of them return the same red color. In addition creating color values within scripting, I can also have the user generated color value, using the system.gui.chooseColor function. This function simply requires that I pass in a starting color. But when this script executes, you'll notice that it actually brings up a color picker. Here, the user can actually select a color and the script will return that value instead. While not necessarily a data type, JSON strings have become increasingly popular. A JSON string is simply formatted in certain way that makes it readily accessible by other systems, whereas different Python specific datatype might not be. Again, ignition has some built-in functions that we can use to help us with JSON. Here on line 71, I have a dictionary. In line 72, I then use the system.util.jsonEncode function to encode that dictionary into a string with JSON formatting. The five simply indicates the level of spacing that I want in that new JSON formatted string. When I execute this code, you'll notice that it takes the dictionary and simply makes it look little bit different. While all of the information seems to be the same, this function is actually done two things. First, it's taken that dictionary and changed it into a string. Second, it's then taken that string and formatted it to look like what you see in the output. This format can then be recognized by other systems outside of ignition, even if they don't use Python. In the next lesson, we're going to take a look at how we can use flow control to determine how our scripts run.

You are editing this transcript.

Make any corrections to improve this transcript. We'll review any changes before posting them.

close

Share this video