Solved

syntax error

Posted on 2013-06-06
13
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 17

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 17

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
Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

 
LVL 7

Author Comment

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

Expert Comment

by:gelonida
ID: 39226424
0
 
LVL 17

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 17

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 17

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 17

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
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 modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

726 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