Python Enumerate Question

Hello,

I wanted know if it looks like my python is right below?  I know that the non-python stuff is probably not known to most of you.  I have a question mostly about my use of the enumerate function.  I am hoping that this line:

gp.interpolatepolytopatch_3d (tin, InPolygon + ("_%03d" % numbers[tin_index]) +".shp", OutPolygon + ("_%03d" % numbers[tin_index]) +".shp")

Reads as: gp.interpolatepolytopatch_3D(C:/giswork/DEMS/dem2tin/tin_001, C:/temp/raster2polygon01/LC2001_001, C:/temp/3Dpoly2patch2001/3D_area_2001_001)"
then:
gp.interpolatepolytopatch_3D(C:/giswork/DEMS/dem2tin/tin_002, C:/temp/raster2polygon01/LC2001_002, C:/temp/3Dpoly2patch2001/3D_area_2001_002)"
then ...
gp.interpolatepolytopatch_3D(C:/giswork/DEMS/dem2tin/tin_100, C:/temp/raster2polygon01/LC2001_100, C:/temp/3Dpoly2patch2001/3D_area_2001_100)"

Will it? Can I use the enumerate function twice in the same line like I did and expect it to give me the same value for those two instances?  I am not very familiar with the function.

Thanks,
JE
justearthAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

clockwatcherCommented:
The line that you posted doesn't include the use of the enumerate function.  Can you show us where you're using enumerate?  

And this:

  gp.interpolatepolytopatch_3d (tin, InPolygon + ("_%03d" % numbers[tin_index]) +".shp", OutPolygon + ("_%03d" % numbers[tin_index]) +".shp")

would probably be much more readable as:

  gp.interpolatepolytopatch_3d (tin, "%s_%03d.shp" % (InPolygon, numbers[tin_index]), "%s_%03d.shp" % (OutPolygon,numbers[tin_index]))
0
mish33Commented:
1st of all use intermediate var to save some typing and computations:

shp = "_%03d.shp" % numbers[tin_index]

gp.interpolatepolytopatch_3d (tin, InPolygon + shp, OutPolygon + shp)

It will produce

gp.interpolatepolytopatch_3D("C:/giswork/DEMS/dem2tin/tin_001"
, "C:/temp/raster2polygon01/LC2001_001.shp", "C:/temp/3Dpoly2patch2001/3D_area_2001_001.shp")

