Show
Ignore:
Timestamp:
12/03/08 13:16:32 (20 months ago)
Author:
JensDiemer
Message:

VideoTools? update

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • CodeSnippets/VideoTools/shared/tools.py

    r1807 r1811  
    55 
    66 
    7 def human_filesize(bytes): 
     7class Humanize(object): 
    88    """ 
    9     >>> human_filesize(1*1024) 
     9    >>> Humanize().filesize(1023.9) 
     10    '1023.9bytes' 
     11    >>> Humanize().filesize(1*1024) 
    1012    '1.0KB' 
    11     >>> human_filesize(1*1024*1024) 
    12     '1.0MB' 
    13     >>> human_filesize(1.5*1024*1024) 
    14     '1.5MB' 
     13    >>> Humanize().filesize(1.9*1024*1024) 
     14    '1.9MB' 
     15    >>> Humanize().filesize(1*1024*1024*1024) 
     16    '1.0GB' 
     17    >>> Humanize().filesize(1*1024*1024*1024*1024*1024) 
     18    '1024.0TB' 
     19 
     20    >>> Humanize().time(10.1) 
     21    '10.1sec' 
     22    >>> Humanize().time(59.9) 
     23    '59.9sec' 
     24    >>> Humanize().time(60) 
     25    '1.0min' 
     26    >>> Humanize().time(60*60) 
     27    '1.0h' 
     28    >>> Humanize().time(60*60*60) 
     29    '60.0h' 
    1530    """ 
    16     bytes = float(bytes) 
    17     for unit in ['bytes','KB','MB','GB','TB']: 
    18         if bytes < 1000: 
    19             return "%s%s" % (round(bytes,1), unit) 
    20         bytes /= 1024.0 
     31    def _humanize(self, source, units, divisor): 
     32        temp = float(source) 
     33 
     34        for unit in units: 
     35            if temp < divisor: 
     36                final_value = temp 
     37                break 
     38            final_value = temp 
     39            temp /= divisor 
     40 
     41        return "%s%s" % (round(final_value,1), unit) 
    2142 
    2243 
     44    def filesize(self, bytes): 
     45        return self._humanize( 
     46            source = bytes, 
     47            units = ("bytes", "KB", "MB", "GB", "TB"), 
     48            divisor = 1024 
     49        ) 
     50 
     51    def time(self, sec): 
     52        return self._humanize( 
     53            source = sec, 
     54            units = ("sec", "min", "h"), 
     55            divisor = 60 
     56        ) 
     57 
     58human_filesize = Humanize().filesize 
     59human_time = Humanize().time 
    2360 
    2461 
     
    2865    """ 
    2966    delete all non-ALLOW_CHARS characters 
    30      
     67 
    3168    >>> make_slug("a test") 
    3269    'a test' 
     
    4077        if char not in allow_chars: 
    4178            if parts[-1] != "": 
    42                 # No double "-" e.g.: "foo - bar" -> "foo-bar" not "foo---bar"    
     79                # No double "-" e.g.: "foo - bar" -> "foo-bar" not "foo---bar" 
    4380                parts.append("") 
    4481        else: 
     
    4784    item_name = join_string.join(parts) 
    4885    item_name = item_name.strip(join_string) 
    49      
     86 
    5087    return item_name 
    5188 
     
    5794    - delete all non-ALLOW_CHARS characters. 
    5895    - if the shotcut already exists in name_list -> add a sequential number 
    59      
     96 
    6097    >>> makeUnique("two", ["one", "two", "three"]) 
    6198    'two1' 
     
    107144        if char in ("\r", "\x08"): 
    108145            continue 
    109          
     146 
    110147        if char == "\n": 
    111148            char_count = 0 
     
    117154        if char_count>79: 
    118155            sys.stdout.write("\n") 
    119             char_count = 0             
     156            char_count = 0 
    120157        sys.stdout.flush() 
    121158