本文整理汇总了Python中struct.struct_unpack函数的典型用法代码示例。如果您正苦于以下问题:Python struct_unpack函数的具体用法?Python struct_unpack怎么用?Python struct_unpack使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了struct_unpack函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _load_from_file
def _load_from_file(self,source):
self.filename = source
self.wavFile = wavFile = wave_open(self.filename, "rb")
channels = wavFile.getnchannels()
sampleWidth = wavFile.getsampwidth()
samplingRate = wavFile.getframerate()
numSamples = wavFile.getnframes()
compName = wavFile.getcompname()
if (channels not in [1,2]):
msg = "for \"%s\", channels=%d is not supported" \
% (self.filename,channels)
raise UGenError(msg)
if (sampleWidth not in [1,2]):
msg = "for \"%s\", sampleWidth=%d is not supported" \
% (self.filename,sampleWidth)
raise UGenError(msg)
if (samplingRate != UGen.samplingRate):
msg = "for \"%s\", samplingRate=%s does not match %s" \
% (self.filename,samplingRate)
raise UGenError(msg)
if (compName != "not compressed"):
msg = "for \"%s\", compName=\"%s\" is not supported" \
% (self.filename,compName)
raise UGenError(msg)
self.inChannels = 0
self.outChannels = channels
sampleScale = (1<<(8*sampleWidth-1)) - 1
if (sampleWidth == 1): packFormat = "b"
else: packFormat = "h"
if (channels == 2): packFormat = "2%s" % packFormat
self._allocate(numSamples)
if ("Clip" in UGen.debug):
print >>stderr, "Clip.load(%s)" % self
print >>stderr, " channels = %s" % channels
print >>stderr, " numSamples = %s" % numSamples
print >>stderr, " sampleWidth = %s" % sampleWidth
print >>stderr, " sampleScale = %s" % sampleScale
print >>stderr, " packFormat = %s" % packFormat
if (channels == 1):
for ix in xrange(numSamples):
sample = wavFile.readframes(1)
sample = struct_unpack(packFormat,sample)[0]
if (sample < -sampleScale): sample = -sampleScale
self._buffer[ix] = sample / float(sampleScale)
else: # (channels == 2):
for ix in xrange(numSamples):
sample = wavFile.readframes(1)
(sample,sample2) = struct_unpack(packFormat,sample)
if (sample < -sampleScale): sample = -sampleScale
if (sample2 < -sampleScale): sample2 = -sampleScale
self._buffer [ix] = sample / float(sampleScale)
self._buffer2[ix] = sample2 / float(sampleScale)
wavFile.close()
示例2: _read_snappy_java_header
def _read_snappy_java_header(self):
magic = self.fileobj.read(len(SNAPPY_JAVA_MAGIC))
if magic != SNAPPY_JAVA_MAGIC:
raise IOError, 'Not a snappy-java file'
version = struct_unpack("<H", self._read_exact(2))
compatible_version = struct_unpack("<H", self._read_exact(2))
示例3: read_le_bytes
def read_le_bytes(f, n):
s = f.read(n)
if len(s) < n:
raise ValueError("data malformed")
if n == 1:
return ord(s)
elif n == 2:
return struct_unpack("<H", s)[0]
elif n == 3:
return ord(s[0]) | (ord(s[1]) << 8) | (ord(s[2]) << 16)
elif n == 4:
return struct_unpack("<I", s)[0]
else:
raise ValueError("number of bytes must be in range [1..4]")
示例4: intread
def intread(buf):
"""Unpacks the given buffer to an integer."""
try:
if isinstance(buf, int):
return buf
length = len(buf)
if length == 1:
return buf[0]
elif length <= 4:
tmp = buf + b"\x00" * (4 - length)
return struct_unpack("<I", tmp)[0]
tmp = buf + b"\x00" * (8 - length)
return struct_unpack("<Q", tmp)[0]
except:
raise
示例5: getAdata
def getAdata(self):
if DNS.LABEL_UTF8:
enc = 'utf8'
else:
enc = DNS.LABEL_ENCODING
x = socket.inet_aton(self.getaddr().decode(enc))
return struct_unpack("!I", x)[0]
示例6: _binparse_fixed
def _binparse_fixed(self, read):
s = struct_unpack(self._struct_format, read(self.arraysize * 2))[0]
s = s.decode('utf_16_be')
end = s.find('\0')
if end != -1:
return s[:end], False
return s, False
示例7: decode_date
def decode_date(encvalue):
val, = struct_unpack('I', encvalue + b'\x00')
return date(
day=val & 0x1F,
month=(val >> 5) & 0xF,
year=(val >> 9)
)
示例8: addr2bin
def addr2bin(addr):
# Updated from pyspf
"""Convert a string IPv4 address into an unsigned integer.
Examples::
>>> addr2bin('127.0.0.1')
2130706433
>>> addr2bin('127.0.0.1') == socket.INADDR_LOOPBACK
1
>>> addr2bin('255.255.255.254')
4294967294L
>>> addr2bin('192.168.0.1')
3232235521L
Unlike old DNS.addr2bin, the n, n.n, and n.n.n forms for IP addresses
are handled as well::
>>> addr2bin('10.65536')
167837696
>>> 10 * (2 ** 24) + 65536
167837696
>>> addr2bin('10.93.512')
173867520
>>> 10 * (2 ** 24) + 93 * (2 ** 16) + 512
173867520
"""
return struct_unpack("!L", inet_aton(addr))[0]
示例9: decode_time
def decode_time(encvalue):
if len(encvalue) == 6:
val, = struct_unpack('Q', encvalue + b'\x00\x00')
return time(
microsecond=val & 0xFFFFF,
second=(val >> 20) & 0x3F,
minute=(val >> 26) & 0x3F,
hour=(val >> 32)
)
else: # must be 3
val, = struct_unpack('I', encvalue + b'\x00')
return time(
microsecond=0,
second=(val) & 0x3F,
minute=(val >> 6) & 0x3F,
hour=(val >> 12)
)
示例10: ioctl_GWINSZ
def ioctl_GWINSZ(fd):
try:
from fcntl import ioctl as fcntl_ioctl
import termios
cr = struct_unpack('hh',
fcntl_ioctl(fd, termios.TIOCGWINSZ, '1234'))
return cr
except:
pass
示例11: parse
def parse(self, data):
""" unpacks iwparam"""
size = struct_calcsize(self.fmt)
m, e, i, pad = struct_unpack(self.fmt, data[:size])
# XXX well, its not *the* frequency - we need a better name
if e == 0:
return m
else:
return float(m)*10**e
示例12: __init__
def __init__(self, data, range):
"""Initialize the scan result with the access point data"""
self.iwstruct = Iwstruct()
self.range = range
self.bssid = "%02X:%02X:%02X:%02X:%02X:%02X" % struct_unpack('BBBBBB', data[2:8])
self.essid = None
self.mode = None
self.rate = []
self.quality = Iwquality()
self.frequency = None
self.encode = None
self.custom = []
self.protocol = None
示例13: __iter__
def __iter__(self):
while 1:
buf = self.__f.read(8)
if len(buf) < 8:
break
blk_type, blk_len = struct_unpack('<II' if self.__le else '>II', buf)
buf += self.__f.read(blk_len - 8)
if blk_type == PCAPNG_BT_EPB:
epb = EnhancedPacketBlockLE(buf) if self.__le else EnhancedPacketBlock(buf)
ts = self._tsoffset + (((epb.ts_high << 32) | epb.ts_low) / self._divisor)
yield (ts, epb.pkt_data)
示例14: _read
def _read(self, size=1024*30):
raw_chunk_size = self.fileobj.read(4)
if not raw_chunk_size:
return False
chunk_size = struct_unpack(">L", raw_chunk_size)[0]
chunk = snappy_uncompress(self._read_exact(chunk_size))
self.pos += len(chunk)
if self.extrabuf:
self.extrabuf += chunk
else:
self.extrabuf = chunk
return True
示例15: parse_data
def parse_data(self, fmt, data):
""" unpacks raw C data """
size = struct_calcsize(fmt)
idx = self.idx
str = data[idx:idx + size]
self.idx = idx+size
value = struct_unpack(fmt, str)
# take care of a tuple like (int, )
if len(value) == 1:
return value[0]
else:
return value