SlideShare a Scribd company logo
1 of 58
Download to read offline
ModelBuilder to Python:
Step-by-Step
Zeke Houk
GIS Developer
ModelBuilder Model:
Delete Features from
simple_conduits:
Pull snapshot from SDE:
Get Rid of Conduit 0:
Dissolve on Conduit Number:
Guido Van Rossum
BDFL
Where did the name come from?
Python script:
Clean up names:
• >>> # Local variables:
• >>> simple_conduits__3_ =
R:ARG_ProjectsConduitMapBookDissolvedConduits.gdbsimple_conduits"
• >>> dissolved_conduits__3_ = "R:ARG_ProjectsConduit
MapBookDissolvedConduits.gdbdissolved_conduits"
• >>> ARG_CONDUIT__3_ = "Database ConnectionsConnection to
DWGIS3.sdeARG.DistributionSystemARG.CONDUIT"
• >>> simple_conduits__4_ = "R:ARG_ProjectsConduit
MapBookDissolvedConduits.gdbsimple_conduits"
• >>> N0_Zero_Conduits = "R:ARG_ProjectsConduit
MapBookDissolvedConduits.gdbN0_Zero_Conduits"
• >>> dissolved_conduits = "R:ARG_ProjectsConduit
MapBookDissolvedConduits.gdbdissolved_conduits"
• >>> simple_conduits__6_ = "R:ARG_ProjectsConduit
MapBookDissolvedConduits.gdbsimple_conduits"
>>> simple_conduits_3_,
>>> simple_conduits_4_,
>>> simple_Conduits_6_
>>> redundant…
Replace with simple_conduits:
• >>> # Local variables:
• >>> simple_conduits = "R:ARG_ProjectsConduit
MapBookDissolvedConduits.gdbsimple_conduits"
• >>> dissolved_conduits__3_ = "R:ARG_ProjectsConduit
MapBookDissolvedConduits.gdbdissolved_conduits"
• >>> ARG_CONDUIT__3_ = "Database ConnectionsConnection to
DWGIS3.sdeARG.DistributionSystemARG.CONDUIT"
• >>> simple_conduits = "R:ARG_ProjectsConduit
MapBookDissolvedConduits.gdbsimple_conduits"
• >>> N0_Zero_Conduits = "R:ARG_ProjectsConduit
MapBookDissolvedConduits.gdbN0_Zero_Conduits"
• >>> dissolved_conduits = "R:ARG_ProjectsConduit
MapBookDissolvedConduits.gdbdissolved_conduits"
• >>> simple_conduits = "R:ARG_ProjectsConduit
MapBookDissolvedConduits.gdbsimple_conduits"
Global replace simple_conduits
For simple_conduits_3_,
simple_conduits_4_, and
simple_conduits_6_.
Duplicates dropped:
>>> # Local variables:
>>> simple_conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbsimple_conduits"
>>> dissolved_conduits__3_ = "R:ARG_ProjectsConduit
MapBookDissolvedConduits.gdbdissolved_conduits"
>>> ARG_CONDUIT__3_ = "Database ConnectionsConnection to
DWGIS3.sdeARG.DistributionSystemARG.CONDUIT"
>>> N0_Zero_Conduits = "R:ARG_ProjectsConduit
MapBookDissolvedConduits.gdbN0_Zero_Conduits"
>>> dissolved_conduits = "R:ARG_ProjectsConduit
MapBookDissolvedConduits.gdbdissolved_conduits"
Global replace simple_conduits
For simple_conduits_3_,
simple_conduits_4_, and
simple_Conduits_6_.
Now fix dissolved_conduits:
# Local variables:
simple_conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbsimple_conduits"
dissolved_conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbdissolved_conduits"
ARG_CONDUIT__3_ = "Database ConnectionsConnection to
DWGIS3.sdeARG.DistributionSystemARG.CONDUIT"
N0_Zero_Conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbN0_Zero_Conduits"
Becomes…
# Local variables:
simple_conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbsimple_conduits"
dissolved_conduits__3_ = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbdissolved_conduits"
ARG_CONDUIT__3_ = "Database ConnectionsConnection to DWGIS3.sdeARG.DistributionSystemARG.CONDUI
N0_Zero_Conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbN0_Zero_Conduits"
dissolved_conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbdissolved_conduits"
Use File Geodatabase Name:
• # Local variables:
• file_gdb = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdb"
• simple_conduits = file_gdb + "simple_conduits"
• dissolved_conduits = file_gdb + "dissolved_conduits"
• N0_Zero_Conduits = file_gdb + "N0_Zero_Conduits"
• ARG_CONDUIT = "Database ConnectionsConnection to
DWGIS3.sdeARG.DistributionSystemARG.CONDUIT"
Becomes…
• # Local variables:
• simple_conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbsimple_conduits"
• dissolved_conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbdissolved_conduits"
• ARG_CONDUIT = "Database ConnectionsConnection to DWGIS3.sdeARG.DistributionSystemARG.CONDUI
• N0_Zero_Conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbN0_Zero_Conduits"
Use File Geodatabase Name:
file_gdb = "R:ARG_ProjectsConduit
MapBookDissolvedConduits.gdb"
simple_conduits = file_gdb + "simple_conduits"
dissolved_conduits = file_gdb + "dissolved_conduits"
N0_Zero_Conduits = file_gdb + "N0_Zero_Conduits"
Use SDE Connection Name:
# Local variables:
file_gdb = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdb"
simple_conduits = file_gdb + "simple_conduits"
dissolved_conduits = file_gdb + "dissolved_conduits"
N0_Zero_Conduits = file_gdb + "N0_Zero_Conduits"
sde_connection = "Database ConnectionsConnection to DWGIS3.sde"
ARG_CONDUIT = sde_connection + "ARG.CONDUIT"
Becomes…
# Local variables:
file_gdb = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdb"
simple_conduits = file_gdb + "simple_conduits"
dissolved_conduits = file_gdb + "dissolved_conduits"
N0_Zero_Conduits = file_gdb + "N0_Zero_Conduits"
ARG_CONDUIT = "Database ConnectionsConnection to DWGIS3.sdeARG.DistributionSystemARG.CONDUIT"
Use SDE Connection Name:
sde_connection = "Database ConnectionsConnection to
DWGIS3.sde"
ARG_CONDUIT = sde_connection + "ARG.CONDUIT"
Cleaned up names:
# Local variables:
file_gdb = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdb"
simple_conduits = file_gdb + "simple_conduits"
dissolved_conduits = file_gdb + "dissolved_conduits"
N0_Zero_Conduits = file_gdb + "N0_Zero_Conduits"
sde_connection = "Database ConnectionsConnection to DWGIS3.sde"
ARG_CONDUIT = sde_connection + "ARG.CONDUIT"
Congratulations!
PyWin:
PyWin
Python for Windows extensions
by
Mark Hammond
http://sourceforge.net/projects/pywin32/
pywin32-218.win32-py2.7.exe
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
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
ConnectionsConnection to
DWGIS3.sdeARG.DistributionSystemARG.CONDUIT,COND_NUM,-1,-
1;WATER_TYPE "WATER_TYPE" true true false 5 Text 0 0 ,First,#,Database
ConnectionsConnection to
DWGIS3.sdeARG.DistributionSystemARG.CONDUIT,WATER_TYPE,-1,-
1;Shape_Length "Shape_Length" false true true 8 Double 0 0 ,First,#", "")
Becomes…
• # Process: Append
• arcpy.Append_management(
• "'Database ConnectionsConnection to DWGIS3.sde
• ARG.DistributionSystemARG.CONDUIT'", simple_conduits,
• "NO_TEST", "COND_NUM "COND_NUM" true true false 2 Short 0 0 ,First,#,Database C
Fix up Process Steps:
• # Process: Delete
• arcpy.Delete_management(N0_Zero_Conduits, "FeatureClass")
Becomes…
• # Process: Delete
• arcpy.Delete_management(
• "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdb
• NO_Zero_Conduits",
• "FeatureClass")
Fix up Process Steps:
• # Process: Delete
• arcpy.Delete_management(dissolved_conduits, "FeatureClass")
Becomes…
• # Process: Delete
• arcpy.Delete_management(
• "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdb
• dissolved_conduits",
• "FeatureClass")
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 ConnectionsConnection to
DWGIS3.sdeARG.DistributionSystemARG.CONDUIT,COND_NUM,-1,-1;WATER_TYPE
"WATER_TYPE" true true false 5 Text 0 0 ,First,#,Database ConnectionsConnection to
DWGIS3.sdeARG.DistributionSystemARG.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")
Geek Speak - If:
if arcpy.Exists(something):
# do something with it
if not arcpy.Exists(something):
# it does not exist
Avoid Errors:
if arcpy.Exists(dissolved_conduits):
arcpy.Delete_management(dissolved_conduits, "FeatureClass")
Becomes…
arcpy.Delete_management(dissolved_conduits, "FeatureClass")
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
Alternative:
>>> from arcpy import env
>>> env.overwriteOutput = True
>>> env.workspace = “C:/EsriPress/Python/Data/”
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.
Geek Speak – Try / Catch:
>>> try:
>>> # .. To do something that might blow up
>>> # and it “throws” an Exception
>>> except Exception ex:
>>> print ex
>>>
Catch Exception:
>>> try:
>>> arcpy.Append_management(CONDUIT
>> except Exception as ex:
>>> print ex
Test Exception:
>>> try:
>>> bogus = 1.0 / 0.0 # forces exception
>>> arcpy.Append_management(ARG_CONDUIT,
>>> except Exception as ex:
>>> print ex
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."
C:tempdemo.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!
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)
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.”
Pythonic:
• Explicit is better than implicit.
• Simple is better than complex.
• Readability counts.
The Zen of Python, by Tim Peters,
includes:
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
Duck Typing:
If it looks like a duck, quacks like a duck, etc.
Works with strings,
integers,
floating point,
lists,
etc.
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
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
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. “””
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
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
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 ‘
Command Line Args:
C:> D:python27python.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
Command Line Args:
>>> userid = arcpy.GetParameterAsText(0)
>>> pword = arcpy.GetParameterAsText(1)
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
Oracle connection:
cursor.execute('select distinct diameter from conduit')
allresults = cursor.fetchall()
for result in allresults:
print result
cursor.close()
connection.close()
print "Done."
Nee
Here is a shrubbery.
Not Habit Forming:
But is considered a “Gateway”
language,
which frequently leads to more
nerdy behavior.
Good Book:
Good Book:
GIS Day November 22:
See denverwater.org for directions...
Becomes…

More Related Content

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

Lens: Data exploration with Dask and Jupyter widgets
Lens: Data exploration with Dask and Jupyter widgetsLens: Data exploration with Dask and Jupyter widgets
Lens: Data exploration with Dask and Jupyter widgets
Víctor Zabalza
 
Big Data - Lab A1 (SC 11 Tutorial)
Big Data - Lab A1 (SC 11 Tutorial)Big Data - Lab A1 (SC 11 Tutorial)
Big Data - Lab A1 (SC 11 Tutorial)
Robert Grossman
 
1403 app dev series - session 5 - analytics
1403   app dev series - session 5 - analytics1403   app dev series - session 5 - analytics
1403 app dev series - session 5 - analytics
MongoDB
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
Yehuda Katz
 

Similar to 2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by-Step by Zeke Houk (20)

Novalug 07142012
Novalug 07142012Novalug 07142012
Novalug 07142012
 
Umleitung: a tiny mochiweb/CouchDB app
Umleitung: a tiny mochiweb/CouchDB appUmleitung: a tiny mochiweb/CouchDB app
Umleitung: a tiny mochiweb/CouchDB app
 
11. From Hadoop to Spark 2/2
11. From Hadoop to Spark 2/211. From Hadoop to Spark 2/2
11. From Hadoop to Spark 2/2
 
Building a Cross Channel Content Delivery Platform with MongoDB
Building a Cross Channel Content Delivery Platform with MongoDBBuilding a Cross Channel Content Delivery Platform with MongoDB
Building a Cross Channel Content Delivery Platform with MongoDB
 
Lens: Data exploration with Dask and Jupyter widgets
Lens: Data exploration with Dask and Jupyter widgetsLens: Data exploration with Dask and Jupyter widgets
Lens: Data exploration with Dask and Jupyter widgets
 
Gmaps Railscamp2008
Gmaps Railscamp2008Gmaps Railscamp2008
Gmaps Railscamp2008
 
(BDT203) From Zero to NoSQL Hero: Amazon DynamoDB Tutorial | AWS re:Invent 2014
(BDT203) From Zero to NoSQL Hero: Amazon DynamoDB Tutorial | AWS re:Invent 2014(BDT203) From Zero to NoSQL Hero: Amazon DynamoDB Tutorial | AWS re:Invent 2014
(BDT203) From Zero to NoSQL Hero: Amazon DynamoDB Tutorial | AWS re:Invent 2014
 
Automating Research Data Flows and Introduction to the Globus Platform
Automating Research Data Flows and Introduction to the Globus PlatformAutomating Research Data Flows and Introduction to the Globus Platform
Automating Research Data Flows and Introduction to the Globus Platform
 
Capistrano2
Capistrano2Capistrano2
Capistrano2
 
Webinar: Applikationsentwicklung mit MongoDB : Teil 5: Reporting & Aggregation
Webinar: Applikationsentwicklung mit MongoDB: Teil 5: Reporting & AggregationWebinar: Applikationsentwicklung mit MongoDB: Teil 5: Reporting & Aggregation
Webinar: Applikationsentwicklung mit MongoDB : Teil 5: Reporting & Aggregation
 
Improving Apache Spark Downscaling
 Improving Apache Spark Downscaling Improving Apache Spark Downscaling
Improving Apache Spark Downscaling
 
What's new in Redis v3.2
What's new in Redis v3.2What's new in Redis v3.2
What's new in Redis v3.2
 
Gradle: The Build System you have been waiting for!
Gradle: The Build System you have been waiting for!Gradle: The Build System you have been waiting for!
Gradle: The Build System you have been waiting for!
 
Big Data - Lab A1 (SC 11 Tutorial)
Big Data - Lab A1 (SC 11 Tutorial)Big Data - Lab A1 (SC 11 Tutorial)
Big Data - Lab A1 (SC 11 Tutorial)
 
Living the Nomadic life - Nic Jackson
Living the Nomadic life - Nic JacksonLiving the Nomadic life - Nic Jackson
Living the Nomadic life - Nic Jackson
 
1403 app dev series - session 5 - analytics
1403   app dev series - session 5 - analytics1403   app dev series - session 5 - analytics
1403 app dev series - session 5 - analytics
 
Tuning and Debugging in Apache Spark
Tuning and Debugging in Apache SparkTuning and Debugging in Apache Spark
Tuning and Debugging in Apache Spark
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
 
Android gradle-build-system-overview
Android gradle-build-system-overviewAndroid gradle-build-system-overview
Android gradle-build-system-overview
 
Overview of Spark for HPC
Overview of Spark for HPCOverview of Spark for HPC
Overview of Spark for HPC
 

More from GIS in the Rockies

2018 GIS in the Rockies Vendor Showcase (Th): The Data Driven Government
2018 GIS in the Rockies Vendor Showcase (Th): The Data Driven Government2018 GIS in the Rockies Vendor Showcase (Th): The Data Driven Government
2018 GIS in the Rockies Vendor Showcase (Th): The Data Driven Government
GIS in the Rockies
 
2018 GIS in the Rockies Vendor Showcase (Th): Solving Real World Issues With ...
2018 GIS in the Rockies Vendor Showcase (Th): Solving Real World Issues With ...2018 GIS in the Rockies Vendor Showcase (Th): Solving Real World Issues With ...
2018 GIS in the Rockies Vendor Showcase (Th): Solving Real World Issues With ...
GIS in the Rockies
 
2018 GIS in Recreation: A Creek Runs Through It
2018 GIS in Recreation: A Creek Runs Through It2018 GIS in Recreation: A Creek Runs Through It
2018 GIS in Recreation: A Creek Runs Through It
GIS in the Rockies
 

More from GIS in the Rockies (20)

GISCO Fall 2018: Bike Network Equity: A GIS and Qualitative Analysis of Ameri...
GISCO Fall 2018: Bike Network Equity: A GIS and Qualitative Analysis of Ameri...GISCO Fall 2018: Bike Network Equity: A GIS and Qualitative Analysis of Ameri...
GISCO Fall 2018: Bike Network Equity: A GIS and Qualitative Analysis of Ameri...
 
GISCO Fall 2018: Colorado 811: Changes and Challenges – Brian Collison
GISCO Fall 2018: Colorado 811: Changes and Challenges – Brian CollisonGISCO Fall 2018: Colorado 811: Changes and Challenges – Brian Collison
GISCO Fall 2018: Colorado 811: Changes and Challenges – Brian Collison
 
GISCO Fall 2018: Senate Bill 18-167 and GIS – Dave Murray
GISCO Fall 2018: Senate Bill 18-167 and GIS – Dave MurrayGISCO Fall 2018: Senate Bill 18-167 and GIS – Dave Murray
GISCO Fall 2018: Senate Bill 18-167 and GIS – Dave Murray
 
2018 GIS in the Rockies Workshop: Coordinate Systems and Projections
2018 GIS in the Rockies Workshop: Coordinate Systems and Projections 2018 GIS in the Rockies Workshop: Coordinate Systems and Projections
2018 GIS in the Rockies Workshop: Coordinate Systems and Projections
 
2018 GIS in Emergency Management: Denver Office of Emergency Management Overview
2018 GIS in Emergency Management: Denver Office of Emergency Management Overview2018 GIS in Emergency Management: Denver Office of Emergency Management Overview
2018 GIS in Emergency Management: Denver Office of Emergency Management Overview
 
2018 GIS in the Rockies Vendor Showcase (Th): The Data Driven Government
2018 GIS in the Rockies Vendor Showcase (Th): The Data Driven Government2018 GIS in the Rockies Vendor Showcase (Th): The Data Driven Government
2018 GIS in the Rockies Vendor Showcase (Th): The Data Driven Government
 
2018 GIS in the Rockies Vendor Showcase (Th): Solving Real World Issues With ...
2018 GIS in the Rockies Vendor Showcase (Th): Solving Real World Issues With ...2018 GIS in the Rockies Vendor Showcase (Th): Solving Real World Issues With ...
2018 GIS in the Rockies Vendor Showcase (Th): Solving Real World Issues With ...
 
2018 GIS in the Rockies Vendor Showcase (Th): ERDAS Imagine What's New and Ti...
2018 GIS in the Rockies Vendor Showcase (Th): ERDAS Imagine What's New and Ti...2018 GIS in the Rockies Vendor Showcase (Th): ERDAS Imagine What's New and Ti...
2018 GIS in the Rockies Vendor Showcase (Th): ERDAS Imagine What's New and Ti...
 
2018 GIS in the Rockies Vendor Showcase (Th): Building High Performance Gover...
2018 GIS in the Rockies Vendor Showcase (Th): Building High Performance Gover...2018 GIS in the Rockies Vendor Showcase (Th): Building High Performance Gover...
2018 GIS in the Rockies Vendor Showcase (Th): Building High Performance Gover...
 
2018 GIS in Recreation: The Making of a Trail
2018 GIS in Recreation: The Making of a Trail2018 GIS in Recreation: The Making of a Trail
2018 GIS in Recreation: The Making of a Trail
 
2018 GIS in Recreation: The Latest Trail Technology Crowdsourcing Maps and Apps
2018 GIS in Recreation: The Latest Trail Technology Crowdsourcing Maps and Apps2018 GIS in Recreation: The Latest Trail Technology Crowdsourcing Maps and Apps
2018 GIS in Recreation: The Latest Trail Technology Crowdsourcing Maps and Apps
 
2018 GIS in the Rockies: Riparian Shrub Assessment of the Mancos River Canyon...
2018 GIS in the Rockies: Riparian Shrub Assessment of the Mancos River Canyon...2018 GIS in the Rockies: Riparian Shrub Assessment of the Mancos River Canyon...
2018 GIS in the Rockies: Riparian Shrub Assessment of the Mancos River Canyon...
 
2018 GIS in Development: Partnerships Lead to Additional Recreational Content...
2018 GIS in Development: Partnerships Lead to Additional Recreational Content...2018 GIS in Development: Partnerships Lead to Additional Recreational Content...
2018 GIS in Development: Partnerships Lead to Additional Recreational Content...
 
2018 GIS in Recreation: Adding Value to Colorado the Beautiful Initiative carr
2018 GIS in Recreation: Adding Value to Colorado the Beautiful Initiative carr2018 GIS in Recreation: Adding Value to Colorado the Beautiful Initiative carr
2018 GIS in Recreation: Adding Value to Colorado the Beautiful Initiative carr
 
2018 GIS in Recreation: A Creek Runs Through It
2018 GIS in Recreation: A Creek Runs Through It2018 GIS in Recreation: A Creek Runs Through It
2018 GIS in Recreation: A Creek Runs Through It
 
2018 GIS in Recreation: Virtually Touring the National Trails
2018 GIS in Recreation: Virtually Touring the National Trails2018 GIS in Recreation: Virtually Touring the National Trails
2018 GIS in Recreation: Virtually Touring the National Trails
 
2018 GIS in the Rockies PLSC Track: Turning Towards the Future
2018 GIS in the Rockies PLSC Track: Turning Towards the Future2018 GIS in the Rockies PLSC Track: Turning Towards the Future
2018 GIS in the Rockies PLSC Track: Turning Towards the Future
 
2018 GIS in the Rockies PLSC: Intro to PLSS
2018 GIS in the Rockies PLSC: Intro to PLSS2018 GIS in the Rockies PLSC: Intro to PLSS
2018 GIS in the Rockies PLSC: Intro to PLSS
 
2018 GIS in the Rockies PLSC Track: Grid to Ground NATRF2022
2018 GIS in the Rockies PLSC Track: Grid to Ground NATRF20222018 GIS in the Rockies PLSC Track: Grid to Ground NATRF2022
2018 GIS in the Rockies PLSC Track: Grid to Ground NATRF2022
 
2018 GIS in Development: USGS and Citizen Science Success and Enhancements fo...
2018 GIS in Development: USGS and Citizen Science Success and Enhancements fo...2018 GIS in Development: USGS and Citizen Science Success and Enhancements fo...
2018 GIS in Development: USGS and Citizen Science Success and Enhancements fo...
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

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

  • 5. Get Rid of Conduit 0:
  • 8. Where did the name come from?
  • 10. Clean up names: • >>> # Local variables: • >>> simple_conduits__3_ = R:ARG_ProjectsConduitMapBookDissolvedConduits.gdbsimple_conduits" • >>> dissolved_conduits__3_ = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbdissolved_conduits" • >>> ARG_CONDUIT__3_ = "Database ConnectionsConnection to DWGIS3.sdeARG.DistributionSystemARG.CONDUIT" • >>> simple_conduits__4_ = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbsimple_conduits" • >>> N0_Zero_Conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbN0_Zero_Conduits" • >>> dissolved_conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbdissolved_conduits" • >>> simple_conduits__6_ = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbsimple_conduits" >>> simple_conduits_3_, >>> simple_conduits_4_, >>> simple_Conduits_6_ >>> redundant…
  • 11. Replace with simple_conduits: • >>> # Local variables: • >>> simple_conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbsimple_conduits" • >>> dissolved_conduits__3_ = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbdissolved_conduits" • >>> ARG_CONDUIT__3_ = "Database ConnectionsConnection to DWGIS3.sdeARG.DistributionSystemARG.CONDUIT" • >>> simple_conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbsimple_conduits" • >>> N0_Zero_Conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbN0_Zero_Conduits" • >>> dissolved_conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbdissolved_conduits" • >>> simple_conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbsimple_conduits" Global replace simple_conduits For simple_conduits_3_, simple_conduits_4_, and simple_conduits_6_.
  • 12. Duplicates dropped: >>> # Local variables: >>> simple_conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbsimple_conduits" >>> dissolved_conduits__3_ = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbdissolved_conduits" >>> ARG_CONDUIT__3_ = "Database ConnectionsConnection to DWGIS3.sdeARG.DistributionSystemARG.CONDUIT" >>> N0_Zero_Conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbN0_Zero_Conduits" >>> dissolved_conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbdissolved_conduits" Global replace simple_conduits For simple_conduits_3_, simple_conduits_4_, and simple_Conduits_6_.
  • 13. Now fix dissolved_conduits: # Local variables: simple_conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbsimple_conduits" dissolved_conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbdissolved_conduits" ARG_CONDUIT__3_ = "Database ConnectionsConnection to DWGIS3.sdeARG.DistributionSystemARG.CONDUIT" N0_Zero_Conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbN0_Zero_Conduits" Becomes… # Local variables: simple_conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbsimple_conduits" dissolved_conduits__3_ = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbdissolved_conduits" ARG_CONDUIT__3_ = "Database ConnectionsConnection to DWGIS3.sdeARG.DistributionSystemARG.CONDUI N0_Zero_Conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbN0_Zero_Conduits" dissolved_conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbdissolved_conduits"
  • 14. Use File Geodatabase Name: • # Local variables: • file_gdb = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdb" • simple_conduits = file_gdb + "simple_conduits" • dissolved_conduits = file_gdb + "dissolved_conduits" • N0_Zero_Conduits = file_gdb + "N0_Zero_Conduits" • ARG_CONDUIT = "Database ConnectionsConnection to DWGIS3.sdeARG.DistributionSystemARG.CONDUIT" Becomes… • # Local variables: • simple_conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbsimple_conduits" • dissolved_conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbdissolved_conduits" • ARG_CONDUIT = "Database ConnectionsConnection to DWGIS3.sdeARG.DistributionSystemARG.CONDUI • N0_Zero_Conduits = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdbN0_Zero_Conduits"
  • 15. Use File Geodatabase Name: file_gdb = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdb" simple_conduits = file_gdb + "simple_conduits" dissolved_conduits = file_gdb + "dissolved_conduits" N0_Zero_Conduits = file_gdb + "N0_Zero_Conduits"
  • 16. Use SDE Connection Name: # Local variables: file_gdb = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdb" simple_conduits = file_gdb + "simple_conduits" dissolved_conduits = file_gdb + "dissolved_conduits" N0_Zero_Conduits = file_gdb + "N0_Zero_Conduits" sde_connection = "Database ConnectionsConnection to DWGIS3.sde" ARG_CONDUIT = sde_connection + "ARG.CONDUIT" Becomes… # Local variables: file_gdb = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdb" simple_conduits = file_gdb + "simple_conduits" dissolved_conduits = file_gdb + "dissolved_conduits" N0_Zero_Conduits = file_gdb + "N0_Zero_Conduits" ARG_CONDUIT = "Database ConnectionsConnection to DWGIS3.sdeARG.DistributionSystemARG.CONDUIT"
  • 17. Use SDE Connection Name: sde_connection = "Database ConnectionsConnection to DWGIS3.sde" ARG_CONDUIT = sde_connection + "ARG.CONDUIT"
  • 18. Cleaned up names: # Local variables: file_gdb = "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdb" simple_conduits = file_gdb + "simple_conduits" dissolved_conduits = file_gdb + "dissolved_conduits" N0_Zero_Conduits = file_gdb + "N0_Zero_Conduits" sde_connection = "Database ConnectionsConnection to DWGIS3.sde" ARG_CONDUIT = sde_connection + "ARG.CONDUIT"
  • 20.
  • 21. PyWin: PyWin Python for Windows extensions by Mark Hammond http://sourceforge.net/projects/pywin32/ pywin32-218.win32-py2.7.exe
  • 22. 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
  • 23. 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 ConnectionsConnection to DWGIS3.sdeARG.DistributionSystemARG.CONDUIT,COND_NUM,-1,- 1;WATER_TYPE "WATER_TYPE" true true false 5 Text 0 0 ,First,#,Database ConnectionsConnection to DWGIS3.sdeARG.DistributionSystemARG.CONDUIT,WATER_TYPE,-1,- 1;Shape_Length "Shape_Length" false true true 8 Double 0 0 ,First,#", "") Becomes… • # Process: Append • arcpy.Append_management( • "'Database ConnectionsConnection to DWGIS3.sde • ARG.DistributionSystemARG.CONDUIT'", simple_conduits, • "NO_TEST", "COND_NUM "COND_NUM" true true false 2 Short 0 0 ,First,#,Database C
  • 24. Fix up Process Steps: • # Process: Delete • arcpy.Delete_management(N0_Zero_Conduits, "FeatureClass") Becomes… • # Process: Delete • arcpy.Delete_management( • "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdb • NO_Zero_Conduits", • "FeatureClass")
  • 25. Fix up Process Steps: • # Process: Delete • arcpy.Delete_management(dissolved_conduits, "FeatureClass") Becomes… • # Process: Delete • arcpy.Delete_management( • "R:ARG_ProjectsConduit MapBookDissolvedConduits.gdb • dissolved_conduits", • "FeatureClass")
  • 26. 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 ConnectionsConnection to DWGIS3.sdeARG.DistributionSystemARG.CONDUIT,COND_NUM,-1,-1;WATER_TYPE "WATER_TYPE" true true false 5 Text 0 0 ,First,#,Database ConnectionsConnection to DWGIS3.sdeARG.DistributionSystemARG.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")
  • 27. Geek Speak - If: if arcpy.Exists(something): # do something with it if not arcpy.Exists(something): # it does not exist
  • 28. Avoid Errors: if arcpy.Exists(dissolved_conduits): arcpy.Delete_management(dissolved_conduits, "FeatureClass") Becomes… arcpy.Delete_management(dissolved_conduits, "FeatureClass")
  • 29. 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
  • 30. Alternative: >>> from arcpy import env >>> env.overwriteOutput = True >>> env.workspace = “C:/EsriPress/Python/Data/”
  • 31. 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.
  • 32. Geek Speak – Try / Catch: >>> try: >>> # .. To do something that might blow up >>> # and it “throws” an Exception >>> except Exception ex: >>> print ex >>>
  • 33. Catch Exception: >>> try: >>> arcpy.Append_management(CONDUIT >> except Exception as ex: >>> print ex
  • 34. Test Exception: >>> try: >>> bogus = 1.0 / 0.0 # forces exception >>> arcpy.Append_management(ARG_CONDUIT, >>> except Exception as ex: >>> print ex
  • 35. 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."
  • 36. C:tempdemo.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!
  • 37. 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)
  • 38. 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.”
  • 39. Pythonic: • Explicit is better than implicit. • Simple is better than complex. • Readability counts. The Zen of Python, by Tim Peters, includes:
  • 40. 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
  • 41. Duck Typing: If it looks like a duck, quacks like a duck, etc. Works with strings, integers, floating point, lists, etc.
  • 42. 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
  • 43. 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
  • 44. 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. “””
  • 45. 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
  • 46. 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
  • 47. 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 ‘
  • 48. Command Line Args: C:> D:python27python.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
  • 49. Command Line Args: >>> userid = arcpy.GetParameterAsText(0) >>> pword = arcpy.GetParameterAsText(1)
  • 50. 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
  • 51. Oracle connection: cursor.execute('select distinct diameter from conduit') allresults = cursor.fetchall() for result in allresults: print result cursor.close() connection.close() print "Done."
  • 52. Nee
  • 53. Here is a shrubbery.
  • 54. Not Habit Forming: But is considered a “Gateway” language, which frequently leads to more nerdy behavior.
  • 57. GIS Day November 22: See denverwater.org for directions...