Re: Multiway Branching

2006-01-09 Thread Justin Azoff
[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/els

Re: Multiway Branching

2006-01-09 Thread Justin Azoff
[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/els

Re: Multiway Branching

2006-01-09 Thread bearophileHUGS
A dict can be useful: byte1, byte2 = 32, 1 conv1 = {(32, 32):0, (36,32):"natural", (32,1):5, (66,32):0.167} print conv1[byte1, byte2] If you use Psyco maybe something like this can be faster: conv2 = dict((k1*256+k2,v) for (k1,k2),v in conv1.items()) print conv2[(byte1<<8) + byte2] conv1/conv2

Re: Multiway Branching

2006-01-08 Thread Ivan Voras
[EMAIL PROTECTED] wrote: > 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. It depends on your actual environment, of course, but 38kbps is usually not considered "fa

Re: Multiway Branching

2006-01-08 Thread rshepard
On 2006-01-08, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > 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), DE

Re: Multiway Branching

2006-01-08 Thread Bengt Richter
On Sun, 8 Jan 2006 20:31:49 +0100, "Fredrik Lundh" <[EMAIL PROTECTED]> wrote: >[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

Re: Multiway Branching

2006-01-08 Thread Bengt Richter
On 8 Jan 2006 18:59:28 GMT, [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

Re: Multiway Branching

2006-01-08 Thread Fredrik Lundh
[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/