Multiway Branching

2006-01-08 Thread rshepard
  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"
 Voice: 503-667-4517 Fax: 503-667-8863
-- 
http://mail.python.org/mailman/listinfo/python-list


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), 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"
 Voice: 503-667-4517 Fax: 503-667-8863
-- 
http://mail.python.org/mailman/listinfo/python-list


Running Application Within Emacs

2007-02-07 Thread rshepard
  My editor is emacs in linux, and I have the python mode enabled. The two
menus -- IM-Python and Python -- allow me to navigate within the loaded
module and open execute buffers, among other things. But, I don't see a way
to run a wxPython application from within the editor as I would from the
command line.

  Is this possible? If so, how?

Ric
-- 
http://mail.python.org/mailman/listinfo/python-list


'IF' Syntax For Alternative Conditions

2007-02-07 Thread rshepard
  All my python books and references I find on the web have simplistic
examples of the IF conditional. A few also provide examples of multiple
conditions that are ANDed; e.g.,
if cond1:
if cond2:
do_something.

  However, I cannot find, nor create by trial-and-error, the syntax for
alternative conditions that are ORed; e.g.,

if cond1 OR if cond2:
do_something.

  I've tried using the C syntax for OR (||) but python complained. I'm sure
there's a way to do this rather than using if cond1: elif cond2: both with
the same code to execute.

  Please pass me a pointer so I can learn how to correctly write this.

Rich
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 'IF' Syntax For Alternative Conditions

2007-02-08 Thread rshepard
On 2007-02-08, Paul Rubin  wrote:
> [EMAIL PROTECTED] writes:
>>  if cond1:
>>  if cond2:
>>  do_something.
>
> You can write:
>if cond1 and cond2:
>   do_something
>
>>  if cond1 OR if cond2:
>>  do_something.
>
> if cond1 or cond2:
>do_something
>
>>   I've tried using the C syntax for OR (||) but python complained. I'm sure
>> there's a way to do this rather than using if cond1: elif cond2: both with
>> the same code to execute.
>
> Python uses the "and" and "or" keywords for && and ||.

  Allow me to thank all of you who responded with this one article. For
whatever reason, it did not occur to me to use the words 'and' and 'or.'
And, I did not see this in the tutorial or introduction ... which is my
fault.

  So, I do thank all of you.

Rich
-- 
http://mail.python.org/mailman/listinfo/python-list


Matching Strings

2007-02-09 Thread rshepard
  I'm not sure how to change a string so that it matches another one.

  My application (using wxPython and SQLite3 via pysqlite2) needs to compare
a string selected from the database into a list of tuples with another
string selected in a display widget.

  An extract of the relevant code is:

selName = self.polTree.GetItemText(selID)
...  
for item in self.appData.polNat:
  print 'Item: ', item, '\n', 'selName: ', selName, '\n'
  if item == selName:
print '* ', self.appData.polNat[1]

  The last comparison and print statement never work because the strings are
presented this way:

Item:  (u'ground water',) 
selName:  ground water

  What do I need to do to 'Item' to strip the parentheses, unicode symbol,
single quotes, and comma? Do I want 'raw' output? If so, how do I specify
that in the line 'if item == selName:'?

TIA,

Rich
-- 
http://mail.python.org/mailman/listinfo/python-list


Referencing Items in a List of Tuples

2007-02-24 Thread rshepard
  While working with lists of tuples is probably very common, none of my
five Python books or a Google search tell me how to refer to specific items
in each tuple. I find references to sorting a list of tuples, but not
extracting tuples based on their content.

  In my case, I have a list of 9 tuples. Each tuple has 30 items. The first
two items are 3-character strings, the remaining 28 itmes are floats.

  I want to create a new list from each tuple. But, I want the selection of
tuples, and their assignment to the new list, to be based on the values of
the first two items in each tuple.

  If I try, for example, writing:

for item in mainlist:
if mainlist[item][0] == 'eco' and mainlist[item][1] == 'con':
  ec.Append(mainlist[item][2:])

python doesn't like a non-numeric index.

  I would really appreciate a pointer so I can learn how to manipulate lists
of tuples by referencing specific items in each tuple (string or float).

Rich
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Referencing Items in a List of Tuples

2007-02-25 Thread rshepard
On 2007-02-25, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:

>   Item is ALREADY the "current" tuple...
>
> for tpl in mainlist:
>   if tpl[0] == "eco" and tpl[1] == "con":
>   ec.Append(tpl[2:])  #presuming ec is NOT a list, as Append()
>   #is not a list method 
> (append() is)

  Of course! As a morning person I don't do as well at night. That's such a
silly error I'm embarrassed by having to have the obvious pointed out to me.
My apologies to all.

  And, the .Append comes from list widgets in wxPython.

  Thank you all for forcing my head straight again.

Rich
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Referencing Items in a List of Tuples

2007-02-25 Thread rshepard
On 2007-02-25, Paddy <[EMAIL PROTECTED]> wrote:

> You might also use list comprehensions to accumulate the values you
> need:
>
> ec = [ item[2:]  for item in mainlist  if item[:2] == ['eco','con'] ]

  Thank you, Paddy. That's the syntax I couldn't work out myself.

Rich
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Referencing Items in a List of Tuples

2007-02-25 Thread rshepard
On 2007-02-25, Jussi Salmela <[EMAIL PROTECTED]> wrote:

> I'm nitpicking, but the OP has a list of tuples:
> ec = [ item[2:]  for item in mainlist  if item[:2] == ('eco','con') ]

Jussi,

  An excellent nit to pick.

Thank you,

