Solved

syntax error

Posted on 2013-06-06
13
316 Views
Last Modified: 2013-06-12
Hi,

I am trying run the follwoing python code but getting a syntax error. Please help to solve.

'''

import struct
import sys

class STLUtils:
	def resetVariables(self):
		self.normals = []
		self.points = []
		self.triangles = []
		self.bytecount = []
		self.fb = [] # debug list
		
	# Calculate volume fo the 3D mesh using Tetrahedron volume
	# based in: http://stackoverflow.com/questions/1406029/how-to-calculate-the-volume-of-a-3d-mesh-object-the-surface-of-which-is-made-up
	def signedVolumeOfTriangle(self,p1, p2, p3):
		v321 = p3[0]*p2[1]*p1[2]
		v231 = p2[0]*p3[1]*p1[2]
		v312 = p3[0]*p1[1]*p2[2]
		v132 = p1[0]*p3[1]*p2[2]
		v213 = p2[0]*p1[1]*p3[2]
		v123 = p1[0]*p2[1]*p3[2]
		return (1.0/6.0)*(-v321 + v231 + v312 - v132 - v213 + v123)

	def unpack(self, sig, l):
		s = self.f.read(l)
		self.fb.append(s)
		return struct.unpack(sig, s)

	def read_triangle(self):
		n  = self.unpack("<3f", 12)
		p1 = self.unpack("<3f", 12)
		p2 = self.unpack("<3f", 12)
		p3 = self.unpack("<3f", 12)
		b  = self.unpack("<h", 2)
		
		self.normals.append(n)
		l = len(self.points)
		self.points.append(p1)
		self.points.append(p2)
		self.points.append(p3)
		self.triangles.append((l, l+1, l+2))
		self.bytecount.append(b[0])
		return self.signedVolumeOfTriangle(p1,p2,p3)

	def read_length(self):
   		length = struct.unpack("@i", self.f.read(4))
   		return length[0]

	def read_header(self):
		self.f.seek(self.f.tell()+80)
		
	def cm3_To_inch3Transform(self, v):
		return v*0.0610237441
		
	def calculateWeight(self,volumeIn_cm):
		return volumeIn_cm*1.04
	
	def calculateVolume(self,infilename, unit):
		print "C:\Users\sag\Desktop\gf.stl"
		self.resetVariables()
		totalVolume = 0
		try:
			self.f = open( infilename, "rb")
			self.read_header()
			l = self.read_length()
			print "total triangles:",l
			try:
				while True:
					totalVolume +=self.read_triangle()
			except Exception, e:
				#print e
				print "End calculate triangles volume"
			#print len(self.normals), len(self.points), len(self.triangles), l, 
			if unit=="cm":
				totalVolume = (totalVolume/1000)
				print "Total volume:", totalVolume,"cm"
			else:
				totalVolume = self.cm3_To_inch3Transform(totalVolume/1000)
				print "Total volume:", totalVolume,"inch"
		except Exception, e:
			print e
		return totalVolume

if __name__ == '__main__':
	if len(sys.argv)==1:
		print "C:\Users\sag\Desktop\gf.stl"
	else:
		mySTLUtils = STLUtils()
		if(len(sys.argv)>2 and sys.argv[2]=="inch"):
			mySTLUtils.calculateVolume(sys.argv[1],"inch")
		else:
			mySTLUtils.calculateVolume(sys.argv[1],"cm")

Open in new window


thanks ayha
0
Comment
Question by:ayha1999
  • 7
  • 6
13 Comments
 
LVL 16

Expert Comment

by:gelonida
ID: 39226309
Do you use python3?
This code is intended for Python2

In python3 for example you have to change for example
print "C:\Users\sag\Desktop\gf.stl"
to
print("C:\Users\sag\Desktop\gf.stl")

I don't use python3 but you if I remember corerctly might also have to change

class STLUtils:

to
class STLUtils(object):


Could you please also post the exact error message?
0
 
LVL 7

Author Comment

by:ayha1999
ID: 39226332
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32

syntax error at

print "C:\Users\sag\Desktop\gf.stl"
0
 
LVL 16

Expert Comment

by:gelonida
ID: 39226389
try to apply the changes, that I suggested.
I don't have python3 at hand at the moment, so can't try.

But as I suggested before.
The main issues is you try to run Python2 code with Python3.

Either you try to run it with Python2
or you fix all the Python3 incompatibilities as for example
adding parenthesises to all print statements.

Also change line 6 to:
class STLUtils(object):

You might also encount other errors just post them if you'd like to stay with Python3

Have also a look at
http://wiki.python.org/moin/Python2orPython3
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 7

Author Comment

by:ayha1999
ID: 39226398
where can I download python 2?
0
 
LVL 16

Expert Comment

by:gelonida
ID: 39226424
0
 
LVL 16

Expert Comment

by:gelonida
ID: 39226433
Python 27 and python3 can co-exist on the same machine however there are some things which might become a little tricky.

I don't have two python versions on a windows machine, so can't really advicse on this.
0
 
LVL 7

Author Comment

by:ayha1999
ID: 39226941
OK. I installed 2.7 ver and no more error. How can I display the total volume?

thanks
0
 
LVL 16

Expert Comment

by:gelonida
ID: 39227050
I'm not sure.
Normally the volume should be printed on the console.

What I assume is, that your program starts, prints the result and closes the console window immediately.

what you can do is following:

add following line somehwere at the beginning for example aftyer line 4
import time


add following line at the end of your script
        time.sleep(5)


this should give you 5 seconds to read the result

Otherwise you should be able to run your script from one of the IDE's or from a cmd window, but
I don't know your windows setup that well, thus suggested just a simple work around.
0
 
LVL 7

Author Comment

by:ayha1999
ID: 39227960
I am running from IDE. how can I display the thevolume in the following line?


if __name__ == '__main__':
      if len(sys.argv)==1:
            print "test"    <------------------------------------------print thevolume here.



thevolume is returned in "def calculateVolume(self,infilename, unit)"

thanks
0
 
LVL 16

Expert Comment

by:gelonida
ID: 39228172
Your script  prints the result either in
line 77 or in line 80 whenever the function is called,

but only if you passed an argument to your script.
it seems, that you start the script without passing a command line  argument.

What you hav to o is change the logic such, that the script without argument uses for example 'cms' as default value

You can do this by removing/commenting out lines 86 to 88
0
 
LVL 7

Author Comment

by:ayha1999
ID: 39228500
could you please modify the code to print thevolume. I have no experience in python all.

Thanks
0
 
LVL 16

Accepted Solution

by:
gelonida earned 250 total points
ID: 39231795
'''

