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.
Suggestions appreciated.
Rich
--
Richard B. Shepard, Ph.D. | Author of "Quantifying Environmental
Applied Ecosystem Services, Inc. (TM) | Impact Assessments Using Fuzzy Logic"
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863 8 1762 rs******@appl-ecosys.com 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>
On 8 Jan 2006 18:59:28 GMT, rs******@salmo.appl-ecosys.com 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.
Suggestions appreciated.
Set up a dict to map your byte pairs to values, e.g., pairvalues = dict([
... ((32,32), 0),
... ((36,32), "natural"),
... # ...
... ((32, 1), 5),
... ((66,32), 0.167)
... ])
Then you can access the values like:
row_value = pairvalues.get((36,32), '<default>') row_value
'natural'
The .get method allows you to specify a default, in case you get an unexpected pair.
You could also use pairvalues[(byte1,byte2)] and catch the KeyError exception for
unexpected pairs.
for byte1, byte2 in (66,32), (32,1), (36,32), (32,32), (20,20):
... print '%10s => %r' %((byte1,byte2), pairvalues.get((byte1,byte2), 'DEFAULT ??'))
...
(66, 32) => 0.16700000000000001
(32, 1) => 5
(36, 32) => 'natural'
(32, 32) => 0
(20, 20) => 'DEFAULT ??'
HTH
Regards,
Bengt Richter
On Sun, 8 Jan 2006 20:31:49 +0100, "Fredrik Lundh" <fr*****@pythonware.com> wrote: rs******@appl-ecosys.com 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)
Much better than my version, since you went beyond the OP's code to what he said
he was trying to do ("look at two-byte pairs coming from a machine ... over the serial cable").
I just went for a direct translation of the code, which is nearly always a mistake.
I should know better. I guess I do, since I always rant about requirements ;-/
Also don't know why I chose to use dict([]) vs {}, since there's no bare key names to
clean the quotes off of. Oh well.
Regards,
Bengt Richter
On 2006-01-08, Fredrik Lundh <fr*****@pythonware.com> 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), DEFAULT)
Thank you, Fredrik. That's ideal, and a great lesson for a newcomer to
Python.
Rich
--
Richard B. Shepard, Ph.D. | Author of "Quantifying Environmental
Applied Ecosystem Services, Inc. (TM) | Impact Assessments Using Fuzzy Logic"
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863 rs******@salmo.appl-ecosys.com 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 "fast" today - given a modern CPU (or even one from a few
years back), 21 (or 42) short string comparisons in an interprated
language should be trivially fast.
(I'm not saying that using a different approach such as dictionaries
isn't good because of other reasons :) )
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 has to be defined just one time.
Bye,
bearophile
rshep...@salmo.appl-ecosys.com 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.
Suggestions appreciated.
Rich
-- Richard B. Shepard, Ph.D. | Author of "Quantifying Environmental Applied Ecosystem Services, Inc. (TM) | Impact Assessments Using Fuzzy Logic" <http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863
Use a dictionary:
byte_values = {
(32,32) : 0,
(36,32) : 'natural',
(32,1 ) : 5,
}
row_value = byte_values[byte1,byte2]
--
- Justin
rshep...@salmo.appl-ecosys.com 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.
Suggestions appreciated.
Rich
-- Richard B. Shepard, Ph.D. | Author of "Quantifying Environmental Applied Ecosystem Services, Inc. (TM) | Impact Assessments Using Fuzzy Logic" <http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863
Use a dictionary:
byte_values = {
(32,32) : 0,
(36,32) : 'natural',
(32,1 ) : 5,
}
row_value = byte_values[byte1,byte2]
--
- Justin This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ralf Wahner |
last post by:
Dear Masters of XSLT
Could I ask you for a clue on the following question? I'd
like to use XSLT to transform an XML source file to LaTeX.
In the following small example the <para> Element...
|
by: Ruthless |
last post by:
hello.
All XML and XSLT are processed by preprocessor as a trees.
How can i simply display my XML as some kind of tree.
given xml:
<struct>
<node level="1" no="1">
|
by: JT |
last post by:
We have an exception handler which needs some tweaking because when we
changed compilers, the embedded assembly didn't work. Ideally, we
would like a complete C++ solution, without assembly...
|
by: BH |
last post by:
Is there a way to display different message based on the value of a
particualr column bound to a datagrid or datalist. For example, I have the
following column from the bound DataTable:
<%#...
|
by: Matze |
last post by:
I have setup Visual Studio to work with a VSS-Server. (Source Off
Site)
I dont see a way to share and later branch my project.
File -> Source Control -> Share is grayed out.
What is the best way...
|
by: Russell Shaw |
last post by:
Hi,
I'm making my first database.
I have a list of parts, each of which is sold by multiple
vendors. I also have a list of vendors, each of which sell
multiple parts.
How should i arrange...
|
by: mouton |
last post by:
Hello,
I am a beginner. My website has 2 pages: one in English and one in
French. I would like non English people to be directed to the English
page. There I would like to display a...
|
by: hamdan |
last post by:
how one can make logic branching decision in C & C++ based on timer functions. An example of this is in the following simple algorithm:
if ( time elapsed = 10 sec )
do so & so or start...
|
by: pbmods |
last post by:
While researching SCM software, I came across Git . It looked phenomenal, especially the part about branching. Or at least, the way I thought it handled branching looked phenomenal.
I'm curious...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |