PSP Scripting 201: Basic Python: Functions and Libraries

I’ve shown you next to no functions available to you in Python.  We’ve covered print, round(), list(), dict(), tuple(), and I’ve shown input().  There are more commands/functions available for you to use.  Python not only has a large number of default commands, but it also comes with a series of standard libraries that include even more functions available for you to use.

I won’t be getting into them, but it’s important to know how libraries are imported into your script.  This is because the first line of any PSP script imports a library known as PSPApp.  In PSP8 and 9 it’s JascApp.  PSP is backward compatible with older versions of PSP so a script that imports JascApp will also run in newer versions of PSP.

Let’s look at the two main ways you can use to import a library.  We’ll use the math library to keep things simple.

The most common and preferred way to import most libraries is done with a very simple

import LibraryName

That’s it.  I now have access to all of the commands stored in the math library.  Commands such as sin(), cos(), radians(), etc.  If you import libraries this way then you end up calling a command you must prefix the command with the name of the library followed by a period and then the command.

import math
radians_of_180Degrees = math.radians(180)

The less preferred method for importing is to import every function, class, and variable.  This is less preferred because it has the potential to cause naming conflicts if you bring in too many libraries and it uses up a lot more memory.  This method removes the need to prefix the command name with the library name.  The syntax is equally simple.

from LibraryName import *

The asterisk at the end is a wildcard that means “everything”.  You can also limit your import to only a single set of functions by replacing the asterisk with the name of the function you wish to import.

This is the method that we use to import JascApp and PSPApp.

from JascApp import *

This must always be the first line of any script.  If we want to import other libraries after it that’s perfectly fine.  But this should always be the first.

Functions

Alright, now let’s look at how to create our own functions.  Or, more specifically, how a function definition looks.

Python, like any programming language, would be of very little use if we couldn’t make our own commands that do those special things we want it to do.  Functions can be thought of as miniature programs inside our main program.  This is overtly simplistic but it’ll do.

When you call a function it performs an action, and can even return a result.  Take for example, let’s say we wanted a function that took two lists and returned the difference between them.  Well we could go looking for a library that contained such a function, or we could make it ourselves.

In order to use a function you must first define it.  Defining a function gives it a name, and tells Python what parameters it uses.  Parameters are just variable that will hold data.  These variables only exist inside the function and are only set when the function is called.

Here is how a standard Python function definition looks.

def FunctionName(param1, param2):
    #some code here for the function to perform

As you can see you start creating a function by first using the keyword “def” in all lowercase.  This tells Python we are “defining” a new function.  Then we provide it with a name.  This name must follow the same restrictions as a regular variable name.  Next, inside a pair of parentheses you enter in the names of the parameter variables you’ll be using inside the function.  Name these whatever you want, just be sure they are meaningful to you.  Finally we end with a “:” to begin a new code block.

Let’s look at a simple standard example.   This is not a useful function but it gets the idea across.

def printAdd(num1, num2):
    print num1 + num2

printAdd(3,4)

The result of this program should be Python printing out the number 7 to the screen.

I would like to point out that I stopped using an indent when I was done with the function.  This ends the code block and lets me get back to the rest of the program.  I will also not be able to cal num1 or num2 outside of this function as they only exist within that function.  I think I’ll quickly address variable “scope” next tutorial.

return

So let’s say we wanted to return some information from the function.  We do this using the “return” keyword.  This keyword is very simple, it returns whatever follows the keyword be it a number, string, list, dict, or tuple.  You can return multiple values by separating them with commas.  This will create a tuple.

return can also be used by itself to simply end the execution of the command without returning anything.  An exit early option.  Something you may find yourself doing in your scripts.

Here’s an example of returning a value

def add(num1, num2):
    return num1 + num2

print add(num1,num2)

And here is a common example of quiting a PSP script early if there are no open images.  This will include things you’re not familiar with but I think you can handle it.

def Do(Environment):
    if len(App.Documents) == 0:  #No open documents
        return
    #The rest of the script if documents were open

There is a lot more to Python scripting than this.  We never even got into loops, or splices, adding to lists, removing from lists, bitwise operators, opening files, accessing the internet, etc.  Python is so powerful it’s truly amazing that they thought to use it as the language of PSP scripts.

There is only one more topic I want to cover before we get directly into scripting inside of PSP.  Knowing what few things I’ve covered will help you to recognize what it is you’re seeing when you read a PSP Script.

The last topic we’ll cover is scope.  While not usually seen in most scripts, I think it’s an important topic to cover.

Remember, if you want to learn more about Python programming, or to get more thorough descriptions and maybe a bit more accurate information I o recommend you seek out some official python programming help.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: