本文整理汇总了Python中bitstring.BitStream.append方法的典型用法代码示例。如果您正苦于以下问题:Python BitStream.append方法的具体用法?Python BitStream.append怎么用?Python BitStream.append使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bitstring.BitStream
的用法示例。
在下文中一共展示了BitStream.append方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: decode
# 需要导入模块: from bitstring import BitStream [as 别名]
# 或者: from bitstring.BitStream import append [as 别名]
def decode(self, in_stream, out_stream):
bs = BitStream()
dq = deque()
at_least_three = False
for word in self.words_from_file(in_stream):
if not word or word not in self.word_dict:
continue
#print >> sys.stderr, 'word:"', word, '"'
dq.append(self.word_dict[word])
if at_least_three or len(dq) == 3:
bs.append(pack(self.int_type, dq.popleft()))
at_least_three = True
if bs.len > self.bit_buffer:
cut = 0
for byte in bs.cut(self.bit_buffer):
cut += 1
byte.tofile(out_stream)
del bs[:cut * self.bit_buffer]
# dq has to have exactly 2 elements here, the last is the bit length of the first, unless it's 0
#print >> sys.stderr, 'dq:', dq
extra_bits = dq.pop()
bs.append(pack('uint:' + str(extra_bits), dq.popleft()))
bs.tofile(out_stream)
示例2: encode
# 需要导入模块: from bitstring import BitStream [as 别名]
# 或者: from bitstring.BitStream import append [as 别名]
def encode(self, in_stream, out_stream):
extra_bits = self.num_bits
bs = BitStream()
try:
while True:
chunk = in_stream.read(self.byte_buffer)
#print >> sys.stderr, 'chunk:', chunk
if(chunk):
bs.append(BitStream(bytes=chunk))
else:
while True:
self.print_index(bs.read(self.int_type), out_stream)
try:
while True:
self.print_index(bs.read(self.int_type), out_stream)
except ReadError, e:
#print >> sys.stderr, 'inner:', e
pass
except ReadError, e:
#print >> sys.stderr, 'outer:', e
extra_bits = bs.len - bs.bitpos
if extra_bits > 0:
#print >> sys.stderr, 'extra_bits:', extra_bits
self.print_index(bs.read('uint:' + str(extra_bits)), out_stream)
else:
extra_bits = self.num_bits
示例3: RTCM_converter_thread
# 需要导入模块: from bitstring import BitStream [as 别名]
# 或者: from bitstring.BitStream import append [as 别名]
def RTCM_converter_thread(server, port, username, password, mountpoint, rtcm_callback = None):
import subprocess
nt = subprocess.Popen(["./ntripclient",
"--server", server,
"--password", password,
"--user", username,
"--mountpoint", mountpoint ],
stdout=subprocess.PIPE)
"""nt = subprocess.Popen(["./ntrip.py", server, str(port), username, password, mountpoint],
stdout=subprocess.PIPE)"""
if nt is None or nt.stdout is None:
indev = sys.stdin
else:
indev = nt.stdout
print("RTCM using input {}".format(indev))
while True:
sio = indev
d = ord(sio.read(1))
if d != RTCMv3_PREAMBLE:
continue
pack_stream = BitStream()
l1 = ord(sio.read(1))
l2 = ord(sio.read(1))
pack_stream.append(bs.pack('2*uint:8', l1, l2))
pack_stream.read(6)
pkt_len = pack_stream.read(10).uint
pkt = sio.read(pkt_len)
parity = sio.read(3)
if len(pkt) != pkt_len:
print "Length error {} {}".format(len(pkt), pkt_len)
continue
if True: #TODO check parity
for d in pkt:
pack_stream.append(bs.pack('uint:8',ord(d)))
msg = parse_rtcmv3(pack_stream)
if msg is not None and rtcm_callback is not None:
rtcm_callback(msg)
示例4: decrypt
# 需要导入模块: from bitstring import BitStream [as 别名]
# 或者: from bitstring.BitStream import append [as 别名]
def decrypt(ciphertext, key):
bstream = BitStream()
p, g, y = key[0]
u = key[1]
#trying to improve execution speed
#a_pow_u = mod_exp(ciphertext[1][0], u, p)
#inv_a_pow_u = modinv(a_pow_u, p)
for block in ciphertext[1:]:
#sys.stdout.write(".")
#trying to improve execution speed
a_pow_u = mod_exp(block[0], u, p)
inv_a_pow_u = modinv(a_pow_u, p)
x = (block[1] * inv_a_pow_u) % p
block_size = math.floor(math.log(p,2))
bstream.append('0b' + bin(x)[2:].zfill(int(block_size)))
return bstream.read(ciphertext[0])
示例5: pack_pak
# 需要导入模块: from bitstring import BitStream [as 别名]
# 或者: from bitstring.BitStream import append [as 别名]
def pack_pak(dir, file_list = None, align_toc = 16, align_files = 16, eof = False):
if file_list == None:
file_list = sorted(os.listdir(dir))
num_files = len(file_list)
toc_length = (num_files + 1) * 4
if eof:
toc_length += 1
if toc_length % align_toc > 0:
toc_length += align_toc - (toc_length % align_toc)
archive_data = BitStream(uintle = 0, length = toc_length * 8)
archive_data.overwrite(bitstring.pack("uintle:32", num_files), 0)
for file_num, item in enumerate(file_list):
full_path = os.path.join(dir, item)
if os.path.isfile(full_path):
data = pack_file(full_path)
else:
data = pack_dir(full_path, align_toc, align_files, eof)
file_size = data.len / 8
padding = 0
if file_size % align_files > 0:
padding = align_files - (file_size % align_files)
data.append(BitStream(uintle = 0, length = padding * 8))
file_pos = archive_data.len / 8
archive_data.overwrite(bitstring.pack("uintle:32", file_pos), (file_num + 1) * 32)
archive_data.append(data)
del data
if eof:
archive_data.overwrite(bitstring.pack("uintle:32", archive_data.len / 8), (num_files + 1) * 32)
return archive_data
示例6: serialize
# 需要导入模块: from bitstring import BitStream [as 别名]
# 或者: from bitstring.BitStream import append [as 别名]
def serialize(self):
bitstream = BitStream()
if self.leaf():
bitstream.append('bin=1')
bitstream.append("{0:#0{1}x}".format(self.value, 4))
else:
bitstream.append('bin=0')
bitstream += self.left.serialize() + self.right.serialize()
return bitstream
示例7: to_bin
# 需要导入模块: from bitstring import BitStream [as 别名]
# 或者: from bitstring.BitStream import append [as 别名]
def to_bin(commands):
data = BitStream()
lines = 0
for op, params in commands:
if op == WRD_HEADER:
continue
if not op in OP_PARAMS:
# raise Exception("Unknown op: 0x%02X" % op)
print "Unknown op: 0x%02X" % op
continue
param_info = OP_PARAMS[op]
# If it has a custom parsing function, use the equivalent packing function.
if isinstance(param_info, basestring):
command = globals()[OP_FUNCTIONS[op]](**params)
data.append(command)
else:
if op == WRD_SHOW_LINE:
lines += 1
data.append(bitstring.pack("uint:8, uint:8", CMD_MARKER, op))
unnamed_param_id = 0
for param_name, param_type in param_info:
if param_name == None:
data.append(bitstring.pack(param_type, params[param_name][unnamed_param_id]))
unnamed_param_id += 1
else:
data.append(bitstring.pack(param_type, params[param_name]))
return bitstring.pack("uint:8, uint:8, uintle:16", CMD_MARKER, WRD_HEADER, lines) + data
示例8: compress
# 需要导入模块: from bitstring import BitStream [as 别名]
# 或者: from bitstring.BitStream import append [as 别名]
def compress(self, data):
weight = Counter(data)
priority_queue = [HuffmanNode(value=byte, weight=weight[byte])
for byte in weight]
heapify(priority_queue)
while len(priority_queue) > 1:
left = heappop(priority_queue)
right = heappop(priority_queue)
node = HuffmanNode(left, right, weight=left.weight + right.weight)
heappush(priority_queue, node)
root = heappop(priority_queue)
dictionary = root.assign_codes()
"""
we need to add the tree to the compressed data, so that the
decompressor can rebuild it in order do to it's work
"""
tree = root.serialize()
result = BitStream()
tree_len_bits = len(bin(len(tree))[2:])
if tree_len_bits > 16:
raise ValueError("Huffman tree len is max 10*255-1 bit")
# this converts len(tree) to hex with zero front pad to two bytes
result.append("{0:#0{1}x}".format(len(tree), 6))
result += tree
for byte in data:
result.append('bin=' + dictionary[byte])
pad = 0
if len(result) % 8 != 0:
pad = 8 - len(result) % 8
result.append('bin=' + '0' * pad)
"""
the compressed data layout is as follows:
* 1B - number of pad bits (for byte align)
* 2B- Huffman tree length (which btw = 10*num_of_chars_in_the_tree -1)
* the Huffman tree itself (not byte aligned)
* the encoded data paded with 0-7 bits at the end
"""
result = BitStream("{0:#0{1}x}".format(pad, 4)) + result
return bytearray(result.bytes)
示例9: __init__
# 需要导入模块: from bitstring import BitStream [as 别名]
# 或者: from bitstring.BitStream import append [as 别名]
class NALUnit:
def __init__(self, filp, pos, utype, size, tid, nosetup=False):
self.filp = filp
self.pos = pos
self.num_bytes_in_nalu = size
self.nal_unit_type = utype
self.temporal_id = tid
self.rbsp_byte = BitStream()
self.setup_rbsp()
self.print_bin()
if not nosetup:
self.setup()
def rbsp_read(self, fmt):
return self.rbsp_byte.read(fmt)
def next_bits(self, fmt, forward=False):
try:
if not forward:
ret = self.filp.peek(fmt)
else:
ret = self.filp.read(fmt)
except ReadError:
return 0
return ret
def setup_rbsp(self):
self.filp.bytepos += 2
self.num_bytes_in_rbsp = 0
i = 2
while i < self.num_bytes_in_nalu:
if i + 2 < self.num_bytes_in_nalu and self.next_bits('hex: 24') == '000003':
self.rbsp_byte.append(self.filp.read(8))
self.num_bytes_in_rbsp += 1
self.rbsp_byte.append(self.filp.read(8))
self.num_bytes_in_rbsp += 1
# discard emulation_prevention_three_byte
self.filp.read(8)
i += 3
else:
self.rbsp_byte.append(self.filp.read(8))
self.num_bytes_in_rbsp += 1
i += 1
self.filp.bytepos = self.pos
def more_rbsp_data(self):
try:
while self.rbsp_read('uint: 1') != 1:
pass
return True
except ReadError:
return False
def rbsp_trailing_bits(self):
pos = self.rbsp_byte.pos
bits = self.rbsp_byte.bytealign()
self.rbsp_byte.pos = pos
if bits == 0 or self.rbsp_read('uint: %d' % bits) != 1 << bits:
print('Wrong rbsp_trailing_bits at NALU begined at bytes %d' % self.pos)
exit(1)
def profile_tier_level(self, ProfilePresentFlag, MaxNumSubLayersMinus1):
if ProfilePresentFlag:
self.general_profile_space = self.rbsp_read('uint: 2')
self.general_tier_flag = self.rbsp_read('uint: 1')
self.general_profile_idc = self.rbsp_read('uint: 5')
self.general_profile_compatibility_flag = [0] * 32
for i in range(0, 32):
self.general_profile_compatibility_flag[i] = self.rbsp_read("uint: 1")
self.general_reserved_zero_16bits = self.rbsp_read('uint: 16')
self.general_level_idc = self.rbsp_read('uint: 8')
self.sub_layer_profile_present_flag = [0] * MaxNumSubLayersMinus1
self.sub_layer_level_present_flag = [0] * MaxNumSubLayersMinus1
self.sub_layer_profile_space = [0] * MaxNumSubLayersMinus1
self.sub_layer_tier_flag = [0] * MaxNumSubLayersMinus1
self.sub_layer_profile_idc = [0] * MaxNumSubLayersMinus1
self.sub_layer_reserved_zero_16bits = [0] * MaxNumSubLayersMinus1
self.sub_layer_level_idc = [0] * MaxNumSubLayersMinus1
self.sub_layer_profile_compatibility_flag = [[0] * 32] * MaxNumSubLayersMinus1
for i in range(0, MaxNumSubLayersMinus1):
self.sub_layer_profile_present_flag[i] = self.rbsp_read('uint: 1')
self.sub_layer_level_present_flag[i] = self.rbsp_read('uint: 1')
if ProfilePresentFlag and self.sub_layer_profile_present_flag[i]:
self.sub_layer_profile_space[i] = self.rbsp_read('uint: 2')
self.sub_layer_tier_flag[i] = self.rbsp_read('uint: 1')
self.sub_layer_profile_idc[i] = self.rbsp_read('uint: 5')
for j in range(0, 32):
self.sub_layer_profile_compatibility_flag[i][j] = self.rbsp_read('uint: 1')
self.sub_layer_reserved_zero_16bits[i] = self.rbsp_read('uint: 16')
if self.sub_layer_level_present_flag[i]:
self.sub_layer_level_idc[i] = self.rbsp_read('uint: 8')
def op_point(self, opIdx):
self.op_num_layer_id_values_minus1 = [opIdx] = self.rbsp_read('ue')
self.op_layer_id[opIdx] = [0] * self.op_num_layer_id_values_minus1
for i in range(0, self.op_num_layer_id_values_minus1):
self.op_layer_id[opIdx][i] = self.rbsp_read('uint: 6')
def short_term_ref_pic_set(self, idxRps):
#.........这里部分代码省略.........
示例10: BitStream
# 需要导入模块: from bitstring import BitStream [as 别名]
# 或者: from bitstring.BitStream import append [as 别名]
if( usbEndpoint != INCOMING_ENDPOINT and
usbEndpoint != OUTGOING_ENDPOINT ):
continue
usbBuffer = BitStream('0x%s' % ( packet.data.usb_capdata.replace( ':', '' ) ) )
usbHeader = usbBuffer.readlist( 'bytes:3, uint:8' )
# Validate the header
if( usbEndpoint == OUTGOING_ENDPOINT and usbHeader[0].encode( 'hex' ) != '000000' ):
print 'Unexpected USB Header. Expected "0x000000", got "0x%s".' % ( usbHeader[0].encode( 'hex' ) )
raise Exception
if( usbEndpoint == INCOMING_ENDPOINT and usbHeader[0] != 'ABC' ):
print 'Unexpected USB Header. Expected "0x414243", got "0x%s".' % ( usbHeader[0].encode( 'hex' ) )
raise Exception
messageBuffer.append( usbBuffer.read( usbHeader[1] * 8 ) )
# Clear the messageBuffer if we have a full message
# TODO - we need to be able to figure out if all 60 bytes are conusumed, but it's the end of the message
if( usbHeader[1] < USB_PACKET_SIZE ):
print >> sys.stderr, 'Message %s' % ( 'OUT' if usbEndpoint == OUTGOING_ENDPOINT else 'IN' )
print >> sys.stderr, 'Hex: %s' % ( messageBuffer.hex )
# TODO - make a bayerMessage to also handle standard command sequences and ASTM messages
if( messageBuffer.bytes[0:2] != 'Q\x03' ):
print >> sys.stderr, 'String: %s\n' % ( messageBuffer.bytes )
else:
msg = BayerBinaryMessage.MessageFactory( messageBuffer,
BayerBinaryMessage.OUT if usbEndpoint == OUTGOING_ENDPOINT else BayerBinaryMessage.IN, pumpSession )
if( msg is not None ):
if( isinstance( msg, MtGetAttachedPumpMessage ) ):
pumpSession.pumpSerial = msg.pumpSerial
示例11: hash_string
# 需要导入模块: from bitstring import BitStream [as 别名]
# 或者: from bitstring.BitStream import append [as 别名]
def hash_string(k, p, plaintext):
bstream = BitStream()
for c in plaintext:
bstream.append("0x" + c.encode("hex"))
return AHash(k, p, bstream)
示例12: encrypt_string
# 需要导入模块: from bitstring import BitStream [as 别名]
# 或者: from bitstring.BitStream import append [as 别名]
def encrypt_string(plaintext, pub_key):
bstream = BitStream()
for c in plaintext:
bstream.append("0x" + c.encode("hex"))
return encrypt(bstream, pub_key)
示例13: PacketDecoder
# 需要导入模块: from bitstring import BitStream [as 别名]
# 或者: from bitstring.BitStream import append [as 别名]
class PacketDecoder(object):
HEADER = "0x59"
FOOTER = "0x5254464d"
LENGTH = 56 * 8
PATTERN = "2BH9I8B2I"
packet_store = []
def __init__(self, listener_func):
self.listener_func = listener_func
self.stream = BitStream()
def push(self, byte_array):
self.stream.append(byte_array)
def handle(self):
while True:
if len(self.stream) < self.LENGTH:
break
# Look for a header
header_pos = self.stream.find(self.HEADER)
if header_pos:
header_pos = header_pos[0]
else:
self.stream.clear()
break
end_pos = header_pos + self.LENGTH
if len(self.stream) < end_pos:
# Not Enough Data
break
# Extract potential packet
potential_packet = self.stream[header_pos:end_pos].bytes
valid_packet = self.analyze_packet(potential_packet)
if valid_packet:
self.stream = self.stream[end_pos:]
else:
self.stream = self.stream[header_pos + 1 :]
self.save_packets()
self.packet_store = []
def analyze_packet(self, byte_string):
if len(byte_string) != 56:
return
data = struct.unpack(self.PATTERN, byte_string)
header = data[0]
footer = data[21]
if hex(header) == self.HEADER and hex(footer) == self.FOOTER:
if self.validate_checksums(data):
packet = self.create_packet(data)
self.packet_store.append(packet)
return True
return False
def validate_checksums(self, packet_data):
# voltage_checksum = packet_data[12]
# current_checksum = packet_data[13]
# period_checksum = packet_data[14]
# active_checksum = packet_data[15]
# reactive_checksum = packet_data[16]
# apparent_power = packet_data[17]
# phase_angle_checksum = packet_data[18]
# power_factor_checksum = packet_data[19]
# checksum = packet_data[20]
return True
def create_packet(self, packet_data):
flags = packet_data[2]
epoch = packet_data[3]
voltage = packet_data[4] * (2.37748 * 10 ** -4) + -0.14427
current = packet_data[5] * (113240.82786) + 953.97194
period = packet_data[6]
active_power = packet_data[7]
reactive_power = packet_data[8]
apparent_power = packet_data[9]
phase_angle = packet_data[10]
power_factor = packet_data[11]
return Packet(
flags=flags,
epoch=epoch,
voltage=voltage,
current=current,
period=period,
active_power=active_power,
reactive_power=reactive_power,
apparent_power=apparent_power,
phase_angle=phase_angle,
power_factor=power_factor,
)
def save_packets(self):
# submit packets to lib
if self.packet_store:
self.listener_func(self.packet_store)
示例14: encode
# 需要导入模块: from bitstring import BitStream [as 别名]
# 或者: from bitstring.BitStream import append [as 别名]
def encode(self, bit_strm=None, offset=0):
if bit_strm is None:
bit_strm = BitStream()
if not isinstance(bit_strm, BitStream):
return None
bit_strm.append(BitArray(uint=self.syncword, length=12))
bit_strm.append(BitArray(int=self.ID, length=1))
bit_strm.append(BitArray(int=self.layer, length=2))
bit_strm.append(BitArray(int=self.protection_absent, length=1))
bit_strm.append(BitArray(int=self.profile, length=2))
bit_strm.append(BitArray(int=self.sampling_index, length=4))
bit_strm.append(BitArray(int=self.private_bit, length=1))
bit_strm.append(BitArray(int=self.chan_config, length=3))
bit_strm.append(BitArray(int=self.original_copy, length=1))
bit_strm.append(BitArray(int=self.home, length=1))
bit_strm.append(BitArray(int=self.copyright_id_bit, length=1))
bit_strm.append(BitArray(int=self.copyright_id_start, length=1))
bit_strm.append(BitArray(uint=self.aac_frame_length, length=13))
bit_strm.append(BitArray(uint=self.adts_buffer_fullness, length=11))
bit_strm.append(BitArray(int=self.number_of_raw_data_blocks_in_frame, length=2))
bit_strm.byteswap()
return bit_strm
示例15: dataCollect
# 需要导入模块: from bitstring import BitStream [as 别名]
# 或者: from bitstring.BitStream import append [as 别名]
#.........这里部分代码省略.........
#if p_process_start.search(line) and complete_block is False:
# # skip part event log block
# complete_block = True
# continue
##if p_process_found is False and p_process_start.search(line):
## p_process_found = True
## continue
##if p_process_found and skip_lines > 0:
## skip_lines -= 1
## continue
##if p_process_found and p_close.search(line):
## p_process_found = False
## break
##if args.maxNumEventEntries :
## if maxNumLines > 0:
## maxNumLines -= 1
## else:
## break
currentLine += 1 #ag counter from 1
if currentLine >= startLine and currentLine < endLine: #[startline,endline)
if (currentLine - startLine)%1000 == 0:
print("{} entries collected".format((currentLine - startLine)*2))
##if p_process_found and skip_lines == 0:
#m = p_data.search(line)
##print(line)
#data = m.group(1).split(" ")
#if len(data) == 4:
# eventLogEntryBitStream.append('0x'+data[0])
# eventLogEntryBitStream.append('0x'+data[1])
# eventLogEntryBitStream.append('0x'+data[2])
# eventLogEntryBitStream.append('0x'+data[3])
# self.dataEntryAppend(eventLogEntryBitStream)
# eventLogEntryBitStream.clear()
# entryList.append(entryIndex)
# entryIndex += 1
#else:
# # two event entries of 64 bits for each
# eventLogEntryBitStream.append('0x'+data[0])
# eventLogEntryBitStream.append('0x'+data[1])
# eventLogEntryBitStream.append('0x'+data[2])
# eventLogEntryBitStream.append('0x'+data[3])
# self.dataEntryAppend(eventLogEntryBitStream)
# eventLogEntryBitStream.clear()
# entryList.append(entryIndex)
# entryIndex += 1
# eventLogEntryBitStream.append('0x'+data[4])
# eventLogEntryBitStream.append('0x'+data[5])
# eventLogEntryBitStream.append('0x'+data[6])
# eventLogEntryBitStream.append('0x'+data[7])
# self.dataEntryAppend(eventLogEntryBitStream)
# eventLogEntryBitStream.clear()
# entryList.append(entryIndex)
# entryIndex += 1
data = line.strip().split(" ")
# offset '' entries
if len(data) == 6:
eventLogEntryBitStream.append('0x'+data[2])
eventLogEntryBitStream.append('0x'+data[3])
eventLogEntryBitStream.append('0x'+data[4])
eventLogEntryBitStream.append('0x'+data[5])
self.dataEntryAppend(eventLogEntryBitStream)
eventLogEntryBitStream.clear()
entryList.append(entryIndex)
entryIndex += 1
else:
# two event entries of 64 bits for each
eventLogEntryBitStream.append('0x'+data[2])
eventLogEntryBitStream.append('0x'+data[3])
eventLogEntryBitStream.append('0x'+data[4])
eventLogEntryBitStream.append('0x'+data[5])
self.dataEntryAppend(eventLogEntryBitStream)
eventLogEntryBitStream.clear()
entryList.append(entryIndex)
entryIndex += 1
eventLogEntryBitStream.append('0x'+data[6])
eventLogEntryBitStream.append('0x'+data[7])
eventLogEntryBitStream.append('0x'+data[8])
eventLogEntryBitStream.append('0x'+data[9])
self.dataEntryAppend(eventLogEntryBitStream)
eventLogEntryBitStream.clear()
entryList.append(entryIndex)
entryIndex += 1
print("--finish 64 bit eventlog entry collection")
pass