[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
Medium Priority
324 Views
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
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):
self.fb.append(s)
return struct.unpack(sig, s)

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)

return length[0]

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")
print "total triangles:",l
try:
while True:
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

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")
``````

thanks ayha
0
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
• 7
• 6

LVL 17

Expert Comment

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

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

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

ID: 39226398
0

LVL 17

Expert Comment

ID: 39226424
0

LVL 17

Expert Comment

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

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

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

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

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

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

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

Thanks
0

LVL 17

Accepted Solution

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
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):
self.fb.append(s)
return struct.unpack(sig, s)

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)

return length[0]

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")
print "total triangles:",l
try:
while True:
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

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)
``````

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

LVL 7

Author Closing Comment

ID: 39241488
Thanks
0

## Featured Post

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â€¦
###### Suggested Courses
Course of the Month13 days, 5 hours left to enroll