From 8a0eeb9d3b3c2e7b1549a9fbf0c960ac1f9bae89 Mon Sep 17 00:00:00 2001 From: Anders Blomdell <anders.blomdell@control.lth.se> Date: Tue, 13 Aug 2013 10:31:56 +0200 Subject: [PATCH] Allow tuples and other iterables for arrays in python. Fixed python encode_byte to be unsigned. --- lib/python/labcomm/LabComm.py | 20 +++++++++++++++----- test/test_encoder_decoder.py | 2 +- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/python/labcomm/LabComm.py b/lib/python/labcomm/LabComm.py index 2a66397..6fa3ca8 100644 --- a/lib/python/labcomm/LabComm.py +++ b/lib/python/labcomm/LabComm.py @@ -93,6 +93,7 @@ # sequences of 7 bit chunks, represented in bytes with the high bit meaning # that more data is to come. +import types import struct as packer VERSION = "LabComm2013" @@ -316,18 +317,26 @@ class array(object): encoder.encode_packed32(i) encoder.encode_type_number(self.decl) - def min_max_shape(self, l, depth=0, shape=[]): - if isinstance(l, list): + def min_max_shape(self, l, depth, shape): + if isinstance(l, types.StringTypes): + return shape + try: length = len(l) if len(shape) <= depth: shape.append((length, length)) + pass else: (low, high) = shape[depth] low = min(low, length) high = max(high, length) shape[depth] = (low, high) + pass for e in l: shape = self.min_max_shape(e, depth + 1, shape) + pass + pass + except TypeError: + pass return shape def shape(self, l): @@ -356,7 +365,8 @@ class array(object): return depth def encode_value(self, encoder, value, depth): - if depth and isinstance(value, list): + # if depth and isinstance(value, list): + if depth: for e in value: self.encode_value(encoder, e, depth - 1) else: @@ -585,7 +595,7 @@ class Encoder(Codec): self.pack("!b", 0) def encode_byte(self, v): - self.pack("!b", v) + self.pack("!B", v) def encode_short(self, v): self.pack("!h", v) @@ -669,7 +679,7 @@ class Decoder(Codec): return self.unpack("!b") != 0 def decode_byte(self): - return self.unpack("!b") + return self.unpack("!B") def decode_short(self): return self.unpack("!h") diff --git a/test/test_encoder_decoder.py b/test/test_encoder_decoder.py index acd08e5..3818ab4 100755 --- a/test/test_encoder_decoder.py +++ b/test/test_encoder_decoder.py @@ -59,7 +59,7 @@ def generate(decl): return [False, True] elif decl.__class__ == labcomm.BYTE: - return [-128, 0, 127] + return [0, 127, 128, 255] elif decl.__class__ == labcomm.SHORT: return [-32768, 0, 32767] -- GitLab