2013 tips and tricks mashup, from modelbuilder to formal python code, step-by-step by zeke houk

58
ModelBuilder to Python: Step-by-Step Zeke Houk GIS Developer

Upload: gis-in-the-rockies

Post on 06-May-2015

367 views

Category:

Technology


1 download

DESCRIPTION

ArcGIS ModelBuilder provides the user with a simple way to capture geoprocessing instructions as a Python script. This presentation explores the path to promote that script into formal production level code. Beginning with the geoprocessing foundation exported from ModelBuilder, we augment the code to: • clarify the command line parameters • organize the directories • set the SDE workspace • insert print commands • check if feature classes exist • use try / catch blocks to trap errors • execute operating system commands • add logging throughout the code The end result of these additions is production level code that is easy to maintain and deploy. It is particularly well-suited to run automatically as a Windows Scheduled Task. This session is intended for users who are getting started with Python, or even just thinking about it.

TRANSCRIPT

Page 1: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

ModelBuilder to Python:

Step-by-Step

Zeke Houk

GIS Developer

Page 2: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

ModelBuilder Model:

Page 3: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Delete Features from

simple_conduits:

Page 4: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Pull snapshot from SDE:

Page 5: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Get Rid of Conduit 0:

Page 6: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Dissolve on Conduit Number:

Page 7: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Guido Van Rossum

BDFL

Page 8: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Where did the name come from?

Page 9: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Python script:

Page 10: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Clean up names:

• >>> # Local variables:

• >>> simple_conduits__3_ = R:\\ARG_Projects\\ConduitMapBook\\DissolvedConduits.gdb\\simple_conduits"

• >>> dissolved_conduits__3_ = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\dissolved_conduits"

• >>> ARG_CONDUIT__3_ = "Database Connections\\Connection to DWGIS3.sde\\ARG.DistributionSystem\\ARG.CONDUIT"

• >>> simple_conduits__4_ = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\simple_conduits"

• >>> N0_Zero_Conduits = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\N0_Zero_Conduits"

• >>> dissolved_conduits = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\dissolved_conduits"

• >>> simple_conduits__6_ = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\simple_conduits"

>>> simple_conduits_3_,

>>> simple_conduits_4_,

>>> simple_Conduits_6_

>>> redundant…

Page 11: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Replace with simple_conduits:

• >>> # Local variables:

• >>> simple_conduits = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\simple_conduits"

• >>> dissolved_conduits__3_ = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\dissolved_conduits"

• >>> ARG_CONDUIT__3_ = "Database Connections\\Connection to DWGIS3.sde\\ARG.DistributionSystem\\ARG.CONDUIT"

• >>> simple_conduits = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\simple_conduits"

• >>> N0_Zero_Conduits = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\N0_Zero_Conduits"

• >>> dissolved_conduits = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\dissolved_conduits"

• >>> simple_conduits = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\simple_conduits"

Global replace simple_conduits

For simple_conduits_3_,

simple_conduits_4_, and

simple_conduits_6_.

Page 12: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Duplicates dropped:

>>> # Local variables:

>>> simple_conduits = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\simple_conduits"

>>> dissolved_conduits__3_ = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\dissolved_conduits"

>>> ARG_CONDUIT__3_ = "Database Connections\\Connection to DWGIS3.sde\\ARG.DistributionSystem\\ARG.CONDUIT"

>>> N0_Zero_Conduits = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\N0_Zero_Conduits"

>>> dissolved_conduits = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\dissolved_conduits"

Global replace simple_conduits

For simple_conduits_3_,

simple_conduits_4_, and

simple_Conduits_6_.

Page 13: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Now fix dissolved_conduits:

# Local variables:

simple_conduits = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\simple_conduits"

dissolved_conduits = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\dissolved_conduits"

ARG_CONDUIT__3_ = "Database Connections\\Connection to DWGIS3.sde\\ARG.DistributionSystem\\ARG.CONDUIT"

N0_Zero_Conduits = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\N0_Zero_Conduits"

Becomes…

# Local variables:

simple_conduits = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\simple_conduits"

dissolved_conduits__3_ = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\dissolved_conduits"

ARG_CONDUIT__3_ = "Database Connections\\Connection to DWGIS3.sde\\ARG.DistributionSystem\\ARG.CONDUIT"

N0_Zero_Conduits = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\N0_Zero_Conduits"

dissolved_conduits = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\dissolved_conduits"

Page 14: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Use File Geodatabase Name:

• # Local variables:

• file_gdb = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\"

