Solved

syntax error

Posted on 2013-06-06
13
313 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

"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 …
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now