[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

syntax error

Posted on 2013-06-06
13
Medium Priority
?
324 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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 1000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
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 while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

650 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