Seite 1 von 1

Restliche Zeit?

Verfasst: Mittwoch 26. August 2009, 18:12
von Sannes
Hallo,

Ich hab ein kleines Snippet im Internet gefunden das eine Fortschrittsanzeige ausgibt, funktioniert auch einwandfrei.

Aber ich hätte gerne das mir das Script noch ausrechnet wie lange es dauert bis die Anzeige durchgelaufen ist.

Leider weiß ich nicht mal Ansatzweise wie so etwas aussehen muss/könnte. Evtl. kann mir jemand ein paar Ratschläge geben wie man das realisieren könnte.

Code: Alles auswählen

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)

Verfasst: Mittwoch 26. August 2009, 19:03
von EyDu

Code: Alles auswählen

zeit/prozent*(100-prozent)
"zeit" ist die seit Beginn vergangene Zeit, "prozent" der aktuelle Fortschritt in Prozent.

Verfasst: Mittwoch 26. August 2009, 19:08
von lunar
Verwende doch einfach das progressbar-Modul:

Code: Alles auswählen

import time
from progressbar import ProgressBar, Bar, ETA, Percentage, FileTransferSpeed

widgets = [
        Percentage(), ' ', Bar(marker='#',left='[',right=']'),
        ' ', ETA(), ' ', FileTransferSpeed()
        ]

total = 100
progressbar = ProgressBar(widgets=widgets, maxval=total)
progressbar.start()
for i in xrange(total+1):
    progressbar.update(bytes_read)
    time.sleep(1)
progressbar.finish()

Verfasst: Mittwoch 26. August 2009, 19:21
von derdon
Ich verstehe dich nicht. Als ich ein Paket aus dem pypi vorschlug, kritisiertest du, dass dieses Paket unter der GPL veröffentlicht wird. Jetzt schlägst du selber ein Paket vor, dass GPL-lizensiert ist.

Verfasst: Mittwoch 26. August 2009, 19:27
von lunar
Beachte das L vor dem "GPL" :)

Übrigens sollte das keine Kritik darstellen, sondern nur eine einfache Anmerkung, denn schließlich kann nicht jeder GPL-Pakete verwenden. Wenn das anders rüberkam als von mir beabsichtigt, dann bitte ich darum, das zu entschuldigen :oops:

Verfasst: Mittwoch 26. August 2009, 19:34
von derdon
Ups, hab grad das L entdeckt. Jetzt gehe ich mich schämen und werde so rot wie du rosa bist: :oops:

Verfasst: Donnerstag 27. August 2009, 11:37
von Sannes
Super danke lunar, hab das modul via apt-get installiert. Funktioniert einwandfrei.

Die Lizenz interessiert mich herzhaft wenig, da es eh nur privat genutzt wird.