• simple_conduits = file_gdb + "simple_conduits"

• dissolved_conduits = file_gdb + "dissolved_conduits"

• N0_Zero_Conduits = file_gdb + "N0_Zero_Conduits"

• ARG_CONDUIT = "Database Connections\\Connection to DWGIS3.sde\\ARG.DistributionSystem\\ARG.CONDUIT"

Becomes…

• # Local variables:

• simple_conduits = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\simple_conduits"

• dissolved_conduits = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\dissolved_conduits"

• ARG_CONDUIT = "Database Connections\\Connection to DWGIS3.sde\\ARG.DistributionSystem\\ARG.CONDUIT"

• N0_Zero_Conduits = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\N0_Zero_Conduits"

Page 15: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Use File Geodatabase Name:

file_gdb = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\"

simple_conduits = file_gdb + "simple_conduits"

dissolved_conduits = file_gdb + "dissolved_conduits"

N0_Zero_Conduits = file_gdb + "N0_Zero_Conduits"

Page 16: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Use SDE Connection Name:

# Local variables:

file_gdb = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\"

simple_conduits = file_gdb + "simple_conduits"

dissolved_conduits = file_gdb + "dissolved_conduits"

N0_Zero_Conduits = file_gdb + "N0_Zero_Conduits"

sde_connection = "Database Connections\\Connection to DWGIS3.sde\\"

ARG_CONDUIT = sde_connection + "ARG.CONDUIT"

Becomes…

# Local variables:

file_gdb = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\"

simple_conduits = file_gdb + "simple_conduits"

dissolved_conduits = file_gdb + "dissolved_conduits"

N0_Zero_Conduits = file_gdb + "N0_Zero_Conduits"

ARG_CONDUIT = "Database Connections\\Connection to DWGIS3.sde\\ARG.DistributionSystem\\ARG.CONDUIT"

Page 17: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Use SDE Connection Name:

sde_connection = "Database Connections\\Connection to DWGIS3.sde\\"

ARG_CONDUIT = sde_connection + "ARG.CONDUIT"

Page 18: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Cleaned up names:

# Local variables:

file_gdb = "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\"

simple_conduits = file_gdb + "simple_conduits"

dissolved_conduits = file_gdb + "dissolved_conduits"

N0_Zero_Conduits = file_gdb + "N0_Zero_Conduits"

sde_connection = "Database Connections\\Connection to DWGIS3.sde\\"

ARG_CONDUIT = sde_connection + "ARG.CONDUIT"

Page 19: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Congratulations!

Page 20: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk
Page 21: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

PyWin:

PyWin

Python for Windows extensions

by

Mark Hammond

http://sourceforge.net/projects/pywin32/

pywin32-218.win32-py2.7.exe

Page 22: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Process Steps:

Delete Features from simple_conduits

Append from SDE into simple_conduits,

just COND_NUM and WATER_TYPE

Delete NO_Zero_Conduits

Select from simple_conduits into N0_Zero_Conduits

Delete dissolved_conduits

Dissolve N0_Zero_Conduits into dissolved_conduits

Page 23: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Fix up Process Steps:

• # Process: Append

• arcpy.Append_management(ARG_CONDUIT, simple_conduits, "NO_TEST", "COND_NUM \"COND_NUM\" true true false 2 Short 0 0 ,First,#,Database Connections\\Connection to DWGIS3.sde\\ARG.DistributionSystem\\ARG.CONDUIT,COND_NUM,-1,-1;WATER_TYPE \"WATER_TYPE\" true true false 5 Text 0 0 ,First,#,Database Connections\\Connection to DWGIS3.sde\\ARG.DistributionSystem\\ARG.CONDUIT,WATER_TYPE,-1,-1;Shape_Length \"Shape_Length\" false true true 8 Double 0 0 ,First,#", "")

Becomes…

• # Process: Append

• arcpy.Append_management(

• "'Database Connections\\Connection to DWGIS3.sde\\

• ARG.DistributionSystem\\ARG.CONDUIT'", simple_conduits,

• "NO_TEST", "COND_NUM \"COND_NUM\" true true false 2 Short 0 0 ,First,#,Database Connections\\Connection to DWGIS3.sde\\ARG.DistributionSystem\\ARG.CONDUIT,COND_NUM,-1,-1;WATER_TYPE \"WATER_TYPE\" true true false 5 Text 0 0 ,First,#,Database Connections\\Connection to DWGIS3.sde\\ARG.DistributionSystem\\ARG.CONDUIT,WATER_TYPE,-1,-1;Shape_Length \"Shape_Length\" false true true 8 Double 0 0 ,First,#", "")

