Select to view content in your preferred language

function behaves differently as standalone script than as a function called in a larger module

679
4
08-17-2023 08:52 AM
clt_cabq
Occasional Contributor III

I have a function that standardizes addresses in a table based on values in a dictionary. The function when used in a script by itself works perfectly well and updates all records appropriately. However, when I call the function inside another module, it only changes records where it finds changes are needed. I solved the issue in the code posted below by adding an 'else' statement, but it is unclear to me why this should work differently in one context and not another.

 

 

def update_address():
# in_fc is a table of values defined outside the function
    arcpy.management.AddField(in_fc, "AddressOriginal",'text', "","", 200, "Original Address")
    arcpy.management.CalculateField(in_fc,'AddressOriginal',"!Address!")
    # function standardizes addresses
    def ModAddr(field):
      dct = {" Northeast": " NE", " Northwest": " NW"," Southeast": " SE", " Southwest": " SW",", Albuquerque, NM": "", " BL ": " BLVD ",
             " Avenue ": " AV "," Boulevard ": " BLVD ", " BY ": " BYPASS ",
" CI ": " CIR "," Circle ": " CIR ",  " Court ": " CT ",
             " Drive ": " DR ", " Freeway ": " FRWY ", " FY ": " FRWY "," Lane ": " LA ", " LP ": " LOOP ", " PY ": " PKWY ",
             " Place ": " PL "," Road ": " RD ", " Street ": " ST ", " TL ": " TRL ", " Way ": " WY ", " AVE ": " AV ", " EX ": " EXT "}
      for find_txt, replace_txt in dct.items():
        if find_txt in field:
              field = field.replace(find_txt, replace_txt)
        else: # added this else statement and function works fine
              field = field
      return field
    with arcpy.da.UpdateCursor(in_fc,"Address") as cursor:
      for addr in cursor:
            addr[0] = ModAddr(addr[0])
            cursor.updateRow(addr)

 

 

 

Tags (1)
0 Kudos
4 Replies
BlakeTerhune
MVP Regular Contributor

Unrelated comment: It seems weird that you have variables for in_fc in update_address() but it's not a parameter. I recommend changing that to be a parameter you pass in when you call update_address().

Also, string.replace() will return the original string even if there's no replacements to be made, so you don't need to check if find_txt is in field; just make the replacement.

On to your question, I'm also not sure why it would behave differently. However, you say that in one case it updates everything and the other it only updates things that have changes. It seems like you would only want to update things that have changes, no? Maybe I need more clarification.

0 Kudos
clt_cabq
Occasional Contributor III

I'm working on the script to pass in the 'in_fc' or to reference it directly inside the function. Ultimately the module this is in will be imported and run as function calls from inside another script. Work in progress!

And sorry, about not being clear - what happens in the version without the 'else' is those records (where no changes are found) end up as nulls instead of the original text, which is the problem. The 'field = field' line seems to correct this issue. 

0 Kudos
BlakeTerhune
MVP Regular Contributor

@clt_cabq wrote:

what happens in the version without the 'else' is those records (where no changes are found) end up as nulls instead of the original text, which is the problem. The 'field = field' line seems to correct this issue. 


Oh, interesting. There's no chance you have a selection on the feature class when you're calling update_address()? I don't see what would cause that behavior.

0 Kudos
clt_cabq
Occasional Contributor III

I don't believe I had a selection set - this table is created by a module in a step before this function runs and so shouldn't, I'm running the code in an IDE and not within ArcGIS Pro.  I was viewing and interacting with the table in Pro as I ran the script in VS Code, but I did this multiple times to try and figure out what was going on. It is entirely possible I was doing something that caused this, but I can't for the life of me think what it could have been, the table isn't even open in my Pro project except as I need to review results.

0 Kudos