Arcpy inconsistent in setting layout map extents

Discussion created by mdenil on Feb 29, 2012
Latest reply on Sep 26, 2014 by JamesRamm
I have been using arcpy to set a layout data frame to one of several (39) different locations. The data frame parameters I am setting are map extents, rotation and scale, as well as page location, height and width. I also set some text labels and apply a definition query to certain data layers.

All of the parameters are (currently) saved in a pickled python dictionary. They will likely eventually migrate to a Geodatabase table, but in development the dictionary is convenient.

My problem is with setting the extents for the data frame object. Sometimes the extents set just fine (the frame takes the extents I specify), but often it does not. Sometimes the error is in both the X and Y, and sometimes in only one or the other. It is not consistent in which of the named extents is affected: any one particular map may be correct, wrong in X, wrong in Y, or wrong in both X and Y any particular time.

I have been testing the routine by looping through the process 1000 times, and in each iteration I select a random entry from the parameter dictionary. After each time the frame is set, I create a new data frame object and test the parameters against the values I had just used for setting it.

In 1000 iterations:
426 had no extent errors, and 574 had some error.
of the 574 errors:
279 were incorrect in both X and Y
138 were incorrect in X alone
157 were incorrect in Y alone

There were no errors in rotation, scale, height, width, or page location.
Other values from the dictionary (area long name and abbreviation) are also always correct.

The problem seems to be that setting the extent values do not always 'take'.

The setting code is quite straightforward:
##      set extent
    newExtent = df.extent
    newExtent.XMin = extent[0]
    newExtent.YMin = extent[1]
    newExtent.XMax = extent[2]
    newExtent.YMax = extent[3]
    df.extent = newExtent

df is the data frame object, and extent is a list of extent parameters saved in the dictionary entry.

Values from a previous loop are not being carried over. The wrong values, when they appear, are just plain wrong and seem unconnected with anything.

Why is the data frame object extent object so flaky?