[EMAIL PROTECTED] wrote: > I need to look at two-byte pairs coming from a machine, and interpret the > meaning based on the relative values of the two bytes. In C I'd use a switch > statement. Python doesn't have such a branching statement. I have 21 > comparisons to make, and that many if/elif/else statements is clunky and > inefficient. Since these data are coming from an OMR scanner at 9600 bps (or > faster if I can reset it programmatically to 38K over the serial cable), I > want a fast algorithm. > > The data are of the form: > > if byte1 == 32 and byte2 == 32: > row_value = 0 > elif byte1 == 36 and byte2 == 32: > row_value = "natural" > ... > elif byte1 == 32 and byte2 == 1: > row_value = 5 > elif byte1 == 66 and byte2 == 32: > row_value = 0.167 > > There are two rows where the marked response equates to a string and 28 > rows where the marked response equates to an integer (1-9) or float of > defined values.
DATA_MAP = { chr(32)+chr(32): 0, chr(36)+chr(32): "natural", ... chr(32)+chr(1): 5, chr(66)+chr(32): 0.167, } ... row_value = DATA_MAP[source.read(2)] # or: row_value = DATA_MAP.get(source.read(2), DEFAULT) </F> -- http://mail.python.org/mailman/listinfo/python-list