本文整理汇总了Python中bitstring.Bits方法的典型用法代码示例。如果您正苦于以下问题:Python bitstring.Bits方法的具体用法?Python bitstring.Bits怎么用?Python bitstring.Bits使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bitstring
的用法示例。
在下文中一共展示了bitstring.Bits方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: match_to_bits
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import Bits [as 别名]
def match_to_bits(self, key, val):
"""convert match fields and masks to bits objects.
this allows for masked matching. Converting all match fields to the
same object simplifies things (eg __str__).
"""
if isinstance(val, Bits):
return val
def _val_to_bits(conv, val, length):
if val == -1:
return Bits(int=-1, length=length)
return Bits(bytes=conv(val), length=length)
if key in self.MAC_MATCH_FIELDS:
return _val_to_bits(addrconv.mac.text_to_bin, val, 48)
if key in self.IPV4_MATCH_FIELDS:
return _val_to_bits(addrconv.ipv4.text_to_bin, val, 32)
if key in self.IPV6_MATCH_FIELDS:
return _val_to_bits(addrconv.ipv6.text_to_bin, val, 128)
return Bits(int=int(val), length=64)
示例2: write_bytes
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import Bits [as 别名]
def write_bytes(self, value, nbytes=None):
import bitstring
# TODO: strings are utf-8 from json reading
if isinstance(value, six.text_type):
value = value.encode('latin-1')
value_len = len(value)
# Ensure the string is under the required data width
if nbytes is None:
nbytes = value_len
else:
if value_len > nbytes:
value = value[:nbytes]
elif value_len < nbytes:
value += b' ' * (nbytes - value_len)
# Cannot use string format shortcut, i.e. 'bytes:{}={}' due to the
# automatic whitespace trimming by bitstring.
self.bit_stream += bitstring.Bits(bytes=value)
return value
示例3: to_bits
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import Bits [as 别名]
def to_bits(self, val):
return Bits(self.str_to_bytes(val))
示例4: _mutate
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import Bits [as 别名]
def _mutate(self):
if self._step:
length = self._min_length + self._step * self._current_index
else:
length = self._random.randint(self._min_length, self._max_length)
current_bytes = ''
for _ in range(length // 8 + 1):
current_bytes += chr(self._random.randint(0, 255))
self._current_value = Bits(bytes=strToBytes(current_bytes))[:length]
示例5: transform_data_to_bytes
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import Bits [as 别名]
def transform_data_to_bytes(data_in):
if isinstance(data_in, float):
return bytes(int(data_in))
elif isinstance(data_in, str):
return bytes(data_in, 'utf-16')
elif isinstance(data_in, Bits):
return data_in.tobytes()
else:
return bytes(data_in)
示例6: parity
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import Bits [as 别名]
def parity(bytestring):
par = 0
string = Bits(bytes=bytestring)
for bit in string:
par ^= int(bit)
return par
示例7: parity
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import Bits [as 别名]
def parity(bytestring):
"""Return parity function for a bitstring."""
par = 0
string = Bits(bytes=bytestring)
for bit in string:
par ^= int(bit)
return par
示例8: b2i
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import Bits [as 别名]
def b2i(binaryStringIn):
if len(binaryStringIn) != 64:
print("ERROR: Passed string not 64 characters. String length = %s" % len(binaryStringIn))
print("ERROR: String value '%s'" % binaryStringIn)
raise ValueError("Input strings must be 64 chars long!")
val = Bits(bin=binaryStringIn)
return val.int
示例9: b2u
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import Bits [as 别名]
def b2u(binaryStringIn):
if len(binaryStringIn) != 64:
print("ERROR: Passed string not 64 characters. String length = %s" % len(binaryStringIn))
print("ERROR: String value '%s'" % binaryStringIn)
raise ValueError("Input strings must be 64 chars long!")
val = Bits(bin=binaryStringIn)
return val.uint
# Node ID numbers are derived from the list ordering.
# This will generate a single node with 64 children.
示例10: b2i
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import Bits [as 别名]
def b2i(binaryStringIn):
if len(binaryStringIn) != 64:
print("ERROR: Passed string not 64 characters. String length = %s" % len(binaryStringIn))
print("ERROR: String value '%s'" % binaryStringIn)
raise ValueError("Input strings must be 64 chars long!")
val = Bits(bin=binaryStringIn)
return val.int
# Node ID numbers are derived from the list ordering.
示例11: _pretty_field_str
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import Bits [as 别名]
def _pretty_field_str(self, key, value, mask=None):
mask_str = ""
value_int = value
mask_int = mask
if isinstance(value, Bits):
value_int = value.int
if isinstance(mask, Bits):
mask_int = mask.int # pytype: disable=attribute-error
elif mask is None:
mask_int = -1
if key == 'vlan_vid':
if value_int & ofp.OFPVID_PRESENT == 0:
result = 'vlan untagged'
elif key == 'vlan_vid' and mask_int == ofp.OFPVID_PRESENT:
result = 'vlan tagged'
else:
result = str(value_int ^ ofp.OFPVID_PRESENT)
if mask_int != -1:
mask_str = str(mask_int ^ ofp.OFPVID_PRESENT)
elif isinstance(value, Bits):
result = self.bits_to_str(key, value)
if mask is not None and mask_int != -1:
mask_str = self.bits_to_str(key, mask)
elif isinstance(value, str):
result = value
if mask is not None:
mask_str = mask
elif isinstance(value, int):
if key in self.HEX_FIELDS:
result = hex(value)
if mask is not None and mask != -1:
mask_str = hex(mask)
else:
result = str(value)
if mask is not None and mask != -1:
mask_str = str(mask)
if mask_str:
result += "/{}".format(mask_str)
return result
示例12: set_uint
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import Bits [as 别名]
def set_uint(self, value, nbits, bitpos):
import bitstring
if nbits // NBITS_PER_BYTE == 0:
bins = bitstring.Bits(uint=value, length=nbits)
else:
bins = bitstring.Bits(uintbe=value, length=24)
self.bit_stream[bitpos: bitpos + nbits] = bins
示例13: decrypt_images
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import Bits [as 别名]
def decrypt_images(**kwargs):
path = kwargs.pop('path', 'herders/static/herders/images')
for im_path in iglob(f'{path}/**/*.png', recursive=True):
encrypted = BitStream(filename=im_path)
# Check if it is 'encrypted'. 8th byte is 0x0B instead of the correct signature 0x0A
encrypted.pos = 0x07 * 8
signature = encrypted.peek('uint:8')
if signature == 0x0B:
print(f'Decrypting {im_path}')
# Correct the PNG signature
encrypted.overwrite('0x0A', encrypted.pos)
# Replace bits with magic decrypted values
try:
while True:
pos = encrypted.pos
val = encrypted.peek('uint:8')
encrypted.overwrite(Bits(uint=com2us_decrypt_values[val], length=8), pos)
except ReadError:
# EOF
pass
# Write it back to the file
with open(im_path, 'wb') as f:
encrypted.tofile(f)
continue
# Check for weird jpeg format with extra header junk. Convert to png.
encrypted.pos = 0
if encrypted.peek('bytes:5') == b'Joker':
print(f'Trimming and converting weird JPEG to PNG {im_path}')
del encrypted[0:16 * 8]
# Open it as a jpg and resave to disk
try:
new_imfile = Image.open(io.BytesIO(encrypted.tobytes()))
new_imfile.save(im_path)
except IOError:
print(f'Unable to open {im_path}')
示例14: bits_to_signed_int
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import Bits [as 别名]
def bits_to_signed_int(s):
return Bits(bin=s).int
示例15: parse
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import Bits [as 别名]
def parse(self, bitstrm):
"""
MSP430 instructions can have one or two extension words for 16 bit immediates
We therefore extend the normal parsing so that we make sure we can
get another word if we have to.
"""
data = Instruction.parse(self, bitstrm)
data['S'] = None
data['D'] = None
# We don't always have a source or destination.
# Theoretically I could put these in the TypeXInstruction classes, but
# I'm lazy. Note that we resolve these here, as opposed to later, due to
# needing to fiddle with the bitstream.
l.debug(data)
if 's' in data:
src_mode = int(data['A'], 2)
if (src_mode == ArchMSP430.Mode.INDEXED_MODE and data['s'] != '0011') \
or (data['s'] == '0000' and src_mode == ArchMSP430.Mode.INDIRECT_AUTOINCREMENT_MODE):
data['S'] = bitstring.Bits(uint=bitstrm.read('uintle:16'), length=16).bin
self.bitwidth += 16 # pylint: disable=no-member
if 'd' in data:
dst_mode = int(data['a'], 2)
if dst_mode == ArchMSP430.Mode.INDEXED_MODE:
data['D'] = bitstring.Bits(uint=bitstrm.read('uintle:16'), length=16).bin
self.bitwidth += 16 # pylint: disable=no-member
return data