This might help. Never used it though.

Johan


Christian Wyglendowski wrote:

> -----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Oliver Maunder
Sent: Wednesday, October 05, 2005 1:13 PM
To: tutor@python.org
Subject: [Tutor] Console output

Does anyone know how I can update a line of console output without creating a new line?

Hi Oliver,

If you are on a *nix system, check out the "curses" package in the
standard library.  I have never used it, but it provides ways to
accomplish what you are after.

HTH,

Christian
http://www.dowski.com
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

class progressBar:
	def __init__(self, minValue = 0, maxValue = 10, totalWidth=12):
		self.progBar = "[]"   # This holds the progress bar string
		self.min = minValue
		self.max = maxValue
		self.span = maxValue - minValue
		self.width = totalWidth
		self.amount = 0       # When amount == max, we are 100% done 
		self.updateAmount(0)  # Build progress bar string

	def updateAmount(self, newAmount = 0):
		if newAmount < self.min: newAmount = self.min
		if newAmount > self.max: newAmount = self.max
		self.amount = newAmount

		# Figure out the new percent done, round to an integer
		diffFromMin = float(self.amount - self.min)
		percentDone = (diffFromMin / float(self.span)) * 100.0
		percentDone = round(percentDone)
		percentDone = int(percentDone)

		# Figure out how many hash bars the percentage should be
		allFull = self.width - 2
		numHashes = (percentDone / 100.0) * allFull
		numHashes = int(round(numHashes))

		# build a progress bar with hashes and spaces
		self.progBar = "[" + '#'*numHashes + ' '*(allFull-numHashes) + "]"

		# figure out where to put the percentage, roughly centered
		percentPlace = (len(self.progBar) / 2) - len(str(percentDone)) 
		percentString = str(percentDone) + "%"

		# slice the percentage into the bar
		self.progBar = self.progBar[0:percentPlace] + percentString + 
self.progBar[percentPlace+len(percentString):]

	def __str__(self):
		return str(self.progBar)

==================================================

import time
prog = progressBar(0, 100, 77)
for i in xrange(101):
    prog.updateAmount(i)
    print prog, "\r",
    time.sleep(.05)

==================================================

# Best not to print it every iteration, Terry Carroll, 2004/01/26
#  It can be a substantial performance hit to print the bar every iteration, whether it changes or not. At 100 iterations, like Josiah's' example, it changes every iteration, so it won't' matter. But if you have more than a few thousand, it's' worth checking and not printing an unchanged bar: 
limit = 1000000

prog = progressBar(0, limit, 77)
oldprog = str(prog)
for i in xrange(limit+1):
    prog.updateAmount(i)
    if oldprog != str(prog):
        print prog, "\r",
        oldprog=str(prog)
print
=================================================

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to