Closing a file
Duncan Smith
buzzard at urubu.freeserve.co.uk
Fri Jul 21 14:36:38 EDT 2000
The following seems to do exactly what I want. It was suggested by a
computer scientist who knows no Python (but I presume C). Is 'sys.stdout =
0' a safe way of handling this?
def acceptsim(self, cost, n):
t = 10000.0 / n
if self.cost == 0:
outputfile = open('simresults.txt', 'w')
sys.stdout = outputfile
self.cost = DJT.totalcost(self)
self.low_cost = self.cost
self.low_ordering = self.ordering[:]
while n < 120000:
if cost > 0:
p = exp ((-1 / t) * (float(cost) / self.cost))
if random() > p:
print self.cost
return 0
else:
self.cost = self.cost + cost
print self.cost
return 1
else:
self.cost = self.cost + cost
if self.cost < self.low_cost:
self.low_cost = self.cost
self.low_ordering = self.ordering[:]
print self.cost
return 1
sys.stdout = 0
return 'stop'
More information about the Python-list
mailing list