Mercurial > ~darius > hgwebdir.cgi > pyinst
diff scpi.py @ 73:ca5a822c550a
Fix for Python 3
Add getbin function to read large non-arrat data blocks (eg screen capture)
author | Daniel O'Connor <doconnor@gsoft.com.au> |
---|---|
date | Wed, 25 Sep 2024 20:57:26 +0930 |
parents | 00800345fbae |
children |
line wrap: on
line diff
--- a/scpi.py Wed Sep 25 20:56:39 2024 +0930 +++ b/scpi.py Wed Sep 25 20:57:26 2024 +0930 @@ -28,8 +28,8 @@ import numpy import re -headerre = re.compile('^(:[A-Z:]+ ).*') -insturlre = re.compile('([a-zA-Z0-9]+)://(([^:]+)?(:([^/]+)@))?([^:]+)(:(.+))?') +headerre = re.compile(b'^(:[A-Z:]+ ).*') +insturlre = re.compile(b'([a-zA-Z0-9]+)://(([^:]+)?(:([^/]+)@))?([^:]+)(:(.+))?') def query(d, q, timeout = None): d.write(q) @@ -49,29 +49,34 @@ data = clean(data) return dtype(data) -def bindecode(data, dtype = numpy.float32): - '''Decode binary data, returns numpy array''' - +def getbin(data): + '''Fetch binary blob, strips ASCII length header and checks''' data = clean(data) - if data[0] != '#': + if data[0] != ord('#'): raise ValueError('data length header incorrect') - dlenlen = int(data[1]) + dlenlen = int(chr(data[1])) if dlenlen == 0: return numpy.array([]) - + dlen = int(data[2:dlenlen + 2]) if dlen == 0: - return numpy.array([]) + return b'' wanted = dlen + 2 + dlenlen # Chop off the trailing \n if necessary if len(data) == wanted + 1 and data[-1] == '\n': data = data[0:-1] - + if len(data) != wanted: raise ValueError('length mismatch, header says %d, actually got %d bytes' % (wanted, len(data))) - return numpy.frombuffer(data[2 + dlenlen:], dtype = dtype) + + return data[2 + dlenlen:] + +def bindecode(data, dtype = numpy.float32): + '''Decode binary data, returns numpy array''' + + return numpy.frombuffer(getbin(data), dtype = dtype) def ascdecode(data, dtype = numpy.float32, sep = None): '''Decode ASCII data, returns numpy array'''