HP3562A/__init__.py

changeset 15
b930440af354
parent 14
07e2cbf140df
child 16
de9122b5680a
equal deleted inserted replaced
14:07e2cbf140df 15:b930440af354
11 Constants 11 Constants
12 --------- 12 ---------
13 13
14 """ 14 """
15 import struct 15 import struct
16 import re
16 import gpib 17 import gpib
17 18
18 ######################################## 19 ########################################
19 # HP3562A internal binary types decoders 20 # HP3562A internal binary types decoders
20 21
50 # Some useful functions 51 # Some useful functions
51 def format_header(header, head_struct, columns=80): 52 def format_header(header, head_struct, columns=80):
52 """ 53 """
53 Pretty print a data block (trace, state or coord) 54 Pretty print a data block (trace, state or coord)
54 """ 55 """
56 bool_re = re.compile(r'((?P<before>.*) )?(?P<flag>\w+/\w+)( (?P<after>.*))?')
57
55 todisp = [] 58 todisp = []
56 for row in head_struct: 59 for row in head_struct:
57 key = row[0] 60 key = row[0]
61 typ = row[1]
58 val = header.get(key, "N/A") 62 val = header.get(key, "N/A")
59 if isinstance(val, basestring): 63 if isinstance(val, basestring):
60 val = repr(val) 64 val = repr(val)
61 else: 65 elif typ is bool and isinstance(val, typ):
66 m = bool_re.match(key)
67 if m:
68 d = m.groupdict()
69 key = ""
70 if d['before']:
71 key += d['before']
72 if d['after']:
73 key += d['after']
74 key = key.capitalize()
75 val = d['flag'].split('/')[not val]
76 else:
77 val = str(val)
78 else:
62 val = str(val) 79 val = str(val)
63 todisp.append((key+":", val)) 80 todisp.append((key+":", val))
64 maxk = max([len(k) for k, v in todisp]) 81 maxk = max([len(k) for k, v in todisp])
65 maxv = max([len(v) for k, v in todisp]) 82 maxv = max([len(v) for k, v in todisp])
66 fmt = "%%-%ds %%-%ds"%(maxk, maxv) 83 fmt = "%%-%ds %%-%ds"%(maxk, maxv)

mercurial