Re: Cleaning up conditionals

2017-01-05 Thread Paul Rubin
"Deborah Swanson" writes: > I'm still wondering if these 4 lines can be collapsed to one or two > lines. In the trade that's what we call a "code smell", a sign that code (even if it works) should probably be re-thought after taking a step back to understand what it

Re: Cleaning up conditionals

2017-01-03 Thread Paul Rubin
"Deborah Swanson" writes: > I'm still wondering if these 4 lines can be collapsed to one or two > lines. In the trade that's what we call a "code smell", a sign that code (even if it works) should probably be re-thought after taking a step back to understand what it is

Re: Cleaning up conditionals

2017-01-03 Thread Chris Angelico
On Tue, Jan 3, 2017 at 8:18 PM, Deborah Swanson wrote: > OrderedDict is the new development I'd read about, but I think the key > order is just the order keys were added to the dict. The nice thing > about that though is when you loop over a dict you will always get the

RE: Cleaning up conditionals

2017-01-03 Thread Deborah Swanson
Chris Angelico wrote, on January 03, 2017 12:14 AM > > On Tue, Jan 3, 2017 at 6:35 PM, Deborah Swanson > wrote: > > I think I'm right that core python sequences can't be > indexed in any > > fashion by strings, because strings aren't iterable. I suppose it > >

Re: Cleaning up conditionals

2017-01-03 Thread Chris Angelico
On Tue, Jan 3, 2017 at 6:35 PM, Deborah Swanson wrote: > I think I'm right that core python sequences can't be indexed in any > fashion by strings, because strings aren't iterable. I suppose it might > be possible for strings to be iterable in some sort of ascii char

RE: Cleaning up conditionals

2017-01-02 Thread Deborah Swanson
Gregory Ewing wrote, on January 02, 2017 7:58 PM > > Deborah Swanson wrote: > > Unless you know of, and are suggesting, a way to index a > sequence with > > strings instead of integers, so the code could remain > untouched with > > string indices when changes to the columns are made. > > I'm

Re: Cleaning up conditionals

2017-01-02 Thread Gregory Ewing
Deborah Swanson wrote: Unless you know of, and are suggesting, a way to index a sequence with strings instead of integers, so the code could remain untouched with string indices when changes to the columns are made. I'm talking about this:

RE: Cleaning up conditionals

2017-01-02 Thread Deborah Swanson
Gregory Ewing wrote, on Monday, January 02, 2017 3:28 PM > > Deborah Swanson wrote: > > flds = len(ls[0]) > > cl, ur, d1, de, lo, st, mi, ki, re, da, br, no, yn, ma, ar = > > range(0,flds) > > You might like to consider converting the row into a > namedtuple, then you could refer to the fields

Re: Cleaning up conditionals

2017-01-02 Thread Gregory Ewing
Deborah Swanson wrote: flds = len(ls[0]) cl, ur, d1, de, lo, st, mi, ki, re, da, br, no, yn, ma, ar = range(0,flds) You might like to consider converting the row into a namedtuple, then you could refer to the fields using attribute names instead of indexes. You could even use the header row

RE: Cleaning up conditionals

2017-01-02 Thread Deborah Swanson
Dennis Lee Bieber wrote on January 02, 2017 8:30 AM > > On Sun, 1 Jan 2017 18:30:03 -0800, "Deborah Swanson" > declaimed the following: > > Out of curiosity -- don't those listings have street > addresses? There must be lots of "2 br" units in the city. >

RE: Cleaning up conditionals

2017-01-02 Thread Deborah Swanson
Dennis Lee Bieber wrote, on January 02, 2017 8:30 AM: > > On Sun, 1 Jan 2017 18:30:03 -0800, "Deborah Swanson" > declaimed the following: > > >Dennis Lee Bieber wrote, on Sunday, January 01, 2017 6:07 PM > >> > >> > >> l1 2 br, Elk Plains12-26

Re: Cleaning up conditionals

2017-01-02 Thread Antoon Pardon
Op 31-12-16 om 01:26 schreef Deborah Swanson: >> On 31 December 2016 at 10:00, Deborah Swanson >> wrote: >>> Oops, indentation was messed up when I copied it into the email. >> The indentation of your latest message looks completely >> broken now, you can see it

RE: Cleaning up conditionals

