updated on Thu Jan 26 16:09:46 UTC 2012
[aur-mirror.git] / torrent-leecher / DataUnits.py
blob8551baaab5c76f415423705e33c1eaed862f31cc
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
4 #Classes
5 ###################################################################################
6 class DataUnits:
7 def __init__(self, Parent):
8 self.Parent = Parent
9 #Data units
10 self.dataUnits = [
11 ("TB", "Terabyte", 8796100000000.0),
12 #("Tb", "Terabit", 1099511627776.0),
13 ("GB", "Gigabyte", 8589600000.0),
14 #("Gb", "Gigabit", 1073700000.0),
15 ("MB", "Megabyte", 8388600.0),
16 #("Mb", "Megabit", 1048600.0),
17 ("kB", "Kilobyte", 8192.0),
18 #("kb", "Kilobit", 1024.0),
19 ("B", "Byte", 8.0),
20 #("b", "Bit", 1.0),
23 def convert(self, amount, unit=False):
24 if 0 > amount:
25 amount = 0-amount
26 minus = "-"
27 else:
28 minus = ""
29 amount = int(amount*8)
30 #If we have correct unit selected
31 for i, line in enumerate(self.dataUnits):
32 if line[0] == unit:
33 output = amount/line[2]
34 output = "%.2f" % output
35 return minus+""+str(output)+""+str(line[0])
36 #Select unit automaticly
37 if 1 > amount:
38 return "0.00B"
39 else:
40 for i, line in enumerate(self.dataUnits):
41 if amount >= line[2]/10.240407004:
42 output = amount/line[2]
43 output = "%.2f" % output
44 return minus+""+str(output)+""+str(line[0])