Home arrow GIS Data & Resources arrow Scripts and Code arrow Python arrow Python! data persistence/object serialization
Python! data persistence/object serialization PDF Print E-mail

Written by Kevin Bell,


Wouldn't it be nice if you could take one of you python data objects and just "can it" to open up later?  With the "pickle" library you can do just that. 

I need to compare current data to historical data, so I make a dictionary of OID's with all attributes that looks something like this:

PK99999: (1059, 4, X, Y, etc, etc...)
PK00001: (1259, 2, X, Y, etc, etc...)

So, I like to slurp this dictionary up into a pickle, then open it up a month later to compare against the current data. #--------- WRITE A PICKLE -------------------------------

import pickle
import arcgisscripting
gp = arcgisscripting.create(9.3)

d = {} #----- python dictionary
rows = gp.SearchCursor(r"C:\slcdot.gdb\trafficAccidents")
row = rows.next()
while row:
    d[row.primary_key] = row.occ_date, \
                                    row.occ_time, \
                                    row.week_day, \
                                    row.x_coord, \
                                    row.y_coord, \
                                    row.sev, \
                                    row.loc, \
                                    row.drug_alc,\
                                    row.hit_run, \
                                    row.stolenVeh, \
                                    row.numVeh, \
                                    row.numInj, \
                                    row.numKilled, \
                                    row.motorcycle, \
                                    row.parkedVeh, \
                                    row.ped, \
                                    row.bike, \
                                    row.others, \
                                    row.damage, \
                                    row.otherDam, \
                                    row.charges, \
                                    row.dark, \
                                    row.hyperlink
    row = rows.next()

print len(d)

output = open('accidents.pkl', 'wb')
pickle.dump(d, output)
output.close()
print "done"



#----------- EAT A PICKLE -------------------------------------------------

import pickle
pklFile1 = open('accidents.pkl', 'rb')
d1 = pickle.load(pklFile1)
pklFile1.close()

pklFile2 = open('past6months.pkl', 'rb')
d2 = pickle.load(pklFile2)
pklFile2.close()

d3 = {}

for k in d1.keys(): # test for differences between 2 dictionaries
    #print "key: " + str(k)
    if d2.has_key(k):
        if d1[k] == d2[k]:
            print "key: " + str(k)
            print "------------ same values for key"
        else:
            print "key: " + str(k)
            print "------------ there are differences with 1 or more values"
            tup1, tup2 = d1[k], d2[k]
            for i in xrange(len(tup1)):
                if tup1[i] == tup2[i]:
                    print "d2: " + str(k) + "[" + str(i) + "] ...same"
                else:
                    print "d2: " + str(k) + "[" + str(i) + "] -------------DIFFERENT!"
                    d3[str(k)] = d2[k]
                    #updateRow(k)
    print "\n"

output = open('changedRows_past6months.pkl', 'wb')
pickle.dump(d3, output)
output.close()
print "done building CHANGED ROWS pickle"

# the output is a delta pickle...

Users' Comments  
 

No comment posted

Add your comment

27, Jul. 2009
Last Updated ( 27, Jul. 2009 )
 
Next >

AGRC Contacts | UGIC Contacts

feed image feed image

Utah GIS Portal © 2009 AGRC

Optimized for