iff
tin == "C:/giswork/DEMS/dem2tin/tin_001"
InPolygon == "C:/temp/raster2polygon01/LC2001"
OutPolygon == "C:/temp/3Dpoly2patch2001/3D_area_2001"
and tin_index == 0 (assuming 1st number is 1.

And will work perfectly for increasing tin_index.

Also when you unsure, use print "whatever you want to test"
0
peprCommented:
... plus the .shp extension.  Yes, something like that.  Only, if the filenames were written explicitly, they would be enclosed in single or double quotes. You can use the formatting string and the formated values as many time as you wish.

You can also put the extension into the formatting string.  And you can even extend the formating string with the mark for the part in front of the numbered part.  Also, if the two strings have the same structure, you can define a formatting string template to make it more brief, more readable, and modifiable on a single place.

tpl = '%s_%03d.shp'     # just as normal string

Here it contains two marks -- the first one for a string, the second one for a number.  Then you interpolate the format using a tuple with two values instead of a single value.  This way, you can write...

gp.interpolatepolytopatch_3d (tin, tpl % (InPolygon, numbers[tin_index]), tpl % (OutPolygon, numbers[tin_index]))

I personally would use one more variable to make it even more readable and managed on a single place. The result would be like:
tpl = '%s_%03d.shp'     # just as normal string
    n = numbers[tin_index]  
    gp.interpolatepolytopatch_3d(tin, tpl % (InPolygon, n), tpl % (OutPolygon, n))

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

peprCommented:
(Grrr. It ate the indentation.  All the lines must be indented the same way.  And do not ask me why the InPolygon and OutPolygon are displayed in purple colous and the n and tpl not ;)
0
justearthAuthor Commented:
Hello,

Thank you all for the help.  To confirm: my tin variable is being called from a directory in order (i.e. tin_001, tin_002, etc).  Then the other variable are using the tin_index.  Assuming the tin_index and the tin directory are numbered the same (i.e. tin_001 wil match with tin_index 001) then they should advance together right?

e.g.
gp.interpolatepolytopatch_3D(C:/giswork/DEMS/dem2tin/tin_001,  C:/temp/raster2polygon01/LC2001_001,  C:/temp/3Dpoly2patch2001/3D_area_2001_001)"
then:
gp.interpolatepolytopatch_3D(C:/giswork/DEMS/dem2tin/tin_002,  C:/temp/raster2polygon01/LC2001_002,  C:/temp/3Dpoly2patch2001/3D_area_2001_002)"
then ...
gp.interpolatepolytopatch_3D(C:/giswork/DEMS/dem2tin/tin_100,  C:/temp/raster2polygon01/LC2001_100,  C:/temp/3Dpoly2patch2001/3D_area_2001_100)"


Thanks again,
Cheers,
JE
0
peprCommented:
For tin and tin_index... Well, I do not know the intention.  However, if this is the case, you could think about passing only the tin variable and extracting the number from it.  Better to say, it is not a number but a string that can be converted to a number.  Then you are converting the number back to string...

If you are sure that the suffix of the tin path ends with 3 numerals, you can avoid using numbers[tin_index] and do it this way (not checked syntactically, you should try yourself):
# Say, the tin variable contains 'C:/giswork/DEMS/dem2tin/tin_001'. Just check whether
    # the tin contains the suffix that we expect.
    assert tin[-4] == '_'
    assert tin[-3:].isdigit()

    # Get the numbered suffix from the tin variable.
    nsuffix = tin[-4:]

    # Prepare the template of the generated paths.
    tpl = '%s%s.shp'        # name without extension, numbered suffix, explicit extension

    # Now, put the information together.
    gp.interpolatepolytopatch_3d(tin, tpl % (InPolygon, nsuffix), tpl % (OutPolygon, nsuffix))

Open in new window

0
justearthAuthor Commented:
pepr:
>you could think about passing only the tin variable
This sounds great pepr.  I hope this can be achieved.  
 You helped me with a similar idea in a script a while ago. Using  'assert' and 'suffix'
see: http://www.experts-exchange.com/Q_23580838.html  I do not have a complete understanding of the assert module.

So I tried the code below.  When I execute it, I get nothing. No errors, no output.  Does it look like a python error, or the other software?  

Thanks again for the continued support,
JE

# Purpose: Create a multipatch simulating a portion of a TIN surface.

# Create the Geoprocessor object
import arcgisscripting
gp = arcgisscripting.create(9.3)

#Check out the 3D Analyst extension
gp.CheckOutExtension ("3D")

# Set the workspace (to avoid having to type in the full path to the data every time)
gp.workspace = "C:/giswork/DEMS/dem2tin"
#InTin = gp.workspace
InTin = gp.ListRasters()
InPolygon = "C:/temp/raster2polygon01/LC2001_"
OutPolygon = "C:/temp/3Dpoly2patch2001/3D_area_2001_"
	
tincount = 0
# Select the 3D Analyst Toolbox
gp.toolbox = "3D"

# Say, the tin variable contains 'C:/giswork/DEMS/dem2tin/tin_001'. Just check whether
# the tin contains the suffix that we expect.
for tin in InTin:
	assert tin[-4] == '_'
	assert tin[-3:].isdigit()
	# Get the numbered suffix from the tin variable.
	nsuffix = tin[-4:]
	print nsuffix
	# Prepare the template of the generated paths.
	tpl = '%s%s.shp'
	print tpl
	# name without extension, numbered suffix, explicit extension
	# Now, put the information together.
	gp.interpolatepolytopatch_3d(tin, tpl % (InPolygon, nsuffix), tpl % (OutPolygon, nsuffix))

# Print error message if an error occurs
print gp.GetMessages()

Open in new window

0
peprCommented:
The assert is a "safe net" command.  It is usual also in other programming languages.  In debug mode (which is used by Python by default) the command just does nothing if the condition holds.  It stops loudly when it does not hold (the AssertionError exception).  See http://docs.python.org/reference/simple_stmts.html#the-assert-statement for details.

In your case, the only possible explanation is that the for loop body was never executed.  This means that your InTin contains nothing or something very similar :)
0
justearthAuthor Commented:
Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.