Title: Geography 465 Managing Custom Python Script Tools
1Geography 465 Managing Custom Python Script
Tools
2Considerations for Creating Script Tools
- End User
- Purpose of the script tools
- Toolbox organization
- Documentation
- Code structure
- Reusable components, error handling, more
- How to implement
- Where to store, how to distribute, future tools,
etc. - Mainteance issues
3Organizing your script tools
- Create the script tools (associate each with a
.py script) - Group common tools into toolsets
- Group toolsets into toolbox
4Understanding program flow
- Top-level file (.py file)
- Module that you can run
- Controls the flow
- Can import other modules (.py files)
Top-level file
Imported modules
5Example Spatial Statistics Toolbox
DirectionalDistribution.py
Top-level file
CentralFeature.py
os
locale
sys
HelperFunctions
sys
os
locale
HelperFunctions
random
sys
math
locale
os
Other spatial statistics tools import
HelperFunctions
HelperFunctions.py import sys, math, os, locale,
random, tempfile, arcgisscripting
6Ways of importing
- More than one way to import
- import entire module requires module prefix
- import math
- math.sin (x)
- fromimport specific names from module
- from math import sin, cos,
- sin(x)
- fromimport
- from math import
- tan (x)
- Affects readability, name collision, ability to
reload
7Code order
- Writing custom functions
- Put functions at top
- Put top-level code at bottom so functions are
recognized - Functions
- def
- def
- top-level code
8Controlling access to functions and variables
- Each module has a built in _ _name_ _atttribute
- Module can test its own _ _name_ _
- Top-level file module
- _ _name_ _ is equal to _ _main_ _
- Imported module
- _ _name_ _ is equal to ltmodulenamegt
- _ _main_ _ prevents access when module is
imported for - def statements
- Variables
9Code example Control access
- MyTool.py
- import Example
- fmc Example.find_most_central()
-
- ac Example.areaCalc()
- Example.py
- import arcgisscripting
- gp arcgisscripting.create()
- def get_imputs (arguments) access
unrestricted - df find_most_central() in calling module
- if _ _name_ _ _ _main_ _ access
restricted - def areaCalc() in calling module