本文整理汇总了Python中bitstring.BitArray方法的典型用法代码示例。如果您正苦于以下问题:Python bitstring.BitArray方法的具体用法?Python bitstring.BitArray怎么用?Python bitstring.BitArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bitstring
的用法示例。
在下文中一共展示了bitstring.BitArray方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: rotate
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import BitArray [as 别名]
def rotate(key):
""""Each Byte is rotated 3 positions on the left (not shifted)."""
assert len(key) == Ar_KEY_LEN+1 and type(key) == bytearray
rotated_key = bytearray()
for i in range(0, 17):
byte = BitArray(key[i:i+1])
assert len(byte.bin) == 8
# log.debug('rotate {} byte: {}, {}'.format(i, byte.bin, byte.uint))
# rotated_byte = byte << 3
rotated_byte = byte
rotated_byte.rol(3)
assert len(rotated_byte.bin) == 8
# log.debug('rotate {} rotated_byte: {}, {}'.format(i, rotated_byte.bin, rotated_byte.uint))
# NOTE: byte.uint is unsigned, byte.int is signed
rotated_key.append(rotated_byte.uint)
# log.debug('rotate rotated_key: {}'.format(repr(rotated_key)))
assert len(rotated_key) == Ar_KEY_LEN+1
return rotated_key
示例2: __init__
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import BitArray [as 别名]
def __init__(self,
address_byte,
data_bytes=[]):
"""
All arguments simle binary/hex strings: 0xFF 0b2121
"""
# A command station must send a minimum of 14 preamble bits
self.preamble = BitArray('0b1111111111111111')
self.packet_start_bit = BitArray('0b0')
self.address_byte = BitArray(address_byte)
self.data_byte_start_bit = BitArray('0b0')
self.data_bytes = map(BitArray, data_bytes)
if sys.version_info.major >= 3:
self.data_bytes = list(self.data_bytes)
self.packet_end_bit = BitArray('0b1')
assert(len(self.address_byte) == 8)
for byte in self.data_bytes:
assert(len(byte) == 8)
示例3: to_bit_array
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import BitArray [as 别名]
def to_bit_array(self):
"""
Builds a single string that should end up
being serialized.
Returns an array of True/False
"""
packet = BitArray()
packet.append(self.preamble)
packet.append(self.packet_start_bit)
packet.append(self.address_byte)
for byte in self.data_bytes:
packet.append(self.data_byte_start_bit)
packet.append(byte)
packet.append(self.packet_end_bit)
return map(int, packet)
示例4: __call__
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import BitArray [as 别名]
def __call__(self, row):
context = row.copy()
for key, value in row.items():
# We need to stringify some types to make them properly comparable
if key in self.key_list:
# numbers
# https://www.h-schmidt.net/FloatConverter/IEEE754.html
if isinstance(value, (int, float, decimal.Decimal)):
bits = BitArray(float=value, length=64)
# invert the sign bit
bits.invert(0)
# invert negative numbers
if value < 0:
bits.invert(range(1, 64))
context[key] = bits.hex
return self.key_spec.format(**context)
示例5: __encode_voice_header
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import BitArray [as 别名]
def __encode_voice_header( self, _rx_slot, _sync, _dtype ):
_src_id = _rx_slot.rf_src
_dst_id = _rx_slot.dst_id
_cc = _rx_slot.cc
# create lc
lc = '\x00\x00\x00' + _dst_id + _src_id # PF + Reserved + FLCO + FID + Service Options + Group Address + Source Address
# encode lc into info
full_lc_encode = bptc.encode_header_lc(lc)
_rx_slot.emblc = bptc.encode_emblc(lc) # save off the emb lc for voice frames B-E
_rx_slot.emblc[5] = bitarray(32) # NULL message (F)
# create slot_type
slot_type = chr((_cc << 4) | (_dtype & 0x0f)) # data type is Header or Term
# generate FEC for slot type
slot_with_fec = BitArray(uint=golay.encode_2087(slot_type), length=20)
# construct final frame - info[0:98] + slot_type[0:10] + DMR_DATA_SYNC_MS + slot_type[10:20] + info[98:196]
frame_bits = full_lc_encode[0:98] + slot_with_fec[0:10] + decode.to_bits(_sync) + slot_with_fec[10:20] + full_lc_encode[98:196]
return decode.to_bytes(frame_bits)
# Create a voice header DMR frame
示例6: send_voice72
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import BitArray [as 别名]
def send_voice72(self, _rx_slot, _ambe):
ambe72_1 = BitArray('0x' + ahex(_ambe[0:9]))[0:72]
ambe72_2 = BitArray('0x' + ahex(_ambe[9:18]))[0:72]
ambe72_3 = BitArray('0x' + ahex(_ambe[18:27]))[0:72]
ambe49_1 = ambe_utils.convert72BitTo49BitAMBE(ambe72_1)
ambe49_2 = ambe_utils.convert72BitTo49BitAMBE(ambe72_2)
ambe49_3 = ambe_utils.convert72BitTo49BitAMBE(ambe72_3)
ambe49_1.append(False)
ambe49_2.append(False)
ambe49_3.append(False)
ambe = ambe49_1 + ambe49_2 + ambe49_3
_frame = self._tempVoice[_rx_slot.vf][:33] + ambe.tobytes() + self._tempVoice[_rx_slot.vf][52:] # Insert the 3 49 bit AMBE frames
self.rewriteFrame(_frame, _rx_slot.slot, _rx_slot.dst_id, _rx_slot.rf_src, _rx_slot.repeater_id)
_rx_slot.vf = (_rx_slot.vf + 1) % 6 # the voice frame counter which is always mod 6
pass
示例7: bytes_to_mnemonic
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import BitArray [as 别名]
def bytes_to_mnemonic(seed_bytes: bytes):
seed_array = bytearray(seed_bytes)
word_list = bip39_word_list().splitlines()
checksum = bytes(std_hash(seed_bytes))
seed_array.append(checksum[0])
bytes_for_mnemonic = bytes(seed_array)
bitarray = BitArray(bytes_for_mnemonic)
mnemonics = []
for i in range(0, 24):
start = i * 11
end = start + 11
bits = bitarray[start:end]
m_word_poition = bits.uint
m_word = word_list[m_word_poition]
mnemonics.append(m_word)
return mnemonics
示例8: seed_from_mnemonic
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import BitArray [as 别名]
def seed_from_mnemonic(mnemonic: List[str]):
word_list = {word: i for i, word in enumerate(bip39_word_list().splitlines())}
bit_array = BitArray()
for i in range(0, 24):
word = mnemonic[i]
value = word_list[word]
bit_array.append(BitArray(uint=value, length=11))
all_bytes = bit_array.bytes
entropy_bytes = all_bytes[:32]
checksum_bytes = all_bytes[32]
checksum = std_hash(entropy_bytes)
if checksum[0] != checksum_bytes:
raise ValueError("Invalid order of mnemonic words")
return entropy_bytes
示例9: replaceKey
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import BitArray [as 别名]
def replaceKey(certFile, subPubKey):
s = ''
mainSection = 0
for i in open(certFile,'rt'):
if '---' in i:
mainSection=(not mainSection)
elif mainSection:
s+=i[:-1]
cert = Certificate.decode(s.decode('base64'))
arr = BitArray(bytes=subPubKey)
cert['tbsCertificate']['subjectPublicKeyInfo']['subjectPublicKey'] = arr.bin
bytes = cert.encode()
st = bytes.encode('base64').replace('\n', '')
print "-----BEGIN CERTIFICATE-----"
for i in xrange(0,len(st), 64):
print st[i:i+64]
print "-----END CERTIFICATE-----"
示例10: _format_resource
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import BitArray [as 别名]
def _format_resource(self, pon_intf_id, start_idx, end_idx):
"""
Format resource as json.
:param pon_intf_id: OLT PON interface id
:param start_idx: start index for id pool
:param end_idx: end index for id pool
:return dictionary: resource formatted as dictionary
"""
# Format resource as json to be stored in backend store
resource = dict()
resource[PONResourceManager.PON_INTF_ID] = pon_intf_id
resource[PONResourceManager.START_IDX] = start_idx
resource[PONResourceManager.END_IDX] = end_idx
# resource pool stored in backend store as binary string
resource[PONResourceManager.POOL] = BitArray(end_idx-start_idx).bin
return json.dumps(resource)
示例11: json_from_value
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import BitArray [as 别名]
def json_from_value(value):
bits = BitArray(hex=hexlify(value))
temp = AdtnVlanTaggingOperation(
filter_outer_priority=bits[0:4].uint, # 4 <-size
filter_outer_vid=bits[4:17].uint, # 13
filter_outer_tpid_de=bits[17:20].uint, # 3
# pad 12
filter_inner_priority=bits[32:36].uint, # 4
filter_inner_vid=bits[36:49].uint, # 13
filter_inner_tpid_de=bits[49:52].uint, # 3
# pad 8
filter_ether_type=bits[60:64].uint, # 4
treatment_tags_to_remove=bits[64:66].uint, # 2
# pad 10
treatment_outer_priority=bits[76:80].uint, # 4
treatment_outer_vid=bits[80:93].uint, # 13
treatment_outer_tpid_de=bits[93:96].uint, # 3
# pad 12
treatment_inner_priority=bits[108:112].uint, # 4
treatment_inner_vid=bits[112:125].uint, # 13
treatment_inner_tpid_de=bits[125:128].uint, # 3
)
return json.dumps(temp.fields, separators=(',', ':'))
示例12: json_from_value
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import BitArray [as 别名]
def json_from_value(value):
bits = BitArray(hex=hexlify(value))
temp = VlanTaggingOperation(
filter_outer_priority=bits[0:4].uint, # 4 <-size
filter_outer_vid=bits[4:17].uint, # 13
filter_outer_tpid_de=bits[17:20].uint, # 3
# pad 12
filter_inner_priority=bits[32:36].uint, # 4
filter_inner_vid=bits[36:49].uint, # 13
filter_inner_tpid_de=bits[49:52].uint, # 3
# pad 8
filter_ether_type=bits[60:64].uint, # 4
treatment_tags_to_remove=bits[64:66].uint, # 2
# pad 10
treatment_outer_priority=bits[76:80].uint, # 4
treatment_outer_vid=bits[80:93].uint, # 13
treatment_outer_tpid_de=bits[93:96].uint, # 3
# pad 12
treatment_inner_priority=bits[108:112].uint, # 4
treatment_inner_vid=bits[112:125].uint, # 13
treatment_inner_tpid_de=bits[125:128].uint, # 3
)
return json.dumps(temp.fields, separators=(',', ':'))
示例13: _get_resource
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import BitArray [as 别名]
def _get_resource(self, path):
"""
Get resource from kv store.
:param path: path to get resource
:return: resource if resource present in kv store else None
"""
# get resource from kv store
result = self._kv_store.get_from_kv_store(path)
if result is None:
return result
self._log.info("dumping resource", result=result)
resource = result
if resource is not None:
# decode resource fetched from backend store to dictionary
resource = json.loads(resource)
# resource pool in backend store stored as binary string whereas to
# access the pool to generate/release IDs it need to be converted
# as BitArray
resource[PONResourceManager.POOL] = \
BitArray('0b' + resource[PONResourceManager.POOL])
return resource
示例14: _format_resource
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import BitArray [as 别名]
def _format_resource(self, pon_intf_id, start_idx, end_idx):
"""
Format resource as json.
:param pon_intf_id: OLT PON interface id
:param start_idx: start index for id pool
:param end_idx: end index for id pool
:return dictionary: resource formatted as dictionary
"""
# Format resource as json to be stored in backend store
resource = dict()
resource[PONResourceManager.PON_INTF_ID] = pon_intf_id
resource[PONResourceManager.START_IDX] = start_idx
resource[PONResourceManager.END_IDX] = end_idx
# resource pool stored in backend store as binary string
resource[PONResourceManager.POOL] = BitArray(end_idx).bin
return json.dumps(resource)
示例15: from_bit_array
# 需要导入模块: import bitstring [as 别名]
# 或者: from bitstring import BitArray [as 别名]
def from_bit_array(int_array):
"""
Given [1, 1,...] array try to decode a packet
"""
packet = BitArray(int_array)
# preamble = packet[0:12]
address_byte = packet[13:21]
data_bytes = packet[22:-1]
dbit = 0
data_bytes_a = []
while dbit < len(data_bytes):
data_bytes_a.append(data_bytes[dbit:dbit+8])
dbit += 9 # skip start bit from next data byte
return DCCGeneralPacket(address_byte, data_bytes_a)