import struct
import sys

class STLUtils:
	def resetVariables(self):
		self.normals = []
		self.points = []
		self.triangles = []
		self.bytecount = []
		self.fb = [] # debug list
		
	# Calculate volume fo the 3D mesh using Tetrahedron volume
	# based in: http://stackoverflow.com/questions/1406029/how-to-calculate-the-volume-of-a-3d-mesh-object-the-surface-of-which-is-made-up
	def signedVolumeOfTriangle(self,p1, p2, p3):
		v321 = p3[0]*p2[1]*p1[2]
		v231 = p2[0]*p3[1]*p1[2]
		v312 = p3[0]*p1[1]*p2[2]
		v132 = p1[0]*p3[1]*p2[2]
		v213 = p2[0]*p1[1]*p3[2]
		v123 = p1[0]*p2[1]*p3[2]
		return (1.0/6.0)*(-v321 + v231 + v312 - v132 - v213 + v123)

	def unpack(self, sig, l):
		s = self.f.read(l)
		self.fb.append(s)
		return struct.unpack(sig, s)

	def read_triangle(self):
		n  = self.unpack("<3f", 12)
		p1 = self.unpack("<3f", 12)
		p2 = self.unpack("<3f", 12)
		p3 = self.unpack("<3f", 12)
		b  = self.unpack("<h", 2)
		
		self.normals.append(n)
		l = len(self.points)
		self.points.append(p1)
		self.points.append(p2)
		self.points.append(p3)
		self.triangles.append((l, l+1, l+2))
		self.bytecount.append(b[0])
		return self.signedVolumeOfTriangle(p1,p2,p3)

	def read_length(self):
   		length = struct.unpack("@i", self.f.read(4))
   		return length[0]

	def read_header(self):
		self.f.seek(self.f.tell()+80)
		
	def cm3_To_inch3Transform(self, v):
		return v*0.0610237441
		
	def calculateWeight(self,volumeIn_cm):
		return volumeIn_cm*1.04
	
	def calculateVolume(self,infilename, unit):
		print "C:\Users\sag\Desktop\gf.stl"
		self.resetVariables()
		totalVolume = 0
		try:
			self.f = open( infilename, "rb")
			self.read_header()
			l = self.read_length()
			print "total triangles:",l
			try:
				while True:
					totalVolume +=self.read_triangle()
			except Exception, e:
				#print e
				print "End calculate triangles volume"
			#print len(self.normals), len(self.points), len(self.triangles), l, 
			if unit=="cm":
				totalVolume = (totalVolume/1000)
				print "Total volume:", totalVolume,"cm"
			else:
				totalVolume = self.cm3_To_inch3Transform(totalVolume/1000)
				print "Total volume:", totalVolume,"inch"
		except Exception, e:
			print e
		return totalVolume

if __name__ == '__main__':
        infilename = "C:\Users\sag\Desktop\gf.stl
        unit = "cm"
        if len(sys.argv) > 1:
                infilename = sys.argv[1]
        if len(sys.argv) > 2:
                unit = sys.argv[2]
        mySTLUtils = STLUtils()
        mySTLUtils.calculateVolume(infilename, unit)

Open in new window


This changed version will calculate the volume of C:\Users\sag\Desktop\gf.stl
in cm
0
 
LVL 7

Author Closing Comment

by:ayha1999
ID: 39241488
Thanks
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question