Page 24: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Fix up Process Steps:

• # Process: Delete

• arcpy.Delete_management(N0_Zero_Conduits, "FeatureClass")

Becomes…

• # Process: Delete

• arcpy.Delete_management(

• "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\

• NO_Zero_Conduits",

• "FeatureClass")

Page 25: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Fix up Process Steps:

• # Process: Delete

• arcpy.Delete_management(dissolved_conduits, "FeatureClass")

Becomes…

• # Process: Delete

• arcpy.Delete_management(

• "R:\\ARG_Projects\\Conduit MapBook\\DissolvedConduits.gdb\\

• dissolved_conduits",

• "FeatureClass")

Page 26: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

All Scrubbed Up: # Process:

arcpy.DeleteFeatures_management(simple_conduits)

arcpy.Delete_management(N0_Zero_Conduits, "FeatureClass")

arcpy.Delete_management(dissolved_conduits, "FeatureClass")

arcpy.Append_management(ARG_CONDUIT, simple_conduits, "NO_TEST", "COND_NUM \"COND_NUM\" true true false 2 Short 0 0 ,First,#,Database Connections\\Connection to DWGIS3.sde\\ARG.DistributionSystem\\ARG.CONDUIT,COND_NUM,-1,-1;WATER_TYPE \"WATER_TYPE\" true true false 5 Text 0 0 ,First,#,Database Connections\\Connection to DWGIS3.sde\\ARG.DistributionSystem\\ARG.CONDUIT,WATER_TYPE,-1,-1;Shape_Length \"Shape_Length\" false true true 8 Double 0 0 ,First,#", "")

arcpy.Select_analysis(simple_conduits, N0_Zero_Conduits, "\"COND_NUM\" <> 0")

arcpy.Dissolve_management(N0_Zero_Conduits, dissolved_conduits, "COND_NUM;WATER_TYPE", "", "SINGLE_PART", "DISSOLVE_LINES")

Page 27: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Geek Speak - If:

if arcpy.Exists(something):

# do something with it

if not arcpy.Exists(something):

# it does not exist

Page 28: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Avoid Errors:

if arcpy.Exists(dissolved_conduits):

arcpy.Delete_management(dissolved_conduits, "FeatureClass")

Becomes…

arcpy.Delete_management(dissolved_conduits, "FeatureClass")

Page 29: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Delete Processes: if arcpy.Exists(N0_Zero_Conduits):

arcpy.Delete_management(N0_Zero_Conduits, "FeatureClass")

if arcpy.Exists(dissolved_conduits):

arcpy.Delete_management(dissolved_conduits, "FeatureClass")

if arcpy.Exists(simple_conduits):

arcpy.DeleteFeatures_management(simple_conduits)

else:

print "Missing feature class -> " + simple_conduits

Page 30: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Alternative:

>>> from arcpy import env

>>> env.overwriteOutput = True

>>> env.workspace = “C:/EsriPress/Python/Data/”

Page 31: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Either way, explain:

>>> # There feature classes probably already exist.

>>> # They are left over from the last time we ran this script.

>>> # Deleting the old versions makes room for the new ones.

Page 32: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Geek Speak – Try / Catch:

>>> try:

>>> # .. To do something that might blow up

>>> # and it “throws” an Exception

>>> except Exception ex:

>>> print ex

>>>

Page 33: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Catch Exception:

>>> try:

