Working with script templates in PyScripter

In a previous post I went over some PyScripter configuration which the attendees to the “Introduction to Geoprocessing Scripts using Python” course carry out. These things include adding line numbers to a script and making sure full code completion was available for the ArcPy site package.

This article will explain how to create script templates – scripts with ready to go code. This is another nice feature of PyScripter.

Script templates can be used to provide a new script with code already stubbed out such as:

  • Add frequent comments.
  • Import commonly used modules / site packages.
  • Provide a structure for runtime error handling and log file generation.

An example of a script template is shown below:

TemplateCode
Default Script Template

This default code appears when you press the New Python Module button NewButton or if you go to the File menu and then choose New > New Python Module.

So how do you change this code template?

By default you are provided with the following code (plus comments):

MainModule

This can be easily changed by:

1: Select from the Tools menu > Options > File Templates menu item which will display the File Templates dialog box.

This dialog contains a number of different types of templates which you can use in case if you ever need to create an XML or HTML document within PyScripter.

2: Under Name, choose Python Script.

3: In the File Template section locate the Template area.

01ScriptTemplate

This is where you can add your template code which will appear when you create a new script module.

You might want to include the following:

  • Comments
  • import statements
  • Error handling and logging

**NB** Please be aware that when you update this template there is no way of regaining the original code so it might be a good idea to copy the code into a file just in case….

For example:

#------------------------------------------------------
# Name:        $[ActiveDoc-Name]
# Purpose:
# Author:      $[UserName]
# Created:     $[DateTime-'DD/MM/YYYY'-DateFormat]
#------------------------------------------------------
import arcpy, os, sys
try:
    # Add implementation here
    arcpy.env.workspace = ""
except arcpy.ExecuteError():
    # Capture GP error
    print (arcpy.GetMessages(2))
except Exception as e:
    # Capture general Python error
    print (e)

Notice that in the comments part of the template code there are a number of $[variable] entries, for example $[UserName]. You can probably guess that these custom parameters are similar to inline variables – you can find more information about these parameters via Help > PyScripter > Parameters help documentation.

4: Press the Update UpdateButton button to save your updated code back to the template.

5: Press the OK button

NewPythonFileDialog

When you press the New NewButton button your new module will have your updated template code.

Can I create multiple script templates?

Yes! You can choose from a list of code templates which you have created.

So “How do I create these other templates?

Creating a new script template is pretty easy. It is all achieved, once again, via the File Template dialog – the same dialog which you used a moment ago to change the default code template.

So:

1: From the Tools menu select Options > File Templates menu item.

This will display the File Templates dialog box.

2: Click on Python Script in the Name section to display the template code for a new Python module.

FileTemplateDlg

This is the code which is displayed when you create a new module via the New NewButton button or File menu > New > New Python Module menu item.

3: In the Name text box type in a new name for your new template, for example GPScriptTemplate.

Note that your name can not have any spaces within it.

4: Press the Add AddButton button

This will add your new template to the bottom of the template list.

5: Add your template code by amending what is already there or by writing your own code.

6: Once you have added your code press the Update UpdateButton button to save your changes to the template.

7: Press the Up UpButton button to move your template up the template list so all the all the Python scripts are grouped together.

8: Press the OK button to commit your changes and close the dialog.

 

To choose from a list of code templates you have created you should:

1: Go to the File menu choose New > New File. This will display the New File dialog box.

2: Under Categories, choose Python and you will see your named template, for example GPScriptTemplate

NewFileDlg

3: Press the Create button and your code contained in the template will be added to your newly created script module.

 

So, if you use PyScripter, this could be Heaven for Everyone – it’s worth giving them a go. Script templates can make writing your code just that little bit quicker, especially if you tend to write the same code over and over again.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s