[Tutor] print to file probelm
John Collins
john at netcore.com.au
Mon Aug 15 21:27:42 CEST 2011
Hi,
I'm a baffled beginner. The script below is one of a suite of scripts
written by Simon Tatham a decade ago;
http://www.chiark.greenend.org.uk/~sgtatham/polyhedra/
http://www.chiark.greenend.org.uk/~sgtatham/polyhedra/polyhedra.tar.gz
OS: WinXP
Py Ver: 2.7
Script: canvas.py
It reads from a file name in the command line and should print to a file
name also specified in the command line. It inputs and runs fine but
outputs to the command prompt *screen* while creating and *empty* output
file of the name specified? I cannot figure out why?
[the dashed lines below are not in the script(s)]
-------------------------------------------------------------------
#!/usr/bin/env python
# Read in a polyhedron description, and output a JavaScript list
# suitable for use in the "data" field of a canvas used by
# canvaspoly.js.
import sys
import os
import string
import random
from math import pi, asin, atan2, cos, sin, sqrt
from crosspoint import crosspoint
args = sys.argv[1:]
if len(args) > 0:
infile = open(args[0], "r")
args = args[1:]
else:
infile = sys.stdin
if len(args) > 0:
outfile = open(args[0], "w")
args = args[1:]
else:
outfile = sys.stdout
vertices = {}
faces = {}
normals = {}
lineno = 0
while 1:
s = infile.readline()
if s == "": break
sl = string.split(s)
lineno = lineno + 1
if sl[0] == "point" and len(sl) == 5:
vertices[sl[1]] = \
(string.atof(sl[2]), string.atof(sl[3]), string.atof(sl[4]))
elif sl[0] == "face" and len(sl) == 3:
if not vertices.has_key(sl[2]):
sys.stderr.write("line %d: vertex %s not defined\n" % \
(lineno, sl[2]))
else:
if not faces.has_key(sl[1]):
faces[sl[1]] = []
faces[sl[1]].append(sl[2])
elif sl[0] == "normal" and len(sl) == 5:
if not faces.has_key(sl[1]):
sys.stderr.write("line %d: face %s not defined\n" % \
(lineno, sl[1]))
else:
normals[sl[1]] = \
(string.atof(sl[2]), string.atof(sl[3]), string.atof(sl[4]))
else:
sys.stderr.write("line %d: unrecognised line format\n" % lineno)
continue
infile.close()
# Normalise the radius of our polyhedron so that it just fits
# inside the unit sphere.
maxlen = 0
for v in vertices.values():
vlen = sqrt(v[0]**2 + v[1]**2 + v[2]**2)
if maxlen < vlen: maxlen = vlen
for v in vertices.keys():
vv = vertices[v]
vertices[v] = (vv[0]/maxlen, vv[1]/maxlen, vv[2]/maxlen)
# Assign integer indices to vertices and faces.
vindex = {}
findex = {}
vlist = []
flist = []
for v in vertices.keys():
vindex[v] = len(vlist)
vlist.append(v)
for f in faces.keys():
findex[f] = len(flist)
flist.append(f)
s = "[%d" % len(vertices)
for i in range(len(vertices)):
v = vertices[vlist[i]]
s = s + ",%.17g,%.17g,%.17g" % (v[0], v[1], v[2])
s = s + ",%d" % len(faces)
for i in range(len(faces)):
f = faces[flist[i]]
n = normals[flist[i]]
s = s + ",%d" % len(f)
for v in f:
s = s + ",%d" % vindex[v]
s = s + ",%.17g,%.17g,%.17g" % (n[0], n[1], n[2])
s = s + "]"
print s
------------------------------------------------------------------
The other scripts run and output just fine. They do have some extra
script of varying parameters that seem to be about reassigning the print
command, possible with formatting of output in mind? Here are a those
sections from two of the other scripts:
From mkpoints.py
--------------------------------------------
def realprint(a):
for i in range(len(a)):
outfile.write(str(a[i]))
if i < len(a)-1:
outfile.write(" ")
else:
outfile.write("\n")
def pprint(*a):
realprint(a)
----------------------------------------------
the final output lines are
----------------------------------------------
# Output the points.
for x, y, z in points:
pprint(x, y, z)
----------------------------------------------
From drawpoly.py
----------------------------------------------
def realprint(a):
for i in range(len(a)):
outfile.write(str(a[i]))
if i < len(a)-1:
outfile.write(" ")
else:
outfile.write("\n")
def psprint(*a):
realprint(a)
----------------------------------------------
the last two output lines are
----------------------------------------------
psprint("showpage grestore")
psprint("%%EOF")
----------------------------------------------
For the problem script, canvas.py, the output is a long single line
beginning with a "[", containing a mixture of integers and signed real
numbers separated only by a commas, and ending in a "]".
Any help to begin my understanding of print (re)assignments, and, fixing
this script would be MOST welcome, thanks to you all!
Best Regards,
John. [Total newbie, time since first Python DL, <24hrs! Still use
GWBasic!!! ... Really need to learn Python!]
More information about the Tutor
mailing list