def test_and(self): """Bits and'd (&) are correct""" and_fifteen_hundred = self.leet & 1500 self.assertEqual(and_fifteen_hundred, bitmath.Bit(1304)) and_orig = self.leet & 1337 self.assertEqual(and_orig, bitmath.Bit(1337)) and_thousand = self.leet & 1000 self.assertEqual(and_thousand, bitmath.Bit(296)) and_five_hundred = self.leet & 500 self.assertEqual(and_five_hundred, bitmath.Bit(304))
def convert_to_mbs(self, number_of_bytes): ''' Return Megabit per second from byte per second ''' try: bit = bitmath.Bit(number_of_bytes) return bit.to_Mib() except: return None
def test_adding_with_different_base_units(self): """Adding a bit based type with a byte based type""" kib_sized_bit_from_bytes = bitmath.Bit(self.kib_in_bits) kib = bitmath.KiB(1) added = kib_sized_bit_from_bytes + kib two_kib = bitmath.KiB(2) self.assertEqual(added, two_kib)
def test_subtracting_with_different_base_units(self): """Subtracting a bit based type with a byte based type""" kib_sized_bit_from_bytes = bitmath.Bit(self.kib_in_bits) kib = bitmath.KiB(1) subtracted = kib_sized_bit_from_bytes - kib zero_kib = bitmath.KiB(0) self.assertEqual(subtracted, zero_kib)
def test_best_prefix_negative_less_than_a_byte(self): """best_prefix_base: negative values less than a byte stay as bits""" # assert that a Byte of -4 bits yields Bit(-4) bm1 = bitmath.Byte(bits=-4) expected = bitmath.Bit(-4) res = bitmath.best_prefix(bm1) # Verify that best prefix math works for negative numbers self.assertEqual(res, expected) # Verify that best prefix guessed the correct type self.assertIs(type(res), bitmath.Bit)
def setUp(self): self.bit = bitmath.Bit(1) self.byte = bitmath.Byte(1) # NIST units self.kib = bitmath.KiB(1) self.mib = bitmath.MiB(1) self.gib = bitmath.GiB(1) self.tib = bitmath.TiB(1) self.pib = bitmath.PiB(1) self.eib = bitmath.EiB(1) # SI units self.kb = bitmath.kB(1) self.mb = bitmath.MB(1) self.gb = bitmath.GB(1) self.tb = bitmath.TB(1) self.pb = bitmath.PB(1) self.eb = bitmath.EB(1)
def bits(data): # By default, `best_prefix` returns a value in byte format, this hack (multiply by 8.0) # will convert it into bit format. bit = 8.0 * bitmath.Bit(float(data)) bit = bit.best_prefix(bitmath.SI) byte_to_bit_classes = { 'kB': bitmath.kb, 'MB': bitmath.Mb, 'GB': bitmath.Gb, 'TB': bitmath.Tb, 'PB': bitmath.Pb, 'EB': bitmath.Eb, 'ZB': bitmath.Zb, 'YB': bitmath.Yb, } bps = byte_to_bit_classes.get(bit.unit, bitmath.Bit).from_other(bit) / 8.0 if bps.unit != 'Bit': return bps.format("{value:.4f} {unit}ps") return bps.format("{value:.4f} bps")
def setUp(self): self.leet = bitmath.Bit(1337)
def test_parse_b(self): """parse_string works on bit strings""" self.assertEqual( bitmath.parse_string("123b"), bitmath.Bit(123))
def test_xor(self): """Bits xor'd (^) are correct""" xor_thousand = self.leet ^ 1000 self.assertEqual(xor_thousand, bitmath.Bit(1745)) xor_five_hundred = self.leet ^ 500 self.assertEqual(xor_five_hundred, bitmath.Bit(1229))
def test_or(self): """Bits or'd (|) are correct""" or_thousand = self.leet | 1000 self.assertEqual(or_thousand, bitmath.Bit(2041)) or_five_hundred = self.leet | 500 self.assertEqual(or_five_hundred, bitmath.Bit(1533))
def test_bit_round_up(self): """BASE: 2 Bytes (as a Bit()) round up into a Byte()""" # Two bytes is 16 bits two_bytes = bitmath.Bit(bytes=2) # Bit(16) should round up into Byte(2) self.assertIs(type(two_bytes.best_prefix()), bitmath.Byte)
def test_right_shift(self): """Bits right shifted (>>) are decreased""" shifted = self.leet >> 3 self.assertEqual(shifted, bitmath.Bit(167))
def test_left_shift(self): """Bits left shifted (<<) are increased""" shifted = self.leet << 3 self.assertEqual(shifted, bitmath.Bit(10696))
def test_b(self): self.assertEqual(bitmath.parse_string("123b"), bitmath.Bit(123))
def get_download(cls, entry, quality = None, download_name = None, confirm = False, download_all = False, ext = 'mp4', show_description = True): qp = None quality_str = None if not ext: ext = "mp4" if download_all and not quality and not cls.quality: qp = raw_input(make_colors("QUALITY: ", 'lw', 'lr')) if cls.quality: quality = cls.quality elif qp: cls.quality = qp quality = cls.quality all_formats = entry.get('formats') if cls.is_vimeo: all_formats_temp = [] for f in all_formats: if 'http-' in f.get('format'): all_formats_temp.append(f) all_formats = all_formats_temp link = None n = 1 if not all_formats: print(make_colors("No Download Links !", 'lw', 'lr', ['blink'])) sys.exit() if show_description: print(make_colors("Name", 'lc') + " : " + make_colors(entry.get('title'), 'lw', 'bl')) description = entry.get('description') if description: print(make_colors("Description", 'lg') + ": " + make_colors(description.encode('utf-8'), 'b', 'lg')) if not quality: for f in all_formats: if len(str(n)) == 1: number = '0' + str(n) else: number = str(n) if f.get('filesize'): print(make_colors(number, 'lc') + ". " + make_colors(f.get('format'), 'lw', 'lr') + " [" + make_colors(str("%0.2f"%bitmath.Bit(f.get('filesize')).Mb) + " Mb", 'b','ly') + "] [" + make_colors(f.get('ext'), 'lr', 'lw') + "]") else: print(make_colors(number, 'lc') + ". " + make_colors(f.get('format'), 'lw', 'bl') + " [ ] [" + make_colors(f.get('ext'), 'lr', 'lw') + "]") n +=1 q = raw_input(make_colors("select number: ", 'lw','lr')) if q and str(q).strip().isdigit(): q = int(str(q).strip()) if q <= len(all_formats): link = all_formats[q - 1].get('url') quality_str = all_formats[q - 1].get('format_note') ext = all_formats[q - 1].get('ext') if not download_name: download_name = all_formats[q - 1].get('title') clipboard.copy(link) if confirm and not sys.platform == 'win32': qd = raw_input(make_colors("Download with name:", 'lw', 'bl') + " " + make_colors(download_name, 'lr', 'lw') + " [y/enter] ?: ") if not qd == 'y': sys.exit(make_colors("Exit ...", 'lr')) return link, quality_str, ext else: return False else: for i in all_formats: if not download_name: download_name = i.get('title') if str(quality).lower() in i.get('format_note') and i.get('ext') == 'mp4': link = i.get('url') quality_str = i.get('format_note') ext = i.get('ext') break if not link: print(make_colors("No Quality FOUND !", 'lw', 'lr', ['blink'])) return False clipboard.copy(link) if confirm and not sys.platform == 'win32': qd = raw_input(make_colors("Download with name:", 'lw', 'bl') + " " + make_colors(download_name, 'lr', 'lw') + " [y/enter] ?: ") if not qd == 'y': sys.exit(make_colors("Exit ...", 'lr')) return link, quality_str, ext return False
def convert_bps_to_mbps(self, num_of_bytes): bit = bitmath.Bit(num_of_bytes) return bit.to_Mib().value
def test_extreme_oom_round_down(self): """NIST: 1 Bit (as a EiB()) rounds down into a Bit()""" tiny_EiB = bitmath.EiB.from_other(bitmath.Bit(1)) self.assertIs(type(tiny_EiB.best_prefix()), bitmath.Bit)
number_of_bytes /= step_to_greater_unit unit = 'KB' if (number_of_bytes / step_to_greater_unit) >= 1: number_of_bytes /= step_to_greater_unit unit = 'MB' if (number_of_bytes / step_to_greater_unit) >= 1: number_of_bytes /= step_to_greater_unit unit = 'GB' if (number_of_bytes / step_to_greater_unit) >= 1: number_of_bytes /= step_to_greater_unit unit = 'TB' precision = 1 number_of_bytes = round(number_of_bytes, precision) return str(number_of_bytes) + " " + unit # tools = Utilities() # time = tools.humanize(11557997560) # # print tools.convertTime(1155772339) # print time import bitmath downstream = bitmath.Bit(1048576) print downstream.to_Mib()