ArcPro Calculate Field with Python Code Block

05-23-2018 02:08 PM
New Contributor

I have a dataset for utility work permits and we recently started tracking applicant email addresses so I am looking for help in writing a script for the Code Block field to do the bulk of the work of populating emails for the applicants that have more than one permit.  The logic behind this would essentially be:

if ContactName = Joe Schmo

return in the email field

elif ContactName = Jane Doe

return in the email field

then if ContactName = anything else

return nothing

I would like to add more cases to cover more bases, but only spelt out those 3 for brevity.  I have searched and searched already for previous examples and have tried to write several bits to try to accomplish this but to no avail.  Here are the two bits I have tried to run and have gotten nowhere with both:

Email = Reclass(!Email!)

def Reclass(Email):
    if (ContactName = Joe Schmo)
    elif (ContactName = Jane Doe)


fieldName = "Email"  
expression = "getClass(!ContactName!, Email!)"  
codeblock = """def getClass(ContactName, Email):  
    if ContactName == 'Joe Schmo':  
        return ""  
    return !Email!"""  
arcpy.CalculateField_management(out_feature_class, fieldName, expression, "PYTHON_9.3", codeblock)


I am using ArcPro, so I know the version of Python that it uses is different than ArcMap's, but that is literally all I know.  I do not know what the differences in the syntaxes are and I do not have experience with any syntax or calling functions with Python or any scripting language.  I have next to no background in scripting, so any help on how to set this up in the Code Block would be greatly appreciated.

0 Kudos
4 Replies
MVP Esteemed Contributor

expression = "getClass(!ContactName!, Email!)"   missing ! before Email, assuming Email is a field

0 Kudos
New Contributor

Yes, "Email" is a field.  Other than that missing !, should that bit of code do the operation I mean it to?  Also, if I want to add other cases to run in the same code, would I use "elif", "else", or "if" for the remaining cases before the null case?

0 Kudos
MVP Esteemed Contributor
# ---- d is a dictionary
#      fld is emulating your field
#      my_func is a sample function to which you pass the field
#      Then you 'call' the function passing the field name (ie would have ! ! around it
#  sample print, but you would use 'return' instead
d = {'bob': 'bob_email', 'bee': 'bee_email', 'sue': 'sue_email', 'sie': 'sie_email'}

fld = ['sue', 'bob', 'sie', 'no_one']

def my_func(fld):
    """sample function"""
    for i in fld:
        if i in d.keys():
            val = d[i]
            val = None


if elif else does the program flow control

but if it is going to get ridiculously long, you might want to consider an alternate approach, like using a dictionary.

MVP Esteemed Contributor

I agree with Dan, this will get messy and impossible to manage fairly quickly.  I think a better approach overall would be to maintain a separate table of users and their e-mail addresses.  Then, you can do a join of the user e-mail address table with the permit table.