Rich
-- 
http://mail.python.org/mailman/listinfo/python-list


Lists: Converting Double to Single

2007-02-26 Thread rshepard
  I start with a list of tuples retrieved from a database table. These
tuples are extracted and put into individual lists. So I have lists that
look like this: [1, 2, 3, 4, 5]. When I concatenate lists, I end up with a
list of lists that looks like this: [[1, 2, 3. 4, 5]. [6, 7. 8, 9. 10]].
Then, I average the column values so I end up with a single list, but with
two brackets on each end, for example, [[3.5, 4.5, 5.5, 6.5, 7.5]].

  Unfortunately, when I try to use that last list in a NumPy function, I'm
told that it cannot be broadcast to the correct shape. So, what I want to do
is strip the extra brackes from each end to leave just [3.5, 4.5, 5.5, 6.5,
7.5].

  How do I do this, please?

Rich
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Lists: Converting Double to Single

2007-02-26 Thread rshepard
On 2007-02-27, Leif K-Brooks <[EMAIL PROTECTED]> wrote:

Lief, Bjoern:

> l = l[0]

  Of course! If I had let it work in my mind overnight I would almost
certainly have seen this.

Thank you both for your patient responses,

Rich
-- 
http://mail.python.org/mailman/listinfo/python-list


Tuples from List

2007-02-27 Thread rshepard
  While it should be easy for me to get what I need from a list, it's
proving to be more difficult than I expected.

  I start with this list:

[  6.24249034e-01+0.j   5.11335982e-01+0.j   3.67333773e-01+0.j
   3.01189122e-01+0.j   2.43449050e-01+0.j   1.82948476e-01+0.j
   1.43655139e-01+0.j   9.91225725e-02+0.j]

and I want a list of floats of only the first 6 digits for each value. If I
write:
for i in listname:
print i

I get this:

(0.624249034424+0j)
(0.511335982206+0j)
(0.367333773283+0j)
(0.301189121704+0j)
(0.243449050439+0j)
(0.182948475822+0j)
(0.14365513894+0j)
(0.0991225725344+0j)

  I know it's embarrassingly simple, but the correct syntax eludes my
inexperienced mind. What I want is a list [0.62424, 0.51133, ...] so that I
can normalize those values.

  What is the correct syntax, please?

Rich
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tuples from List

2007-02-27 Thread rshepard
On 2007-02-28, Robert Kern <[EMAIL PROTECTED]> wrote:

> No, that's a numpy array.

Robert,

  That's where I went off. I forgot that I'm still dealing with a 1D NumPy
array and not a list. No wonder I had such fits!

> Those aren't tuples, but complex numbers.

  I have not seen the 'j' suffix before. That was throwing me.

> # Extract the real components (since the imaginary components are all 0):
> eigvals = eigvals.real

  That's so much easier than what I ended up doing, which was creating
another variable and assigning to that an explicit cast to real of the
array.

> # Normalize the eigenvalues:
> eigvals /= eigvals.sum()

  Now that's really nice!

Thank you very much for today's lessons. I really appreciate them

Rich
-- 
http://mail.python.org/mailman/listinfo/python-list


Extract String From Enclosing Tuple

2007-02-28 Thread rshepard
  I'm a bit embarrassed to have to ask for help on this, but I'm not finding
the solution in the docs I have here.

  Data are assembled for writing to a database table. A representative tuple
looks like this:

('eco', "(u'Roads',)", 0.073969887301348305)

Pysqlite doesn't like the format of the middle term:
pysqlite2.dbapi2.InterfaceError: Error binding parameter 1 - probably
unsupported type.

  I want to extract the 'Roads', part from the double-quoted enclosing
tuple. The unicode part should be automatically removed when the string is
printed, but I suspect it's the double quotes and extra parentheses that are
the problem. I know that tuples are immutable, but I thought that I could
slice it. If so, I'm not doing it correctly, because each attempt results in
  TypeError: unsubscriptable object

  Even when I assign that middle term to a variable before assembling the
tuple for insertion in the database, I just cannot get the job done. Whether
in the tuple of three terms or by itself, I haven't applied the proper
technique.

  Insight appreciated.

Rich
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Extract String From Enclosing Tuple

2007-03-01 Thread rshepard
On 2007-02-28, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:

 import itertools
 tuple(itertools.chain((t[0], t2[0].encode('ascii')), t[2:]))
> ('eco', 'Roads', 0.073969887301348305)

Steven,

  As suggested in the previous article, I handled it where the values are
read from the list retrieved from the database. By adding an additional
index of [0] the format is correct.

Thank you all very much,

Rich
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Matching Strings

2007-02-09 Thread rshepard-at-appl-ecosys . com
On 2007-02-10, [EMAIL PROTECTED] wrote:

>   if item == selName:

  Slicing doesn't seem to do anything -- if I've done it correctly. I
changed the above to read,

if item[2:-2] == selName:

but the output's the same.

Rich
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Matching Strings

2007-02-09 Thread rshepard-at-appl-ecosys . com
On 2007-02-10, James Stroud <[EMAIL PROTECTED]> wrote:

> Assuming item is "(u'ground water',)"
>
> import re
> item = re.compile(r"\(u'([^']*)',\)").search(item).group(1)

James,

  I solved the problem when some experimentation reminded me that 'item' is
a list index and not a string variable. by changing the line to,

if item[0] == selName:

I get the matchs correctly.

  Now I need to extract the proper matching strings from the list of tuples,
and I'm working on that.

Many thanks,

Rich
-- 
http://mail.python.org/mailman/listinfo/python-list