Hollinger Week 3

Chapter 6:

Reading Notes:

  • Exists() checks if a dataset exists. I have never used this before, but I can think of a few times it would have been helpful.
  • Catalog paths — paths recognized by Arc Pro.
  • Dynamic properties — “the properties available for use vary with the nature of the object being described”
  • * is a wildcard
  • arcpy.ListWorkspaces(“”, “FILEGDB”) allows you to list all the geodatabases in a workspace.
  • Most list functions in ArcPy return data elements in the current workspace.
  • You can use functions like ListWorkspace(), ListDatasets() and ListFiles() to explore your data sets and workspace.
  • Add brackets around the outside of an expression for list intervention.

Answers to Review Questions:

    • Describe() is a regular function of ArcPy. The properties for use will be determined by the type of object returned (this is true for da.Describe() too). da.Describe() is a function of the ArcPy Data Access module. It will return the same information as Describe(), but as a dictionary. da.Describe() is more transparent, while Describe() is faster.
    • System paths are paths recognized by Windows OS, while catalog paths are the paths recognized by ArcGIS Pro. This means that, for example, if you are exploring data in a folder in ArcGIS Pro it will expect a catalog path not a system path.
    • To list geodatabase feature classes you must specify the geodatabase in the path, without specifying you will just get only shapefile feature classes. Shapefile feature classes will also be listed with .shp while geodatabase feature classes will not.
    • da.Walk() takes the top-level workspace and non-required parameters for optional filtering to return workspace, folder, and filenames at each level.  It can recognize both catalog and system paths. So, for a complex subfolder structure, the function will list the path and then all folders, files, gdb, and workspaces at that path.
    • Some examples given for iterating in a loop are building pyramids for hundreds of rasters, describing all the fields in a dataset, reading several shapefiles, and copying them to another geodatabase. I personally have used them to create a multiring buffer around each of hundreds of polygons so I didn’t have to do it manually.
    • List comprehension is a concise way to create a list by iterating over an object. For example: “[<expression> for <item> in <list>”].  You can also add an if condition to facilitate filtering. It may be used to be more concise when working with lists.

Chapter 7:

Reading Notes:

  • Errors can occur from using a combo of spaces and tabs
  • I would say that I most frequently use print statements and error statements as I am debugging.
  • A single # is used for permanent comments and a double # symbol is used for temporarily commenting out lines of code during the testing process. I didn’t really know this. I usually always use single. I guess that is kind of sloppy though.
  • Built-in Python debugger package is called pdf, but editors also include a debugging environment. I always found debugging packages so much more time-consuming to use than a debugging environment or other methods.
  • Close ArcGIS Pro to release locks while running a script.
  • To check for errors and report meaningfully: try-except to ArcPy messaging function
  • You can specify multiple except statements within a try-except statement. You can also include a finally statement that will always run.
  • I have never used a custom exception before. This is a neat and really useful feature.
  • ArcPy will generate a system error when a geoprocessing error occurs.

Answers to Review Questions:

    • Some of the most common syntax errors have to do with misspelled keywords/variables, missing punctuation, and inconsistent indentation
    • Exceptions are errors that are detected while the script is running. These could be errors or unanticipated events. Handling exceptions allows scripts to be more robust because if the exception is caught then the program can continue running.
    • To debug you might review the content of error messages (this will potentially give you error codes and other useful info to help you figure out what is wrong, but they can also be misleading), print messages to your script (ex: printing out steps as you go so you know what is completed before the error), comment out select code (eliminating the error by commenting out and isolating it, but this does not tell you why an error occurs), or use a python debugger (a tool that allows you to step through your code line by line).
    • First, you should check for syntax issues. Then, set your breakpoints in the script. The debugger will run until a breakpoint is reached, but without it the debugger will go line by line. Run the debugger and at every breakpoint (if you are using IDLE, PyCharm, or a similar program) you will be able to see paths, variable values, etc. You can also step over code or quit the execution of the process while debugging.
    • To use a try-except statement, you would write try: then enter and indent. This is where the code you want to carry out will go. Then, you will use an except statement (aligned with the try statement) followed by a specific exception. On the next line an indented line of code will follow that should be carried out if the specific exception occurs. So, essentially this type of statement says (generalized), try to run this code, unless this exception occurs, then do this instead.
    • Using a custom exception will allow you to make your code and exceptions easier to understand. For example, in the text, a ValueError exception is replaced with the TooShortError. This makes your code more robust and useful because it will be more descriptive in telling what the problem is for you and other users.