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 inef

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

Embedded For Loop With No Data

2007-07-05 Thread rshepard
Haven't found an answer to my question in the books and other docs I have available, so I am asking here. I have three lists of data retrieved from database tables. I want to cycle through all three lists using nested FOR loops. What is the behavior if there are no data in the list used in the

Importing Module To Use Variables In A Second Module

2007-09-27 Thread rshepard
I'm stymied by what should be a simple Python task: accessing the value of a variable assigned in one module from within a second module. I wonder if someone here can help clarify my thinking. I've re-read Chapter 16 (Module Basics) in Lutz and Ascher's "Learning Python" but it's not working for

Re: Importing Module To Use Variables In A Second Module

2007-09-27 Thread rshepard
On 2007-09-27, Steve Holden <[EMAIL PROTECTED]> wrote: > Self-evidently you are *not* creating the variables you think you are in > the variablePage module. Have you tried an interactive test? Try this at > the interpreter prompt: > > >>> import variablePage > >>> dir(variablePage) > > and you wil

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 com

'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-er

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

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:

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 tupl

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() >

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.or

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

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.9

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 bef

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 mi

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 retrie

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