Changeset 1811 for CodeSnippets/VideoTools/shared/tools.py
- Timestamp:
- 12/03/08 13:16:32 (20 months ago)
- Files:
-
- 1 modified
-
CodeSnippets/VideoTools/shared/tools.py (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
CodeSnippets/VideoTools/shared/tools.py
r1807 r1811 5 5 6 6 7 def human_filesize(bytes):7 class Humanize(object): 8 8 """ 9 >>> human_filesize(1*1024) 9 >>> Humanize().filesize(1023.9) 10 '1023.9bytes' 11 >>> Humanize().filesize(1*1024) 10 12 '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' 15 30 """ 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) 21 42 22 43 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 58 human_filesize = Humanize().filesize 59 human_time = Humanize().time 23 60 24 61 … … 28 65 """ 29 66 delete all non-ALLOW_CHARS characters 30 67 31 68 >>> make_slug("a test") 32 69 'a test' … … 40 77 if char not in allow_chars: 41 78 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" 43 80 parts.append("") 44 81 else: … … 47 84 item_name = join_string.join(parts) 48 85 item_name = item_name.strip(join_string) 49 86 50 87 return item_name 51 88 … … 57 94 - delete all non-ALLOW_CHARS characters. 58 95 - if the shotcut already exists in name_list -> add a sequential number 59 96 60 97 >>> makeUnique("two", ["one", "two", "three"]) 61 98 'two1' … … 107 144 if char in ("\r", "\x08"): 108 145 continue 109 146 110 147 if char == "\n": 111 148 char_count = 0 … … 117 154 if char_count>79: 118 155 sys.stdout.write("\n") 119 char_count = 0 156 char_count = 0 120 157 sys.stdout.flush() 121 158