Setting a Single Record to Selected with Python

1395
8
07-16-2010 01:50 PM
by Anonymous User
Not applicable
Original User: BrendanM

Hello

With Avenue it was possible to select a single record in a table using:

theVTab.GetSelection.Set(rec)

I cannot find an equivalent in Python. The only work-around I can find is using a "Select by Attribute" on the OID field. But this is cumbersome when working with datasets of tens or hundreds of thousands of records.

Any help would be much appreciated. Thank you.

Brendan Magill
0 Kudos
8 Replies
DanPatterson_Retired
MVP Emeritus
Your approach is correct...don't blame Python, blame what has been exposed for Python access, otherwise dive into arcobjects
0 Kudos
by Anonymous User
Not applicable
Original User: csny490

The tool MakeFeatureLayer or it's tabular equivalent MakeTableView is a much faster way to "select" features and/or records (use the SQL parameter to select on OID or some other attribute). I'm guessing it's faster than SelectLayerByAttribute since it doesn't have all the fancy selection criteria like "switch selection" and also lacks some of the gp.describe functionality like the .fidset property. MakeFeatureLayer is just stuck in "New Selection" mode. For example:

featureLayer = "mickey_mouse"
fc = r"C:\temp\test.gdb\test"
gp.MakeFeatureLayer_management(fc, featureLayer, "OBJECTID = 1")
0 Kudos
by Anonymous User
Not applicable
Original User: BrendanM

Thanks Dan & Chris.

I am currently trying to run two cursors concurrently - one (SearchCursor) stepping through the records and pulling the relevant values with another (UpdateCursor) selecting records based on the values in the SearchCursor, but this does not seem to be working. Probably cant run two cursors simultaneously.

Seems I may need to take the plunge into ArcObjects.
0 Kudos
DanPatterson_Retired
MVP Emeritus
Haven't tried it, but can you not clone the original file (copy to a dup on disk) then run the search cursor on it and update on the other?
0 Kudos
by Anonymous User
Not applicable
Original User: csny490

imbeded cursors work fine, but they cam be extreemly slow. If you are basically treating the search cursor like a look up table, you might want to look at using a Python dictionary object rather than an imbeded cursor - it will be WAY faster. A simple example:

lookupDict = {}
searchRows = gp.searchcursor(lookupTbl)
searchRow = searchRows.next()
while searchRow:
   lookupDict[searchRow.LOOKUPITEM] = [searchRow.FIELDVALUE1,searchRow.FIELDVALUE2]
   searchRow = searchRows.next()
del searchRow
del searchRows

updateRows = gp.updatecursor(tableToUpdateTbl)
updateRow = updateRows.next()
while updateRow:
   updateRow.MEAN = (lookupDict[updateRow.LOOKUPITEM][0] + lookupDict[updateRow.LOOKUPITEM][1]) / 2
   updateRows.UpdateRow(updateRow)
   updateRow = updateRows.next()
del updateRow
del updateRows
del lookupDict
0 Kudos
by Anonymous User
Not applicable
Original User: BrendanM

Sorry, actually it does work - running two cursors concurrently, that is. It worked well, but, as Chris noted, very slow - took 10 hours to process 50 000 records.

Thanks for your responses. I will try the dictionary object methodology as well.

Brendan Magill
0 Kudos
ChrisSnyder
Regular Contributor III
Yes, I meant that indeed imbeded cursors work, but there is a big difference between working and working fast. Dictionaries are basically a way to build light-speed database tables(s) in memory. Some practical examples using Python dictionaries:

http://forums.esri.com/thread.asp?t=275202&f=1729&c=93#948148 (see the .zip file)

http://arcscripts.esri.com/details.asp?dbid=16700
0 Kudos
by Anonymous User
Not applicable
Original User: BrendanM

Hello Chris
Apologies. When I said "sorry, actually it does work" I was referring to my previous post where I said running two cursors concurrently doesn't work. Wasn't referring to your comment about imbedded cursors. Thanks for the tips on dictionaries - it is definitely something I need to explore further.
Regards
Brendan Magill
0 Kudos