Archive

Archives pour la catégorie ‘Informatique’

Déménagement de wordpress

01/06/2009 Augix 2 commentaires

Désolé, cet article est seulement disponible en English et 中文.

Categories: Informatique Tags:

Augix.com est retourné, c’est joli?

Augix.com is back with a new appearance, how about it?

View Results

Loading ... Loading ...
Categories: Informatique Tags:

(中文) Python编的进度条

25/05/2009 Augix 5 commentaires

我用Perl写程序有五年了,最近开始学习Python,编写了一个进度条程序,发觉Python很好用。

这个进度条程序的用法是:

from progressBar import progressBar
bar = progressBar(max=200)
for i in range(200):
         bar.update(i)
         time.sleep(0.1)

解释一下:
1. 进度条是用来指示用户运行的任务的进度的。
2. 输出在sys.stderr,根据terminal的窗口大小自动调整进度
条的长度,刚好占满terminal的一行。
3. bar.update来刷新进度条的样子。
4. if timeNow - self.timePrevious < 0.5: 用来判断前后两次刷新
的时间间隔是不是太小,如果太小,那就不刷新进度条
了,不然进度条会影响主程序的运行速度。我不知道还
有什么办法,不知道进度条可以不可以定时每间隔一秒
钟运行,让它去问主程序的运行进度,然后再打印进度
条?
5. animation = '|/-' 是希望打印出一个转动的齿轮一样的
东西,但是不好控制,到底是打印这四个字符中的哪一
个,我根据time.time()除四以后的余数来决定,不知道有
没有其他的好办法?

#!/usr/bin/env python
'''
    NAME: progressBar.py
    AUTHOR: Augix
    DATE: 2009-05-20

    FUNCTION: this module prints a progress bar to show how much time you have to wait until the program finish.
    It simply reads a percentage to update the progress bar.

    USAGE: from progressBar import progressBar
    bar = progressBar(min=0, max=200, barLength=None, output=sys.stderr)
    for i in range(200):
        bar.update(i)
        time.sleep(0.1)

    Reference: http://code.activestate.com/recipes/168639/
'''

# === Dependencies ===
import sys, time

# === variables ===

# === functions ===

# === classes ===
class progressBar:
    def __init__(self, min = 0, max = 100, barLength = None, output=sys.stderr):
        self.output = output
        self.progBar = "[]"   # This holds the progress bar string
        self.min = min
        self.max = max - 1
        self.span = self.max - self.min + 1
        self.barLength = self.calBarLength(barLength)
        self.percentDone = 0
        self.timeStart = time.time()
        self.timePrevious = self.timeStart
        self.timePass = 0
        self.finish = False
        self.update(0)  # Build progress bar string

    def calBarLength(self, barLength):
        if barLength == None:
            from fcntl import ioctl
            from termios import TIOCGWINSZ
            from array import array
            a = ioctl(self.output,TIOCGWINSZ,''*8)
            h,w=array('h',a)[:2]
            return w
        else: return barLength

    def calPercent(self, newAmount):
        diffFromMin = float(newAmount - self.min + 1)
        percentDone = (diffFromMin / float(self.span)) * 100.0
        return percentDone

    def calWaitingTime(self):
        self.timePass = time.time() - self.timeStart
        timeWait = self.timePass / float(self.percentDone) * (100.0 - self.percentDone)
        return timeWait

    def formatTime(self, seconds):
        seconds = int(round(seconds))
        hour = seconds / 3600
        if hour == 0: hour = ""
        else: hour = " " + str(hour) + " hour"
        minute = seconds % 3600 / 60
        if minute == 0: minute = ""
        else: minute = " " + str(minute) + " min"
        second = seconds % 60
        second = " " + str(second) + " sec"
        s = hour + minute + second
        return s

    def formatPercent(self, percent):
        i = str(int(round(percent)))
        s = " "*(3-len(i)) + i + "%"
        return s

    def update(self, newAmount = 0):
        timeNow = time.time()
        if timeNow - self.timePrevious < 0.5:
            if newAmount < self.max: return None
        self.timePrevious = timeNow
        # Calculate the percentage finished
        if newAmount < self.min: newAmount = self.min
        if newAmount >= self.max: newAmount = self.max; self.finish = True;
        self.percentDone = self.calPercent(newAmount)
        printPercent = self.formatPercent(self.percentDone)

        # Calculate the waiting time
        if self.percentDone == 0: printETA = ""
        elif self.finish == True: printETA = self.formatTime(self.timePass) + " used"
        else:
            timeWait = self.calWaitingTime()
            printETA = self.formatTime(timeWait) + " left"

        # Figure out how many '=' and spaces the bar should be
        spaceAndEqual = self.barLength - 11 - len(printETA)
        numEqual = (self.percentDone / 100.0) * spaceAndEqual
        numEqual = int(round(numEqual))

        # the rotating thing
        animation = '|/-'
        printAnimation = animation[int(timeNow % 4)]

        # build a progress bar with "=" and spaces
        self.progBar = " " + printPercent + " [" + printAnimation + '='*numEqual + ">" + ' '*(spaceAndEqual-numEqual) + "] " + printETA
        if self.finish == True:
            self.output.write(self.progBar + "n")
        else:
            self.output.write(self.progBar + "r")

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

# === Main ===
if __name__ == '__main__':
    bar = progressBar(max=200)
    for i in range(200):
        bar.update(i)
        time.sleep(0.1)
Categories: Informatique Tags:

(中文) 用Nally在Mac OS X下登入BBS

26/04/2009 Augix 2 commentaires

Désolé, cet article est seulement disponible en English et 中文.

Categories: Informatique Tags: ,

爱因斯坦评augix.com

17/11/2005 Augix 4 commentaires

31374

Look, what Uncle Sam said:

Uncle Sam

Categories: Uncategorized, Informatique Tags: