|
POST
|
The PEP8 Style Guide is a great place to start. Now that you a familiar with the syntax, the style guides will make more sense. From the start, the style guides recommend putting each module you import on its own line. So instead of this import time, sys, platform, imp, arcpy Do this import arcpy
import imp
import platform
import sys
import time Inline comments are helpful but can be overdone, so be careful. They should also be separated from the code line by at least two spaces (you only have one). Consider the maximum line length. Super long lines of code are very hard (for a human) to interpret and arcpy functions are notorious for turning into monsters. Try declaring more of your function arguments as variables and take advantage of the multi-line capabilities within the parenthesis. Instead of this arcpy.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", ' "DCA1" = 80003 OR "DCA2" = 80003 OR "DCA3" = 80003 OR "DCA1" = 11006 OR "DCA2" = 11006 OR "DCA3" = 11006 ') Do this where_clause = ' "DCA1" = 80003 OR "DCA2" = 80003 OR "DCA3" = 80003 OR "DCA1" = 11006 OR "DCA2" = 11006 OR "DCA3" = 11006 '
arcpy.SelectLayerByAttribute_management(
outLayer,
"NEW_SELECTION",
where_clause
) Or you can take advantage of multiline strings and do something like this for the where clause. I used triple double quotes (""") but those are typically used for docstrings so I try to use triple single quotes ('''). However, the GeoNet forums do funny things with triple single quotes in Python syntax highlighting so I had to write this with triple double quotes. where_clause = """
"DCA1" = 80003
OR "DCA2" = 80003
OR "DCA3" = 80003
OR "DCA1" = 11006
OR "DCA2" = 11006
OR "DCA3" = 11006
""" But I would go even further and actually improve the SQL for the where clause and do this where_clause = """
"DCA1" IN(8003,11006)
OR "DCA2" IN(8003,11006)
OR "DCA3" IN(8003,11006)
""" It may also help to declare some smaller local variables at the beginning of the code block it will be used in rather than everything at the beginning. Or you can use comments to group the variables by use, like "connections", "tables", "feature classes", etc. When opening files to read and write (like you've done at the end with the log file), it's better practice to use a with statement so the file is always closed, even if there was an error. If your current code errored after you opened the file and before you closed it, the file would not get closed. Finally, a lot of Python IDEs style two types of comments differently: # and ## I like to use the single hash comment (#) for the heading of a large block of code and the double hash (##) for headings under the main heading or for inline comments. I also try to put one line between the main blocks of code and no lines between code that is all together so you can visually group the major blocks of logic. If a block of code is too long to easily fit this style, then it might be better off written as a function and called from your main(). I'm still learning Python myself and started just like you, Matthew. Please correct me if my assumptions are wrong!
... View more
02-11-2015
12:39 PM
|
3
|
2
|
2161
|
|
POST
|
Sorry for all the trouble but I'm glad you got something to work!
... View more
02-10-2015
02:04 PM
|
0
|
1
|
1605
|
|
POST
|
I should have clarified that you only need the arcpy.ClearWorkspaceCache_management() at the end if you're making an SDE connection. so if you're in a personal or file geodatabase you can get rid of that line.
... View more
02-10-2015
01:55 PM
|
1
|
0
|
2182
|
|
POST
|
Aww, what are you doin' to me giving me data that isn't the same as what you're using! haha I just created a new table (using MS Access) with no ObjectID field and the script still completed successfully and copied the rows. I'm on 10.2.2. Not sure why it's not working for you... Could you try making new feature classes and tables that all have ObjectID fields, then edit the script to point to the new stuff and see if it runs.
... View more
02-10-2015
10:43 AM
|
0
|
0
|
2018
|
|
POST
|
I just tried it again and it works fine for me. All tables and feature classes in the MDB you sent do have an ObjectID field. What version of ArcGIS are you on?
... View more
02-10-2015
09:59 AM
|
0
|
2
|
2018
|
|
POST
|
Download this Python script file then right click, edit with IDLE. Change the local variable paths as needed. Then go to Run, Run Module.
... View more
02-10-2015
08:24 AM
|
0
|
4
|
2018
|
|
POST
|
I did try putting in a wait time (tried 5 seconds and 12 seconds) between each call but it didn't seem to make a difference. However, I have not tried it again since doing the url open calls in the with statement. But I don't see that it would be too different. I haven't tried every combination of these things. I have tried putting another for loop using range 3 so it will try three times to complete the action. If one failed, it was never able to successfully complete on the second or third try either (whether start or stop). Again though, I haven't tried this in combination with the wait time or the new code posted above with the separated functions. What I notice is that it usually gets through at least the first three services when something fails. When one service in the list actually fails, it always fails on all remaining services in the list as well. This is the same as when I was trying three times as well. It also seems to usually be on start that it fails (rather than stop).
... View more
02-09-2015
03:39 PM
|
0
|
4
|
2523
|
|
POST
|
Forgive me, I haven't used model builder much so I may have the AddMessage() part wrong. Please feel free to fix as needed. I have only been running this code in PyScripter; not ArcMap or Model Builder. My suggestion here is to put the code into a try statement and attempt to display more helpful debug information. Primarily, I'd like to know which operation it failed on (since you don't sound too sure about the line 22 bit). import arcpy
import os
# Local variables modified to work in the model as a script
#fielddb = arcpy.GetParameterAsText(3) #in memory
panels = arcpy.GetParameterAsText (0)
supports = arcpy.GetParameterAsText (1)
template = arcpy.GetParameterAsText (2)
try:
# Get all SupportIDs for where clause IN() statement
supportid = tuple(i[0] for i in arcpy.da.SearchCursor(supports, "SupportID"))
# Optional truncate
# arcpy.TruncateTable_management(template)
# Retreive only panel records that have a support and write to output table
fields = [f.name for f in arcpy.ListFields(template)]
where_clause = "SupportID IN {}".format(supportid)
#arcpy.AddMessage(where_clause)
with arcpy.da.SearchCursor(panels, fields, where_clause) as s_cursor: #too few paramenters. Expected 1
with arcpy.da.InsertCursor(template, fields) as i_cursor:
for row in s_cursor:
i_cursor.insertRow(row)
except Exception as err:
if arcpy.GetMessages(2):
arcpy.AddMessage(arcpy.GetMessages())
else:
arcpy.AddMessage(err)
... View more
02-09-2015
12:48 PM
|
0
|
6
|
2018
|
|
POST
|
Thanks, Joshua. A little off topic: I am using Python with ArcGIS Server 10.2.2 to get tokens and stop/start services. To request a token, I use urlllib2 to open the request url and read the json response. To stop/start services, I use urllib to open the request url and read the JSON response. In both cases I thought it was necessary to do it in a with statement using contextlib to close it when finished. Here is my thread. Sorry for the hijack!
... View more
02-09-2015
12:31 PM
|
0
|
1
|
2687
|
|
POST
|
Can you post your modified code since you're not using that template table to get the fields?
... View more
02-09-2015
11:31 AM
|
0
|
8
|
2018
|
|
POST
|
Maybe try being explicit and set all of the z-indexes to what you think it should be to see if it makes a difference.
... View more
02-09-2015
10:48 AM
|
0
|
0
|
1503
|
|
POST
|
Is it necessary to close the url when getting JSON? python - closing files properly opened with urllib2.urlopen() - Stack Overflow
... View more
02-09-2015
10:43 AM
|
0
|
3
|
2687
|
|
POST
|
I'm not too experienced with Model Builder and I didn't know about this. Thanks!
... View more
02-09-2015
10:28 AM
|
0
|
3
|
5778
|
|
POST
|
I apologize Daniel Erklauer, I did not read your first post well and was going off what forest knutsen posted. I have not tried testing any of this yet, but you may have to use a conversion (like feature class to feature class) instead of copy features. Or better yet, maybe you could pass your whole list of shapefiles into feature class to getodatabase for a batch load.
... View more
02-09-2015
09:21 AM
|
0
|
0
|
729
|
|
POST
|
Could you use a z-index (in CSS or javascript) to order them so the infoWindow has a higher z-index value than the foldable widget? Sorry I'm not savvy enough to whip up some code, but it might be worth investigating.
... View more
02-09-2015
09:05 AM
|
1
|
3
|
1503
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 10-23-2025 03:53 PM | |
| 1 | 04-28-2026 07:25 AM | |
| 1 | 03-19-2026 08:59 AM | |
| 1 | 02-12-2026 01:37 PM | |
| 1 | 12-01-2025 06:19 AM |