2017-01-02 Thread Deborah Swanson
Jussi Piitulainen wrote, on January 02, 2017 1:44 AM > > Deborah Swanson writes: > > Jussi Piitulainen wrote: > > [snip] > > >> With your particular conditions of non-emptiness, which is > taken to > >> be truth, you can achieve variations of this result with > any of the > >> following

Re: Cleaning up conditionals

2017-01-02 Thread Jussi Piitulainen
Deborah Swanson writes: > Jussi Piitulainen wrote: [snip] >> With your particular conditions of non-emptiness, which is taken to >> be truth, you can achieve variations of this result with any of the >> following statements: >> >> w = ( l1[v] if len(l1[v]) > 0 else >> l2[v] if len(l2[v])

RE: Cleaning up conditionals

2017-01-01 Thread Deborah Swanson
Dennis Lee Bieber wrote, on Sunday, January 01, 2017 6:07 PM > > On Sun, 1 Jan 2017 15:50:25 -0800, "Deborah Swanson" > declaimed the following: > > >Maybe it would help if I give a couple rows of (made up) > data to show > >what I mean (first row is field titles):

Re: Cleaning up conditionals

2017-01-01 Thread BartC
On 01/01/2017 14:32, Rustom Mody wrote: On Sunday, January 1, 2017 at 7:51:12 PM UTC+5:30, Rustom Mody wrote: On Sunday, January 1, 2017 at 3:39:14 PM UTC+5:30, Deborah Swanson wrote: Sorry guys. I've read all your responses and I kind of get their general drift, but I'm about four sheets to

Re: Cleaning up conditionals

2017-01-01 Thread Gregory Ewing
On Sat, 31 Dec 2016 14:35:46 -0800, "Deborah Swanson" declaimed the following: if len(l1[v]) == 0 and len(l2[v]) != 0: l1[v] = l2[v] elif len(l2[v]) == 0 and len(l1[v]) != 0: l2[v] = l1[v] elif l1[v] != l2[v]: ret += ",

RE: Cleaning up conditionals

2017-01-01 Thread Deborah Swanson
> -Original Message- > From: Python-list > [mailto:python-list-bounces+python=deborahswanson.net@python.o > rg] On Behalf Of Jussi Piitulainen > Sent: Saturday, December 31, 2016 11:45 PM > To: python-list@python.org > Subject: Re: Cleaning up conditiona

RE: Cleaning up conditionals

2017-01-01 Thread Deborah Swanson
Chris Angelico wrote on Saturday, December 31, 2016 9:39 PM > > On Sun, Jan 1, 2017 at 2:58 PM, Deborah Swanson > wrote: > > I'm not sure I understand what you did here, at least not > well enough > > to try it. > > > > What conditional can I do between the 2 rows

RE: Cleaning up conditionals

2017-01-01 Thread Deborah Swanson
Steve D'Aprano wrote > Sent: Saturday, December 31, 2016 10:25 PM > > On Sun, 1 Jan 2017 02:58 pm, Deborah Swanson wrote: > > >> It's possible to select either l1 or l2 using an > expression, and then > >> subscript that with [v]. However, this does not usually make for > >> readable code, so

Re: Cleaning up conditionals

2017-01-01 Thread Rustom Mody
On Sunday, January 1, 2017 at 7:51:12 PM UTC+5:30, Rustom Mody wrote: > On Sunday, January 1, 2017 at 3:39:14 PM UTC+5:30, Deborah Swanson wrote: > > Sorry guys. I've read all your responses and I kind of get their general > > drift, but I'm about four sheets to the wind right now, and no way

Re: Cleaning up conditionals

2017-01-01 Thread Rustom Mody
On Sunday, January 1, 2017 at 3:39:14 PM UTC+5:30, Deborah Swanson wrote: > Sorry guys. I've read all your responses and I kind of get their general > drift, but I'm about four sheets to the wind right now, and no way would > I make it more than a step or two in playing around with these things >

RE: Cleaning up conditionals

2017-01-01 Thread Deborah Swanson
Sorry guys. I've read all your responses and I kind of get their general drift, but I'm about four sheets to the wind right now, and no way would I make it more than a step or two in playing around with these things and trying to get my head around them before it would all descend into a cacaphony

Re: Cleaning up conditionals

2017-01-01 Thread Jussi Piitulainen
Steve D'Aprano writes: > On Sun, 1 Jan 2017 02:58 pm, Deborah Swanson wrote: > >>> It's possible to select either l1 or l2 using an expression, >>> and then subscript that with [v]. However, this does not >>> usually make for readable code, so I don't recommend it. >>> >>> (l1 if whatever else

Re: Cleaning up conditionals

2016-12-31 Thread Jussi Piitulainen
Deborah Swanson writes: > Jussi Piitulainen wrote: >> Sent: Saturday, December 31, 2016 8:30 AM >> Deborah Swanson writes: >> >> > Is it possible to use some version of the "a = expression1 if >> > condition else expression2" syntax with an elif? And for >> > expression1 and expression2 to be

RE: Cleaning up conditionals

2016-12-31 Thread Steve D'Aprano
On Sun, 1 Jan 2017 02:58 pm, Deborah Swanson wrote: >> It's possible to select either l1 or l2 using an expression, >> and then subscript that with [v]. However, this does not >> usually make for readable code, so I don't recommend it. >> >> (l1 if whatever else l2)[v] = new_value >> >> ChrisA

Re: Cleaning up conditionals

2016-12-31 Thread Chris Angelico
On Sun, Jan 1, 2017 at 2:58 PM, Deborah Swanson wrote: > I'm not sure I understand what you did here, at least not well enough to > try it. > > What conditional can I do between the 2 rows of listings (the list names > l1 and l2) that will give me which row has the

RE: Cleaning up conditionals

2016-12-31 Thread Deborah Swanson
Dennis Lee Bieber > Sent: Saturday, December 31, 2016 7:29 PM > > On Sat, 31 Dec 2016 14:35:46 -0800, "Deborah Swanson" > declaimed the following: > > >Here's the function I have so far: > > > >def comprows(l1,l2,st,ki,no): > >ret = '' > >labels = {st:

RE: Cleaning up conditionals

2016-12-31 Thread Deborah Swanson
Chris Angelico wrote: > Sent: Saturday, December 31, 2016 2:19 PM > > On Sun, Jan 1, 2017 at 9:03 AM, Deborah Swanson > wrote: > > And didn't finish it because I couldn't see what a should > be. I want > > it to be l2[v] if the first clause is true, and l1[v] if >

RE: Cleaning up conditionals

2016-12-31 Thread Peter Otten
Deborah Swanson wrote: > Peter Otten wrote: >> Deborah Swanson wrote: >> >> > Here I have a real mess, in my opinion: >> >> [corrected code:] >> >> > if len(l1[st]) == 0: >> > if len(l2[st]) > 0: >> > l1[st] = l2[st] >> > elif len(l2[st]) == 0: >> >

Re: [SPAM] RE: Cleaning up conditionals

2016-12-31 Thread MRAB
On 2016-12-31 22:35, Deborah Swanson wrote: Peter Otten wrote: Deborah Swanson wrote: > Here I have a real mess, in my opinion: [corrected code:] > if len(l1[st]) == 0: > if len(l2[st]) > 0: > l1[st] = l2[st] > elif len(l2[st]) == 0: >

RE: Cleaning up conditionals

2016-12-31 Thread Deborah Swanson
Peter Otten wrote: > Deborah Swanson wrote: > > > Here I have a real mess, in my opinion: > > [corrected code:] > > > if len(l1[st]) == 0: > > if len(l2[st]) > 0: > > l1[st] = l2[st] > > elif len(l2[st]) == 0: > > if len(l1[st]) > 0: > >

Re: Cleaning up conditionals

2016-12-31 Thread Chris Angelico
On Sun, Jan 1, 2017 at 9:03 AM, Deborah Swanson wrote: > And didn't finish it because I couldn't see what a should be. I want it > to be > l2[v] if the first clause is true, and l1[v] if the second. If I was > computing a value, this would work beautifully, but I don't

RE: Cleaning up conditionals

2016-12-31 Thread Deborah Swanson
Jussi Piitulainen wrote: > Sent: Saturday, December 31, 2016 8:30 AM > Deborah Swanson writes: > > > Is it possible to use some version of the "a = expression1 if > > condition else expression2" syntax with an elif? And for > expression1 > > and expression2 to be single statements? That's the

Re: Cleaning up conditionals

2016-12-31 Thread Tim Chase
On 2016-12-30 19:59, Deborah Swanson wrote: > Similar, actually the same as Cameron suggested. I really need to > revisit testing for empty. I probably rejected it early on for some > bad reason (you don't understand everything that goes wrong when > you're learning). If your data is anything

RE: Cleaning up conditionals

2016-12-31 Thread Deborah Swanson
> From: Tim Chase > Sent: Saturday, December 31, 2016 12:41 PM > On 2016-12-30 19:59, Deborah Swanson wrote: > > Similar, actually the same as Cameron suggested. I really need to > > revisit testing for empty. I probably rejected it early on for some > > bad reason (you don't understand

RE: Cleaning up conditionals

2016-12-31 Thread Deborah Swanson
Jussi Piitulainen wrote: > Sent: Saturday, December 31, 2016 8:30 AM > Deborah Swanson writes: > > > Is it possible to use some version of the "a = expression1 if > > condition else expression2" syntax with an elif? And for > expression1 > > and expression2 to be single statements? That's the

Re: Cleaning up conditionals

2016-12-31 Thread Jussi Piitulainen
Deborah Swanson writes: > Is it possible to use some version of the "a = expression1 if > condition else expression2" syntax with an elif? And for expression1 > and expression2 to be single statements? That's the kind of > shortcutting I'd like to do, and it seems like python might be able to >

Re: Cleaning up conditionals

2016-12-31 Thread Peter Otten
Deborah Swanson wrote: > Here I have a real mess, in my opinion: [corrected code:] > if len(l1[st]) == 0: > if len(l2[st]) > 0: > l1[st] = l2[st] > elif len(l2[st]) == 0: > if len(l1[st]) > 0: > l2[st] = l1[st] > Anybody

RE: Cleaning up conditionals

2016-12-31 Thread Steve D'Aprano
On Sat, 31 Dec 2016 11:26 am, Deborah Swanson wrote: > As Mr. Bieber points out, what I had above greatly benefits from the use > of conjunctions. It now reads: > > if not len(l1[st]) and len(l2[st]): > l1[st] = l2[st] > elif not len(l2[st]) and len(l1[st]): > l2[st] = l1[st] Your code

Re: Cleaning up conditionals

2016-12-30 Thread Jussi Piitulainen
"Deborah Swanson" writes: > Michael Torrie wrote: >> On 12/30/2016 05:26 PM, Deborah Swanson wrote: >> > I'm still wondering if these 4 lines can be collapsed to one or two >> > lines. >> >> If the logic is clearly expressed in the if blocks that you >> have, I

RE: Cleaning up conditionals

2016-12-30 Thread Deborah Swanson
Michael Torrie wrote: > On 12/30/2016 05:26 PM, Deborah Swanson wrote: > > I'm still wondering if these 4 lines can be collapsed to one or two > > lines. > > If the logic is clearly expressed in the if blocks that you > have, I don't see why collapsing an if block into one or two > lines would

RE: Cleaning up conditionals

2016-12-30 Thread Deborah Swanson
> On 2016-12-31 00:48, Deborah Swanson wrote: > >> On 30/12/16 23:00, Deborah Swanson wrote: > >> > Oops, indentation was messed up when I copied it into the email. > >> > Should be this: > >> > > >> > if len(l1[st]) == 0: > >> > if len(l2[st]) > 0: > >> >

RE: Cleaning up conditionals

2016-12-30 Thread Deborah Swanson
> On 30Dec2016 15:17, Deborah Swanson wrote: > >>Ever consider using conjunctions? > >> > >>if len(l1[st]) and not len(l2[st]): > >>#0 is considered a false -- no need to test for "==0" > >>#non-0 is considered true -- no need to test for

Re: Re: Cleaning up conditionals

2016-12-30 Thread MRAB
On 2016-12-31 01:59, Cameron Simpson wrote: On 30Dec2016 15:17, Deborah Swanson wrote: Ever consider using conjunctions? if len(l1[st]) and not len(l2[st]): #0 is considered a false -- no need to test for "==0" #non-0

Re: Cleaning up conditionals

2016-12-30 Thread Cameron Simpson
On 30Dec2016 15:17, Deborah Swanson wrote: Ever consider using conjunctions? if len(l1[st]) and not len(l2[st]): #0 is considered a false -- no need to test for "==0" #non-0 is considered true -- no need to test for

Re: Cleaning up conditionals

2016-12-30 Thread MRAB
On 2016-12-31 00:48, Deborah Swanson wrote: On 30/12/16 23:00, Deborah Swanson wrote: > Oops, indentation was messed up when I copied it into the email. > Should be this: > >if len(l1[st]) == 0: > if len(l2[st]) > 0: > l1[st] = l2[st] >

Re: Cleaning up conditionals

2016-12-30 Thread Michael Torrie
On 12/30/2016 05:26 PM, Deborah Swanson wrote: > I'm still wondering if these 4 lines can be collapsed to one or two > lines. If the logic is clearly expressed in the if blocks that you have, I don't see why collapsing an if block into one or two lines would even be desirable. Making a clever

RE: Cleaning up conditionals

2016-12-30 Thread Deborah Swanson
> On 31/12/16 00:26, Deborah Swanson wrote: > > As Mr. Bieber points out, what I had above greatly benefits > from the > > use of conjunctions. It now reads: > > > > if not len(l1[st]) and len(l2[st]): > > IMHO, "if not len(l)" is a _terrible_ way of spelling "if > len(l) == 0" > (mentally, I

RE: Re: Cleaning up conditionals

2016-12-30 Thread Deborah Swanson
> On 30/12/16 23:00, Deborah Swanson wrote: > > Oops, indentation was messed up when I copied it into the email. > > Should be this: > > > > if len(l1[st]) == 0: > > if len(l2[st]) > 0: > > l1[st] = l2[st] > > elif len(l2[st]) == 0: > >

Re: Cleaning up conditionals

2016-12-30 Thread Erik
On 31/12/16 00:26, Deborah Swanson wrote: As Mr. Bieber points out, what I had above greatly benefits from the use of conjunctions. It now reads: if not len(l1[st]) and len(l2[st]): IMHO, "if not len(l)" is a _terrible_ way of spelling "if len(l) == 0" (mentally, I have to read that as "if

RE: Cleaning up conditionals

2016-12-30 Thread Deborah Swanson
> On 31 December 2016 at 10:00, Deborah Swanson > wrote: > > > > Oops, indentation was messed up when I copied it into the email. > > The indentation of your latest message looks completely > broken now, you can see it here: >

Re: Cleaning up conditionals

2016-12-30 Thread Erik
On 30/12/16 23:00, Deborah Swanson wrote: Oops, indentation was messed up when I copied it into the email. Should be this: if len(l1[st]) == 0: if len(l2[st]) > 0: l1[st] = l2[st] elif len(l2[st]) == 0: if

RE: Cleaning up conditionals

2016-12-30 Thread Deborah Swanson
> On Fri, 30 Dec 2016 13:20:15 -0800, "Deborah Swanson" > declaimed the following: > > >I've already learned one neat trick to collapse a conditional: > > > > a = expression1 if condition else expression2 > > > >Here I have a real mess, in my opinion: > > > >

Re: Cleaning up conditionals

2016-12-30 Thread David
On 31 December 2016 at 10:00, Deborah Swanson wrote: > > Oops, indentation was messed up when I copied it into the email. The indentation of your latest message looks completely broken now, you can see it here:

RE: Cleaning up conditionals

2016-12-30 Thread Deborah Swanson
BartC wrote: > Sent: Friday, December 30, 2016 2:11 PM > > On 30/12/2016 21:20, Deborah Swanson wrote: > > I've already learned one neat trick to collapse a conditional: > > > > a = expression1 if condition else expression2 > > > > Here I have a real mess, in my opinion: > > > > if

Re: Cleaning up conditionals

2016-12-30 Thread BartC
On 30/12/2016 21:20, Deborah Swanson wrote: I've already learned one neat trick to collapse a conditional: a = expression1 if condition else expression2 Here I have a real mess, in my opinion: if len(l1[st]) == 0: if len(l2[st]) > 0: l1[st] = l2[st]

Cleaning up conditionals

2016-12-30 Thread Deborah Swanson
I've already learned one neat trick to collapse a conditional: a = expression1 if condition else expression2 Here I have a real mess, in my opinion: if len(l1[st]) == 0: if len(l2[st]) > 0: l1[st] = l2[st] elif len(l2[st]) == 0: if