From 5930c63e511e7d96869395a5a6eea8600ae41e6c Mon Sep 17 00:00:00 2001 From: jim Date: Sun, 10 May 2015 15:08:30 -0500 Subject: [PATCH] #11 made parser section more readable --- nd2reader/reader.py | 61 +++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/nd2reader/reader.py b/nd2reader/reader.py index 20ab5d2..fecf17f 100644 --- a/nd2reader/reader.py +++ b/nd2reader/reader.py @@ -228,48 +228,49 @@ class Nd2FileReader(object): def read_lv_encoding(self, data, count): data = StringIO(data) - res = {} + metadata = {} total_count = 0 - for c in range(count): - lastpos = data.tell() + for _ in xrange(count): + cursor_position = data.tell() total_count += 1 - hdr = data.read(2) - if not hdr: + header = data.read(2) + if not header: break - typ = ord(hdr[0]) - bname = data.read(2*ord(hdr[1])) - name = bname.decode("utf16")[:-1].encode("utf8") - if typ == 1: + data_type, name_length = map(ord, header) + name = data.read(name_length * 2).decode("utf16")[:-1].encode("utf8") + if data_type == 1: value, = struct.unpack("B", data.read(1)) - elif typ in [2, 3]: + elif data_type in [2, 3]: value, = struct.unpack("I", data.read(4)) - elif typ == 5: + elif data_type == 5: value, = struct.unpack("Q", data.read(8)) - elif typ == 6: + elif data_type == 6: value, = struct.unpack("d", data.read(8)) - elif typ == 8: + elif data_type == 8: value = data.read(2) while value[-2:] != "\x00\x00": value += data.read(2) value = value.decode("utf16")[:-1].encode("utf8") - elif typ == 9: + elif data_type == 9: cnt, = struct.unpack("Q", data.read(8)) value = array.array("B", data.read(cnt)) - elif typ == 11: - newcount, length = struct.unpack("