I have a service that chooses whether to give a field one value or another depending on what's present in the data already. When I run the python script outside of the server, it runs fine. When I run it on the server, it fails because it doesn't recognize the uuid module in the code block. I think it's treating uuid as a string, but to use code blocks you pass in the whole code block as a string. Has anybody successfully used code blocks in their GP services?
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">getTractID</SPAN><SPAN class="punctuation token">(</SPAN>isDuplicate<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">import</SPAN> uuid
<SPAN class="keyword token">if</SPAN> isDuplicate <SPAN class="operator token">==</SPAN> <SPAN class="string token">'dupe'</SPAN><SPAN class="punctuation token">:</SPAN>
tractID <SPAN class="operator token">=</SPAN> <SPAN class="string token">'{'</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>uuid<SPAN class="punctuation token">.</SPAN>uuid4<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">'}'</SPAN>
tractID <SPAN class="operator token">=</SPAN> tractID<SPAN class="punctuation token">.</SPAN>upper<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>RemoveJoin_management<SPAN class="punctuation token">(</SPAN>mgmtTractFL<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>UpdateCursor<SPAN class="punctuation token">(</SPAN>mgmtTractFL<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'MgmtTractID'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> uCursor<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> uCursor<SPAN class="punctuation token">:</SPAN>
row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> tractID
uCursor<SPAN class="punctuation token">.</SPAN>updateRow<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">del</SPAN> uCursor
arcpy<SPAN class="punctuation token">.</SPAN>AddJoin_management<SPAN class="punctuation token">(</SPAN>mgmtTractFL<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'OBJECTID'</SPAN><SPAN class="punctuation token">,</SPAN> duplicatePolys<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'IN_FID'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
codeblock <SPAN class="operator token">=</SPAN> <SPAN class="string token">"""def ID():
return '{' + str(uuid.uuid4()) + '}'"""</SPAN>
expression <SPAN class="operator token">=</SPAN> <SPAN class="string token">'ID().upper()'</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CalculateField_management<SPAN class="punctuation token">(</SPAN>mgmtTractFL<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'MgmtTractID'</SPAN><SPAN class="punctuation token">,</SPAN> expression<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'PYTHON'</SPAN><SPAN class="punctuation token">,</SPAN> codeblock<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
On my server it converts 'ID().upper()' into a g_ESRI_variable. I still don't get why it would affect it's efficacy though. The 'expression' variable would still be a string.
g_ESRI_variable_3 <SPAN class="operator token">=</SPAN> u<SPAN class="string token">'ID().upper()'</SPAN>
g_ESRI_variable_4 <SPAN class="operator token">=</SPAN> u<SPAN class="string token">'MgmtTractID'</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">getTractID</SPAN><SPAN class="punctuation token">(</SPAN>isDuplicate<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">import</SPAN> uuid
<SPAN class="keyword token">if</SPAN> isDuplicate <SPAN class="operator token">==</SPAN> <SPAN class="string token">'dupe'</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token">#stuff</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
codeblock <SPAN class="operator token">=</SPAN> <SPAN class="string token">"""def ID():
return '{' + str(uuid.uuid4()) + '}'"""</SPAN>
expression <SPAN class="operator token">=</SPAN> g_ESRI_variable_3
arcpy<SPAN class="punctuation token">.</SPAN>CalculateField_management<SPAN class="punctuation token">(</SPAN>mgmtTractFL<SPAN class="punctuation token">,</SPAN> g_ESRI_variable_4<SPAN class="punctuation token">,</SPAN> expression<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'PYTHON'</SPAN><SPAN class="punctuation token">,</SPAN> codeblock<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>If this can't be figured out, I can always use a cursor but I figure CalculateField would be faster when there's 1000+ records.