Select to view content in your preferred language

Using decorators to initiate an edit session and running update cursors?

660
5
12-05-2022 11:50 AM
RPGIS
by
Regular Contributor

Hi,

I came across the use of decorators while watching YouTube videos on python, and I thought that there might be a way to utilize this to run the update cursor method without having to specify multiple functions or program it right off the bat. It seems to run this segment of code just fine, but I wanted to see if anyone else has done anything like this. Any examples that would be greatly appreciated.

 

 

# Python code to illustrate
# Decorators with parameters in Python
def decorator():
     
    def inner(func):
         
        print ('Starting edit process\n')
        
        # Start edit operation
        print (f'edit = arcpy.da.Editor({Database})')
        print ('edit.startEditing(False, True)')
        print ('edit.startOperation()\n')
         
        func()
        
        # Finish edit operation
        print ('edit.stopOperation()')
        print ('edit.stopEditing(True)\n')
        print ('Finished edit process\n')
        
    # returning inner function   
    return inner
 
@decorator()
def my_func():
    s = ' '*4
    print (f'with udpate as cursor:{s}')
    print (f'{s}for row in cursor:')
    print (f'{s*2}if row[tile] in {Dictionary}:')
    print (f'{s*3}row[tile]=row[tile]')
    print (f'{s*3}row[shape]={Dictionary}[row[tile]]')
    print (f'{s*3}cursor.updateRow(row)\n')

Database = 'AFavorites\GIS.gdb'
Dictionary = 'ATest'

 

 

 

5 Replies
JohannesLindner
MVP Frequent Contributor

AFAIK, arcpy has no native decorators. You would have to define them yourself.

But, the arcpy.da.Editor class supports the with statement, which is somewhat similar to a decorator (it can execute code before and after your function). For the with statement to work, a class need to implement the __enter__ and __exit__ methods. These are called when the script enters and exits the with block.

In your example, you're already using the UpdateCursor in a with block. The UpdateCursor's __enter__ and __exit__ methods open and close the cursor and take care of locking the edited tables.

The Editor's __enter__ and __exit__ methods open and close the edit session.

 

with arcpy.da.Editor("your/database.gdb") as edit:
    with arcpy.da.UpdateCursor("Table", ["Field1", "Field2"]) as cursor:
        for row in cursor:
            #...
            cursor.updateRow(row)

Have a great day!
Johannes
0 Kudos
RPGIS
by
Regular Contributor

Thanks @JohannesLindner,

I haven't tried using edit in regards to the "with" method. The code was copied from the Esri documentation for editing in arcpy.

When I have tried to use the update cursor, I would get an error message stating that the database needs to be in an edit session before the update cursor could run.

But if what you say also works, then I'll give that a try instead since it's also fewer lines of code. 

0 Kudos
BlakeTerhune
MVP Regular Contributor

If you're editing a versioned feature class in an enterprise geodatabase, you probably need to use the Editor class.

0 Kudos
RPGIS
by
Regular Contributor

I do edit versioned feature classes so if that's the case, would a decorator work and would it be similar to the example that I posted. 

0 Kudos
BlakeTerhune
MVP Regular Contributor

Sorry, I'm really not sure about the correct use of a decorator with Editor. I always use it in a with statement, like cursors.

0 Kudos