[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
[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
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
[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
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
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
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
[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/