Python convert to bytes
34 Python code examples are found related to "
convert to bytes".
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: sysvalidator.py From upvm with Apache License 2.0 | 6 votes |
def convert_size_string_to_bytes(size): unit = size[-1:] sz = float(size[:-1]) if unit == 'b': return sz elif unit == 'K': return sz * 1024**1 elif unit == 'M': return sz * 1024**2 elif unit == 'G': return sz * 1024**3 elif unit == 'T': return sz * 1024**4 elif unit == 'P': return sz * 1024**5 elif unit == 'E': return sz * 1024**6 else: print(c.RED("Invalid size specified (use e.g., '256M', '10G')")) exit(1)
Example 2
Source File: util.py From pypowervm with Apache License 2.0 | 6 votes |
def convert_bytes_to_gb(bytes_, low_value=.0001, dp=None): """Converts an integer of bytes to a decimal representation of gigabytes. If the value is too low, will return the 'low_value'. This is useful for converting a small number of bytes (ex. 50) into gigabytes. Rounding may be required. :param bytes_: The integer number of bytes. :param low_value: The minimum value that should be returned. (Note: if dp is also specified, the value returned may be rounded up and thus be higher than low_value.) :param dp: If specified, the value is rounded up to the specified number of decimal places by round_gb_size_up. (Note: None and zero are very different.) :returns: The decimal value. """ gb_size = bytes_ / float(units.Gi) if gb_size < low_value: gb_size = low_value if dp is not None: gb_size = round_gb_size_up(gb_size, dp=dp) return gb_size
Example 3
Source File: Demo_Graph_Elem_Image_Album.py From PySimpleGUI with GNU Lesser General Public License v3.0 | 6 votes |
def convert_to_bytes(file_or_bytes, resize=None): ''' Will convert into bytes and optionally resize an image that is a file or a base64 bytes object. :param file_or_bytes: either a string filename or a bytes base64 image object :type file_or_bytes: (Union[str, bytes]) :param resize: optional new size :type resize: (Tuple[int, int] or None) :return: (bytes) a byte-string object :rtype: (bytes) ''' if isinstance(file_or_bytes, str): img = PIL.Image.open(file_or_bytes) else: img = PIL.Image.open(io.BytesIO(base64.b64decode(file_or_bytes))) cur_width, cur_height = img.size if resize: new_width, new_height = resize scale = min(new_height/cur_height, new_width/cur_width) img = img.resize((int(cur_width*scale), int(cur_height*scale)), PIL.Image.ANTIALIAS) bio = io.BytesIO() img.save(bio, format="PNG") del img return bio.getvalue()
Example 4
Source File: conv.py From IOTA_demo with MIT License | 6 votes |
def convertBigintToBytes(big): bytesArrayTemp = [(abs(big) >> pos * 8) % (1 << 8) for pos in range(48)] # big endian and balanced bytesArray = list(map(lambda x: (x if x <= 0x7F else x - 0x100), reversed(bytesArrayTemp))) if big < 0: # 1-compliment bytesArray = list(map(lambda x: ~x, bytesArray)) # add1 for pos in reversed(range(len(bytesArray))): add = (bytesArray[pos] & 0xFF) + 1 bytesArray[pos] = (add if add <= 0x7F else add - 0x100) if bytesArray[pos] != 0: break return bytesArray
Example 5
Source File: conv.py From IOTA_demo with MIT License | 6 votes |
def convertBytesToBigInt(ba): # copy of array bytesArray = list(map(lambda x: x, ba)) # number sign in MSB signum = (1 if bytesArray[0] >= 0 else -1) if signum == -1: # sub1 for pos in reversed(range(len(bytesArray))): sub = (bytesArray[pos] & 0xFF) - 1 bytesArray[pos] = (sub if sub <= 0x7F else sub - 0x100) if bytesArray[pos] != -1: break # 1-compliment bytesArray = list(map(lambda x: ~x, bytesArray)) # sum magnitudes and set sign return sum((x & 0xFF) << pos * 8 for (pos, x) in enumerate(reversed(bytesArray))) * signum
Example 6
Source File: app.py From thepiratebay with MIT License | 6 votes |
def convert_to_bytes(size_str): ''' Converts torrent sizes to a common count in bytes. ''' size_data = size_str.split() multipliers = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB'] size_magnitude = float(size_data[0]) multiplier_exp = multipliers.index(size_data[1]) size_multiplier = 1024 ** multiplier_exp if multiplier_exp > 0 else 1 return size_magnitude * size_multiplier
Example 7
Source File: conv.py From iota.py with MIT License | 6 votes |
def convertBytesToBigInt(ba: List[int]) -> int: # copy of array bytesArray = list(map(lambda x: x, ba)) # number sign in MSB signum = (1 if bytesArray[0] >= 0 else -1) if signum == -1: # sub1 for pos in reversed(range(len(bytesArray))): sub = (bytesArray[pos] & 0xFF) - 1 bytesArray[pos] = (sub if sub <= 0x7F else sub - 0x100) if bytesArray[pos] != -1: break # 1-compliment bytesArray = list(map(lambda x: ~x, bytesArray)) # sum magnitudes and set sign return sum((x & 0xFF) << pos * 8 for (pos, x) in enumerate(reversed(bytesArray))) * signum
Example 8
Source File: CommandHelper.py From stonix with GNU General Public License v2.0 | 6 votes |
def convert_bytes_to_string(self, data): """ :param data: :return: data :rtype: str|list """ self.logdispatcher.log(LogPriority.DEBUG, "Converting any bytes objects into strings...") data_type = type(data) if data_type is list: for e in data: if type(e) is bytes: data = [e.decode('utf-8') for e in data] elif data_type is bytes: data = data.decode('utf-8') data = str(data) return data
Example 9
Source File: conv.py From iota.py with MIT License | 6 votes |
def convertBigIntToBytes(big: int) -> List[int]: bytesArrayTemp = [(abs(big) >> pos * 8) % (1 << 8) for pos in range(48)] # big endian and balanced bytesArray = list(map( lambda x: (x if x <= 0x7F else x - 0x100), reversed(bytesArrayTemp) )) if big < 0: # 1-compliment bytesArray = list(map(lambda x: ~x, bytesArray)) # add1 for pos in reversed(range(len(bytesArray))): add = (bytesArray[pos] & 0xFF) + 1 bytesArray[pos] = (add if add <= 0x7F else add - 0x100) if bytesArray[pos] != 0: break return bytesArray
Example 10
Source File: 0083_auto_20190215_1009.py From sal with Apache License 2.0 | 6 votes |
def convert_drives_to_int_bytes(apps, schema_editor): """Convert existing drive numbers from kbytes to bytes. This uses the binary system to scale, since that is what was used by machine checkin to convert from bytes initially. """ Machine = apps.get_model('server', 'Machine') for machine in Machine.objects.all(): # Django already converted str to int for us, but we cast back # to int just to be safe. if machine.hd_total: machine.hd_total = int(machine.hd_total * 1024.0) if machine.hd_space: machine.hd_space = int(machine.hd_space * 1024.0) machine.save()
Example 11
Source File: variable.py From RSqueak with BSD 3-Clause "New" or "Revised" License | 5 votes |
def convert_to_bytes_layout(self, wordsize): words = self.words new_words = [r_uint(0)] * len(words * wordsize) for i, word in enumerate(words): new_words[i * 4 + 0] = r_uint((word >> 0) & 0xff) new_words[i * 4 + 1] = r_uint((word >> 8) & 0xff) new_words[i * 4 + 2] = r_uint((word >> 16) & 0xff) new_words[i * 4 + 3] = r_uint((word >> 24) & 0xff) self.words = new_words return self
Example 12
Source File: dnx_protocol_tools.py From dnxfirewall-cmd with GNU General Public License v3.0 | 5 votes |
def convert_dns_string_to_bytes(domain_name): if (not domain_name): return b'\x00' split_domain = domain_name.split('.') domain_bytes = [] for part in split_domain: domain_bytes.append(byte_pack(len(part))) domain_bytes.append(part.encode('utf-8')) else: domain_bytes.append(b'\x00') return b''.join(domain_bytes) # will create dns header specific to response. default resource record count is 1
Example 13
Source File: submissions.py From evalai-cli with BSD 3-Clause "New" or "Revised" License | 5 votes |
def convert_bytes_to(byte, to, bsize=1024): """ Convert bytes to KB, MB, GB etc. Arguments: bytes {int} -- The bytes which are to be converted to {str} -- To which unit it is to be converted """ units_mapping = {"kb": 1, "mb": 2, "gb": 3, "tb": 4, "pb": 5, "eb": 6} unit = byte for value in range(units_mapping[to]): unit = int(unit / bsize) return unit
Example 14
Source File: utils.py From rasa-for-botfront with Apache License 2.0 | 5 votes |
def convert_bytes_to_string(data: Union[bytes, bytearray, Text]) -> Text: """Convert `data` to string if it is a bytes-like object.""" if isinstance(data, (bytes, bytearray)): return data.decode(io_utils.DEFAULT_ENCODING) return data
Example 15
Source File: utils.py From pymacaroons with MIT License | 5 votes |
def convert_to_bytes(string_or_bytes): if string_or_bytes is None: return None if isinstance(string_or_bytes, text_type): return string_or_bytes.encode('utf-8') elif isinstance(string_or_bytes, binary_type): return string_or_bytes else: raise TypeError("Must be a string or bytes object.")
Example 16
Source File: converters.py From mmtf-python with Apache License 2.0 | 5 votes |
def convert_ints_to_bytes(in_ints, num): """Convert an integer array into a byte arrays. The number of bytes forming an integer is defined by num :param in_ints: the input integers :param num: the number of bytes per int :return the integer array""" out_bytes= b"" for val in in_ints: out_bytes+=struct.pack(mmtf.utils.constants.NUM_DICT[num], val) return out_bytes
Example 17
Source File: converters.py From mmtf-python with Apache License 2.0 | 5 votes |
def convert_bytes_to_ints(in_bytes, num): """Convert a byte array into an integer array. The number of bytes forming an integer is defined by num :param in_bytes: the input bytes :param num: the number of bytes per int :return the integer array""" out_arr = [] for i in range(len(in_bytes)//num): val = in_bytes[i * num:i * num + num] unpacked = struct.unpack(mmtf.utils.constants.NUM_DICT[num], val) out_arr.append(unpacked[0]) return out_arr
Example 18
Source File: __init__.py From maas with GNU Affero General Public License v3.0 | 5 votes |
def convert_size_to_bytes(value): """ Converts storage size values with units (GiB, TiB...) to bytes. :param value: A string containing a number and unit separated by at least one space character. If unit is not specified, defaults to bytes. :return: An integer indicating the number of bytes for the given value in any other size unit. :raises UnknownCapacityUnitError: unsupported capacity unit. """ # Split value on the first space. capacity_def = value.split(" ", 1) if len(capacity_def) == 1: # No unit specified, default to bytes. return int(capacity_def[0]) capacity_value, capacity_unit = capacity_def capacity_value = float(capacity_value) capacity_unit = capacity_unit.strip() if capacity_unit in CAPACITY_UNITS: multiplier = CAPACITY_UNITS[capacity_unit] else: raise UnknownCapacityUnitError( "Unknown capacity unit '%s'" % capacity_unit ) # Convert value to bytes. return int(capacity_value * multiplier) # Architectures as defined by: # https://github.com/lxc/lxd/blob/master/shared/osarch/architectures.go # https://www.debian.org/releases/oldstable/i386/ch02s01.html.en
Example 19
Source File: bittorrent.py From Katastrophe with MIT License | 5 votes |
def convertBytesToDecimal(headerBytes, power): size = 0 for ch in headerBytes: size += int(ord(ch))*256**power power -= 1 return size
Example 20
Source File: CH341DriverBase.py From meerk40t with MIT License | 5 votes |
def convert_to_list_bytes(data): if isinstance(data, str): # python 2 return [ord(e) for e in data] else: return [e for e in data]
Example 21
Source File: ipopt_wrapper.py From cyipopt with Eclipse Public License 1.0 | 5 votes |
def convert_to_bytes(options): if sys.version_info >= (3, 0): for key in list(options.keys()): try: if bytes(key, 'utf-8') != key: options[bytes(key, 'utf-8')] = options[key] options.pop(key) except TypeError: pass
Example 22
Source File: ctSESAM.py From ctSESAM-python with GNU General Public License v3.0 | 5 votes |
def convert_bytes_to_password(hashed_bytes, length): number = int.from_bytes(hashed_bytes, byteorder='big') password = '' while number > 0 and len(password) < length: password = password + password_characters[number % len(password_characters)] number = number // len(password_characters) return password
Example 23
Source File: pyT4.py From Sinopac-Order-API with MIT License | 5 votes |
def convert_future_bytes_to_dict(future_order_res_bytes): future_record_field = 'trade_type,account,market_id,code_id,f_callput,ord_bs,ord_price,price_type,ord_qty,' \ 'ord_no,ord_seq,ord_type,oct_type,f_mttype,f_composit,c_futopt,c_code,c_callput,' \ 'c_buysell,c_price,c_quantity,ord_date,preord_date,ord_time,type,err_code,msg' FutureOrderRecord = namedtuple('FutureOrderRecord', future_record_field) future_order_res_format = '2s15s1s10s1s1s12s3s4s6s6s3s1s1s2s1s10s1s1s12s4s8s8s6s1s4s60s' if len(future_order_res_bytes) != struct.calcsize(future_order_res_format): return future_order_res_bytes future_order_res = FutureOrderRecord._make(struct.unpack_from(future_order_res_format, future_order_res_bytes)) future_order_res_lst = [str(item, 'cp950') for item in future_order_res] return FutureOrderRecord(*future_order_res_lst)._asdict()
Example 24
Source File: exception.py From mcsema with Apache License 2.0 | 5 votes |
def convert_to_bytes(value): is64 = get_address_size_in_bytes() == 8 if is64: sv = struct.pack("<Q", value) else: sv = struct.pack("<I", value) return " ".join("%02X" % ord(c) for c in sv)
Example 25
Source File: compat_io.py From pfp with MIT License | 5 votes |
def ConvertBytesToDouble(params, ctxt, scope, stream, coord): raise NotImplementedError() # float ConvertBytesToFloat( uchar byteArray[] )
Example 26
Source File: compat_io.py From pfp with MIT License | 5 votes |
def ConvertBytesToFloat(params, ctxt, scope, stream, coord): raise NotImplementedError() # hfloat ConvertBytesToHFloat( uchar byteArray[] )
Example 27
Source File: compat_io.py From pfp with MIT License | 5 votes |
def ConvertDataToBytes(params, ctxt, scope, stream, coord): raise NotImplementedError() # void DeleteBytes( int64 start, int64 size )
Example 28
Source File: fileIO.py From P2P-music-sharing with MIT License | 5 votes |
def convert_to_bytes(file_path=path_to_file): read_data = None with open(file_path, 'r') as file: read_data = file.read() return read_data.encode("utf-8")
Example 29
Source File: cacher.py From Cacher with Apache License 2.0 | 5 votes |
def convert_bytes_to_human_readable(number_of_bytes): if number_of_bytes < 0: raise ValueError("ERROR: number of bytes can not be less than 0") step_to_greater_unit = 1024. number_of_bytes = float(number_of_bytes) unit = 'bytes' if (number_of_bytes / step_to_greater_unit) >= 1: 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
Example 30
Source File: mgt.py From yosai with Apache License 2.0 | 5 votes |
def convert_identifiers_to_bytes(self, identifiers): """ Encryption requires a binary type as input, so this method converts the identifier collection object to one. :type identifiers: a serializable IdentifierCollection object :returns: a bytestring """ # serializes to bytes by default: return self.encrypt(self.serialization_manager.serialize(identifiers))
Example 31
Source File: mgt.py From yosai with Apache License 2.0 | 5 votes |
def convert_bytes_to_identifiers(self, encrypted, subject_context): """ If a cipher_service is available, it will be used to first decrypt the serialized message. Then, the bytes are deserialized and returned. :param serialized: the bytes to decrypt and then deserialize :param subject_context: the contextual data, that is being used to construct a Subject instance :returns: the de-serialized identifier """ # unlike Shiro, Yosai assumes that the message is encrypted: decrypted = self.decrypt(encrypted) return self.serialization_manager.deserialize(decrypted)
Example 32
Source File: telnetsrvlib.py From heralding with GNU General Public License v3.0 | 5 votes |
def convert_to_bytes(c): if isinstance(c, int): if c < 256: cb = bytes([c]) else: cb = None elif isinstance(c, str): cb = bytes(c, 'utf-8') else: cb = c return cb # vim: set syntax=python ai showmatch:
Example 33
Source File: pyT4.py From Sinopac-Order-API with MIT License | 5 votes |
def convert_stock_bytes_to_dict(stock_order_res_bytes): """委託回報為bytes。所以先轉為有結構的NameTuple,但每個item得從bytes to utf8""" stock_record_field = 'trade_type,account,code_id,ord_price,ord_qty,ord_seq,ord_date,effective_date,' \ 'ord_time,ord_no,ord_soruce,org_ord_seq,ord_bs,ord_type,market_id,price_type,ord_status,Msg' StockOrderRecord = namedtuple('StockOrderRecord', stock_record_field) stock_order_res_format = '2s15s6s6s3s6s8s8s6s5s3s6s1s2s1s1s2s60s' if len(stock_order_res_bytes) != struct.calcsize(stock_order_res_format): return stock_order_res_bytes stock_order_res = StockOrderRecord._make(struct.unpack_from(stock_order_res_format, stock_order_res_bytes)) stock_order_res_lst = [str(item, 'cp950') for item in stock_order_res] return StockOrderRecord(*stock_order_res_lst)._asdict()
Example 34
Source File: utils.py From django-gateone with GNU General Public License v3.0 | 4 votes |
def convert_to_bytes(size_val): """ Given a *size_val* (string) such as '100M', returns an integer representing an equivalent amount of bytes. Accepts the following '<num><char>' formats: =========== ========== ================================== Character Meaning Example =========== ========== ================================== B (or none) Bytes '100' or '100b' -> 100 K Kilobytes '1k' -> 1024 M Megabytes '1m' -> 1048576 G Gigabytes '1g' -> 1073741824 T Terabytes '1t' -> 1099511627776 P Petabytes '1p' -> 1125899906842624 E Exabytes '1e' -> 1152921504606846976 Z Zettabytes '1z' -> 1180591620717411303424L Y Yottabytes '7y' -> 1208925819614629174706176L =========== ========== ================================== .. note:: If no character is given the *size_val* will be assumed to be in bytes. .. tip:: All characters will be converted to upper case before conversion (case-insensitive). Examples:: >>> convert_to_bytes('2M') 2097152 >>> convert_to_bytes('2g') 2147483648 """ symbols = "BKMGTPEZY" letter = size_val[-1:].strip().upper() if letter.isdigit(): # Assume bytes letter = 'B' num = size_val else: num = size_val[:-1] assert num.isdigit() and letter in symbols num = float(num) prefix = {symbols[0]:1} for i, size_val in enumerate(symbols[1:]): prefix[size_val] = 1 << (i+1)*10 return int(num * prefix[letter])