>>> arcpy.Append_management(CONDUIT

>> except Exception as ex:

>>> print ex

Page 34: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Test Exception:

>>> try:

>>> bogus = 1.0 / 0.0 # forces exception

>>> arcpy.Append_management(ARG_CONDUIT,

>>> except Exception as ex:

>>> print ex

Page 35: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Logging: import logging

logfile = "C:/temp/demo.log"

logging.basicConfig(

level=logging.INFO,

filename=logfile)

logger = logging.getLogger()

logger.info("A swallow beats its wings 43 times every second.")

logger.warning("Go away or I shall taunt you a second time.")

logger.error("Run away! Run away!")

print "Done."

Page 36: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

C:\temp\demo.log: INFO:A swallow beats its wings 43 times every second.

WARNING:Go away or I shall taunt you a second time.

ERROR:Run away! Run away!

Page 37: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Logging: logging.info(" checking for " + HYDRANT_BRANCH )

if arcpy.Exists(HYDRANT_BRANCH ):

try:

arcpy.Append_management(APPEND_HYD_BR, hyd_branch, ...)

logging.info(" HYDRANT_BRANCH_shp appended to SDE")

except Exception as ex:

logging.error(" HYDRANT_BRANCH_shp append FAILED")

logging.Exeception(ex)

Page 38: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Recap – Do These 3 Things…

1. Organize variable names

2. Trap Errors

3. Write a log file

"First shalt thou take out the Holy Pin.

Then shalt thou count to three, no more, no less.

Three shall be the number thou shalt count, and the number of the counting shall be three.”

Page 39: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Pythonic:

• Explicit is better than implicit.

• Simple is better than complex.

• Readability counts.

The Zen of Python, by Tim Peters, includes:

Page 40: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Geek Speak - Blocks:

>>> if arcpy.Exists(simple_conduits):

>>> arcpy.DeleteFeatures_management(simple_conduits)

>>> # maybe do something else

>>> # some additional processing

>>> else:

>>> print "Missing feature class -> " + simple_conduits

Page 41: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Duck Typing:

If it looks like a duck, quacks like a duck, etc.

Works with strings,

integers,

floating point,

lists,

etc.

Page 42: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Strings:

Enclosed in either ‘single’ or “double” quotes.

Concatenate using + sign.

>>> topic = "GIS"

>>> location = "Rockies“

>>> event = topic + " in the " + location

>>> print event

GIS in the Rockies

Page 43: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Strings: More built-in functions:

>>> event = "GIS in the Rockies"

>>> print event.lower()

>>> print event.upper()

>>> print event.title()

>>> string_length = len(event)

>>> print string_length

>>> print event.find("Rockies",0,string_length)

gis in the rockies

GIS IN THE ROCKIES

Gis In The Rockies

18

11

Page 44: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Triple quotes can span multiple

lines: “””The Dead Collector: Bring out yer dead.

Large Man: Here's one.

The Dead Collector: That'll be ninepence.

The Dead Body: I'm not dead.

The Dead Collector: 'Ere, he says he's not dead.

Large Man: Yes he is.

The Dead Body: I'm not.

The Dead Collector: He isn't.

Large Man: Well, he will be soon, he's very ill.

The Dead Body: I'm getting better. “””

Page 45: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Lists: >>> python_sketch = ['Dead Parrot', 'Confuse-a-Cat', 'Silly Walks', 'Four Yorkshiremen','Fish Slap'];

>>> # notice duck typing

>>> print python_sketch [0]

>>> print python_sketch [1]

>>> python_sketch.append('Limberjack Song')

>>> print len(python_sketch)

Dead Parrot

Confuse-a-Cat

6

Page 46: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

For loop:

>>> for funny_bit sketch in python_sketch:

>>> print funny_bit + ' hahaha‘

Dead Parrot hahaha

Confuse-a-Cat hahaha

Silly Walks hahaha

Four Yorkshiremen hahaha

Fish Slap hahaha

Limberjack Song hahaha

Page 47: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Command Line Args:

>>> userid = arcpy.GetParameterAsText(0)

>>> if not userid:

>>> print ‘Usage: python.exe my_script.py userid password ‘

>>> pword = arcpy.GetParameterAsText(1)

>>> if not pword:

>>> print ‘Usage: python.exe my_script.py userid password ‘

Page 48: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Command Line Args:

C:\> D:\python27\python.exe my_script.py zeke password

Python.exe is located on D:\python27

my_script.py is some python code

Userid is zeke

My Oracle password is at the end

Page 49: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Command Line Args:

>>> userid = arcpy.GetParameterAsText(0)

>>> pword = arcpy.GetParameterAsText(1)

Page 50: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Oracle connection:

connection_string = userid + '/' + pword + '@dwsde'

# looks like this -> zeke/password@dwsde

import cx_Oracle

connection = cx_Oracle.connect(connection_string)

cursor = connection.cursor()

cursor.execute('select distinct water_type from conduit')

for result in cursor:

print result

Page 51: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Oracle connection:

cursor.execute('select distinct diameter from conduit')

allresults = cursor.fetchall()

for result in allresults:

print result

cursor.close()

connection.close()

print "Done."

Page 52: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Nee

Page 53: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Here is a shrubbery.

Page 54: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Not Habit Forming:

But is considered a “Gateway” language,

which frequently leads to more nerdy behavior.

Page 55: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Good Book:

Page 56: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Good Book:

Page 57: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

GIS Day November 22:

See denverwater.org for directions...

Page 58: 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk

Becomes…