Geoprocessing services not agree with code blocks?

294
3
Jump to solution
02-14-2019 07:07 AM
MollyFoley
Regular Contributor

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?

def getTractID(isDuplicate):
    import uuid
    if isDuplicate == 'dupe':
        tractID = '{' + str(uuid.uuid4()) + '}'
        tractID = tractID.upper()
        arcpy.RemoveJoin_management(mgmtTractFL)
        with arcpy.da.UpdateCursor(mgmtTractFL, 'MgmtTractID') as uCursor:
            for row in uCursor:
                row[0] = tractID
                uCursor.updateRow(row)
        del uCursor
        arcpy.AddJoin_management(mgmtTractFL, 'OBJECTID', duplicatePolys, 'IN_FID')
    else:
        codeblock = """def ID():
            return '{' + str(uuid.uuid4()) + '}'"""
        expression = 'ID().upper()'
        arcpy.CalculateField_management(mgmtTractFL, 'MgmtTractID', expression, 'PYTHON', codeblock)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

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 = u'ID().upper()'
g_ESRI_variable_4 = u'MgmtTractID'

def getTractID(isDuplicate):
    import uuid
    if isDuplicate == 'dupe':
        #stuff
    else:
        codeblock = """def ID():
            return '{' + str(uuid.uuid4()) + '}'"""
        expression = g_ESRI_variable_3
        arcpy.CalculateField_management(mgmtTractFL, g_ESRI_variable_4, expression, 'PYTHON', codeblock)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

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.

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

Just for fun, can you try importing uuid inside the codeblock?

View solution in original post

3 Replies
DarrenWiens2
MVP Honored Contributor

Just for fun, can you try importing uuid inside the codeblock?

MollyFoley
Regular Contributor

That did the trick, thanks.

codeblock = """def ID():
                import uuid
                return '{' + str(uuid.uuid4()) + '}'"""
expression = 'ID().upper()'
arcpy.CalculateField_management(mgmtTractFL, 'MgmtTractID', expression, 'PYTHON', codeblock)‍‍‍‍‍
DanPatterson_Retired
MVP Esteemed Contributor

You may have to put the import uuid inside your quoted code block as well

0 Kudos