Select to view content in your preferred language

Python If statement for Code Block

4880
11
06-16-2011 01:07 PM
ChristopherBlinn
Emerging Contributor
Hello,

I am new to Python, and programming all together really.  I am building a model where I want the user to select from a drop down list a value for a parameter (Climate Variable).  I then want to use the Calculate Value tool to then get a string value for a variable that will be used to choose a field from a feature attribute table (PYCLIM).  I wrote this If Statement (which I believe is wrong) to do such and I am getting the 000989 syntax error on Line 1.

def getCLIMVAR(PYCLIM):
 climvar = str(%Climate Variable%):
  if climvar == "Current Temperature":
   PYCLIM = "CUR_TEMP"
   return PYCLIM
  elif climvar == "High Temperature":
   PYCLIM == "HI_TEMP"
   return PYCLIM
  elif climvar == "Low Temperature":
   PYCLIM == "LOW_TEMP"
   return PYCLIM
  elif climvar == "Wind Chill Temperature":
   PYCLIM == "CHILL_TEMP"
   return PYCLIM
  elif climvar == "Dew Point Temperature":
   PYCLIM == "DEW_TEMP"
   return PYCLIM
  elif climvar == "High Temperature":
   PYCLIM == "HI_TEMP"
   return PYCLIM
  elif climvar == "Relative Humidty":
   PYCLIM == "REL_HUMID"
   return PYCLIM
  elif climvar == "Solar Radiation":
   PYCLIM == "SOL_RAD"
   return PYCLIM
  elif climvar == "Wind Speed":
   PYCLIM == "WIND_SPEED"
   return PYCLIM
  else climvar == "Highest Wind Gust":
   PYCLIM == "HI_WIND_GUS"
   return PYCLIM


Any help would be appreciated.  Thank you in advance! If the code is horrible, please understand that I wrote this without ANY python experience.
Tags (2)
0 Kudos
11 Replies
ChrisSnyder
Honored Contributor
I kinda hinted at this before, but the code block parameter for the CalculateField tool is notoriously difficult (especially if you start trying to put dictionaries and what not into the code). See: http://forums.esri.com/Thread.asp?c=93&f=1729&t=262236#806786

I would offer that perhaps your best bets would be:

1. Rewrite your process entirely in Python, and use an update cursor instead of the codeblock. There is a little learning curve, but trust me, the sooner the better! MB is a waste of time if you are trying to do more colex stuff like conditional expressions and looping.
2. If you really want to stay on the ModelBuilder path, instead of the codeblock, use a series of MakeFeatureLayer tools (each applying the correct SQL - example: TIME = '5 minutes; ), and then the appropriately matching CalculateField tool - example: TF = '5min'.

Granted option #2 is not as efficient as a codeblock or update cursor in terms of updating the table, but in terms of "real time" (time it takes to actually get the damn thing to work), it will by far be the best option.

Sorry I don't have a silver bullet for the code block syntax, but I learned long ago to not put complex code into that thing since the formatting was so difficult to get correct.
0 Kudos
ChristopherBlinn
Emerging Contributor
Chris,

Thanks for all of the information.  I have been absorbing everything about python the past couple months that I can.  I think I am going to apply both options, first the second just to get a product going, then write the entire thing in python for future use.  Thanks again for all of your help.

-Chris
0 Kudos