Changeset 729 for trunk/src/utility.py

Show
Ignore:
Timestamp:
03/12/10 01:26:31 (2 years ago)
Author:
pierre
Message:

Add a nice progress bar to all the new connectors in connectors2.py. Now, a verbose flag allows to display or not a progress bar indicating the percentage of connections established. Can be useful to know how long connections will take. Some minors ameliorations. Everything is now OK, some informal tests shows numbers of synapses are ok for all the connectors. The last point to test is to make sure distances are correct. Should be the case, but should be sure....

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/utility.py

    r696 r729  
    182182            return "%d %s" % (int(val), units) + (val>1 and 's' or '') 
    183183        return ', '.join([add_units(T[part], part) for part in ('year', 'day', 'hour', 'minute', 'second') if T[part]>0]) 
     184 
     185 
     186class ProgressBar: 
     187    def __init__(self, min_value = 0, max_value = 100, width=77,**kwargs): 
     188        self.char = kwargs.get('char', '#') 
     189        self.mode = kwargs.get('mode', 'dynamic') # fixed or dynamic 
     190        if not self.mode in ['fixed', 'dynamic']: 
     191            self.mode = 'fixed' 
     192  
     193        self.bar = '' 
     194        self.min = min_value 
     195        self.max = max_value 
     196        self.span = max_value - min_value 
     197        self.width = width 
     198        self.amount = 0       # When amount == max, we are 100% done  
     199        self.update_amount(0)  
     200  
     201  
     202    def increment_amount(self, add_amount = 1): 
     203        """ 
     204        Increment self.amount by 'add_ammount' or default to incrementing 
     205        by 1, and then rebuild the bar string.  
     206        """ 
     207        new_amount = self.amount + add_amount 
     208        if new_amount < self.min: new_amount = self.min 
     209        if new_amount > self.max: new_amount = self.max 
     210        self.amount = new_amount 
     211        self.build_bar() 
     212  
     213  
     214    def update_amount(self, new_amount = None): 
     215        """ 
     216        Update self.amount with 'new_amount', and then rebuild the bar  
     217        string. 
     218        """ 
     219        if not new_amount: new_amount = self.amount 
     220        if new_amount < self.min: new_amount = self.min 
     221        if new_amount > self.max: new_amount = self.max 
     222        self.amount = new_amount 
     223        self.build_bar() 
     224  
     225  
     226    def build_bar(self): 
     227        """ 
     228        Figure new percent complete, and rebuild the bar string base on  
     229        self.amount. 
     230        """ 
     231        diff = float(self.amount - self.min) 
     232        percent_done = int(round((diff / float(self.span)) * 100.0)) 
     233  
     234        # figure the proper number of 'character' make up the bar  
     235        all_full = self.width - 2 
     236        num_hashes = int(round((percent_done * all_full) / 100)) 
     237  
     238        if self.mode == 'dynamic': 
     239            # build a progress bar with self.char (to create a dynamic bar 
     240            # where the percent string moves along with the bar progress. 
     241            self.bar = self.char * num_hashes 
     242        else: 
     243            # build a progress bar with self.char and spaces (to create a  
     244            # fixe bar (the percent string doesn't move) 
     245            self.bar = self.char * num_hashes + ' ' * (all_full-num_hashes) 
     246  
     247        percent_str = str(percent_done) + "%" 
     248        self.bar = '[ ' + self.bar + ' ] ' + percent_str 
     249  
     250  
     251    def __str__(self): 
     252        return str(self.bar) 
     253