Re: Loop with else clause

2019-02-05 Thread Ben Finney
DL Neil  writes:

> Possible solution:
> To make anything more than the trivial case readable, I think I'd put
> the list processing into one function, and the exception into another
> (except that this case is so trivial), ie
>
>   if list:
>   process_list() #the heading and for-loop, as above
>   else:
>   print( "Sorry...

(As an aside: It's best to avoid choosing names like ‘list’ that clobber
built-in names; your code examples are harder to read that way. I'll
assume a different name, ‘ipsums’.)

One aspect of that example I would prefer to avoid: It scatters the
handling of the list to different locations in the code. It's not
obvious from the purpose of ‘process_list’ whether that function should
be handling an empty list; this could lead to double-handling in
different locations.

An alternative to consider::

if ipsums:
for item in ipsums:
process_item(item)
else:
print("Sorry...")

An advantage of this is that the handling of the list is all in the same
place, where changing that logic later will be easier. The
‘process_item’ then just assumes some other code has decided which items
to handle; it becomes correspondingly simpler.

-- 
 \   “Two possibilities exist: Either we are alone in the Universe |
  `\   or we are not. Both are equally terrifying.” —Arthur C. Clarke, |
_o__) 1999 |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Loop with else clause

2019-02-05 Thread Peter Otten
DL Neil wrote:

> What is the pythonic way to handle the situation where if a condition
> exists the loop should be executed, but if it does not something else
> should be done?

> Possible solution:
> To make anything more than the trivial case readable, I think I'd put
> the list processing into one function, and the exception into another
> (except that this case is so trivial), ie
> 
> if list:
> process_list() #the heading and for-loop, as above
> else:
> print( "Sorry...
> 
> 
> Others wanted to add a semaphore/flag inside the loop to indicate if it
> was executed at least once. Yes, could even use the else clause then!

An argument in favour of the flag is that it works with arbitrary iterables 
whereas if ...: fails:

>>> numbered = enumerate([])
>>> if numbered:
... print("the winners are")
... for ix in numbered: print(*ix)
... 
the winners are
 
> The ideas went (rapidly) down-hill from there...
> 
> 
> Is there another, more pythonic, approach to conditional (for/while)
> loop processing?

I'm not aware of such an approach.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python is going to die! =(

2019-02-05 Thread mojimo2000
python is alive
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python is going to die! =(

2019-02-05 Thread Prahallad Achar
Python is the only immortal

On Tue, 5 Feb 2019, 17:01  python is alive
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python program to phone?

2019-02-05 Thread Abdur-Rahmaan Janhangeer
see pydroid and qpython on play store.2

Abdur-Rahmaan Janhangeer
http://www.pythonmembers.club | https://github.com/Abdur-rahmaanJ
Mauritius
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python program to phone?

2019-02-05 Thread Rhodri James

On 04/02/2019 23:12, Chris Roy-Smith wrote:

On 5/2/19 7:20 am, Steve wrote:

P.S.  Yes, I tried to post this about two weeks ago but could not seem to
respond to the replies I received.  I could contact one or two 
individuals

but apparently not the masses.  How do I find out how this list works?


Hi Steve, to reply to the newsgroup use "followup" NOT "reply"


If you're using the mailing list rather than the newsgroup, reply to the 
list (python-list@python.org) instead of the Reply-To address.  Most 
mail programs have an appropriate reply option: on Thunderbird it's the 
"Reply List" button.



Sorry I can't help you on your initial problem


Likewise :-(

--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python program to phone?

2019-02-05 Thread Mario R. Osorio


Hi there Steve. Did you check BeeWare? (https://pybee.org/)

-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Loop with else clause

2019-02-05 Thread David Raymond
As mentioned somewhere, "readability counts", so why not just go with exactly 
what you said...

if len(nominees) > 0:#if a condition exists
for nominee in nominees: #the loop should be executed
print(nominee)
else:#but if not
print("D'oh! No one is worthy.") #something else should be done


The fancy features of the language can be cool and all, but forcing yourself to 
use them just for the sake of using them and "being pythonic" can result in 
weird situations, and wrong or unclear assumptions or intents. When someone 
else reads it later it may hide what you were actually intending the check to 
look for.

Hmm, "if nominees:" Did they make it like that to look for a 0 length list, or 
to make sure they didn't get None by accident, or both, or something different?

Hmm, "if len(nominees) > 0:" Ahh ok, we want to make sure there's something in 
there.

So my 2 cents: Feel free to be non-pythonic if it makes things clear and works.


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Peter Otten
Sent: Tuesday, February 05, 2019 3:44 AM
To: python-list@python.org
Subject: Re: Loop with else clause

DL Neil wrote:

> What is the pythonic way to handle the situation where if a condition
> exists the loop should be executed, but if it does not something else
> should be done?

> Possible solution:
> To make anything more than the trivial case readable, I think I'd put
> the list processing into one function, and the exception into another
> (except that this case is so trivial), ie
> 
> if list:
> process_list() #the heading and for-loop, as above
> else:
> print( "Sorry...
> 
> 
> Others wanted to add a semaphore/flag inside the loop to indicate if it
> was executed at least once. Yes, could even use the else clause then!

An argument in favour of the flag is that it works with arbitrary iterables 
whereas if ...: fails:

>>> numbered = enumerate([])
>>> if numbered:
... print("the winners are")
... for ix in numbered: print(*ix)
... 
the winners are
 
> The ideas went (rapidly) down-hill from there...
> 
> 
> Is there another, more pythonic, approach to conditional (for/while)
> loop processing?

I'm not aware of such an approach.

-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Loop with else clause

2019-02-05 Thread Adriaan Renting



I don't know if it is very Pythonic, but I would do something like

if no_oscar_nominees:
print ("Sorry ...")
else:
print_list_of_oscar_nominees()

And yes, that for/else construct can be confusing.

Adriaan.


>>> On 5-2-2019 at 5:29, DL Neil 
wrote: 
> What is the pythonic way to handle the situation where if a condition

> exists the loop should be executed, but if it does not something else

> should be done?
> 
> 
> Why am I asking?
> Today's code review included a for...else structure. I've rarely seen

> such a thing, and even knowing it exists, cannot recall ever using
it! 
> The coder intended to implement the scenario (above) but did not
realise 
> that the else-clause means 'execute if the loop ended without using 
> break'. She thought it meant 'if there's nothing in the iterable, 
> execute the else clause' (per if...then...else... ie the two clauses
are 
> mutually-exclusive*) - which one assumes is the reason why the BDfL
is 
> claimed to have said it should never have been implemented (this
way). 
> She neglected to test the exception properly, and was lulled into a 
> false sense of security by the coverage reporting 100%. Oops!
> 
> *see also the more commonly-used try...except...else...[finally...]
> 
> 
> When/how does this occur?
> Our client is more than a little commercially-sensitive. So as a
really 
> simple scenario, imagine a report is required, naming people who have

> become eligible for something, eg students qualified to enter an 
> advanced class, Oscar film award nominees, entrants who have
fulfilled 
> the requirements of a competition from which a winner will be
randomly 
> selected...
> 
> The names all appear in a list, so the most frequent use-case is
trivial:
> 
>   print( "And the winners are:" )
>   for name in list:
>   print( name )
> 
> but, if no-one actually qualifies, a warning message is required, eg
> 
>   print( "Sorry, no-one is eligible" )
> 
> 
> Possible solution:
> To make anything more than the trivial case readable, I think I'd put

> the list processing into one function, and the exception into another

> (except that this case is so trivial), ie
> 
>   if list:
>   process_list() #the heading and for-loop, as above
>   else:
>   print( "Sorry...
> 
> 
> Others wanted to add a semaphore/flag inside the loop to indicate if
it 
> was executed at least once. Yes, could even use the else clause
then!
> 
> The ideas went (rapidly) down-hill from there...
> 
> 
> Is there another, more pythonic, approach to conditional (for/while)

> loop processing?

-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Loop with else clause

2019-02-05 Thread Avi Gross
The topic is how to deal with a python loop that may not be run if you want
something else to happen in that case. Multiple solutions are presented
along with this request:

> Is there another, more pythonic, approach to conditional (for/while) 
> loop processing?

Has anyone considered looking at this from the perspective of the CONDITION
being evaluated?

If the condition is something simple like is the "list" empty, then any
method will work as it can be evaluated before trying the loop with no side
effects.

But what if the item in question is complex and constructed dynamically as
by zipping things together or calling functions and might even produce side
effects? It might be far from trivial to do that before the loop and check
and you might inadvertently change things. Using a sentinel approach might
be safer.

And what about loops that start but exit immediately without doing anything?
If you iterate on something whose first value is wrong in some way and the
loop starts with an "if ... break" then do you want to do the ELSE condition
because the loop sort of did not run as anticipated? A similar example is if
the loop starts with a "try" and the error handler does a break. 
 


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Loop with Else Clause

2019-02-05 Thread Christman, Roger Graydon
-Original Message-
From: Python-list  On
Behalf Of DL Neil
Sent: Monday, February 4, 2019 11:29 PM
To: 'Python' 
Subject: Loop with else clause

What is the pythonic way to handle the situation where if a condition exists 
the loop should be executed, but if it does not something else should be done?


--


Just reading this by itself without seeing the illustrative problem, my first 
gut reaction was "while loop!"  but then it became more obvious that this was a 
one-off condition to decide to get started, as opposed to being the loop 
condition itself.


That being said, I would agree that you really cannot do better than the 
if-else already presented, since it is clearly a binary choice:  execute the 
complete loop, or don't if you can't.


I personally try to avoid the else part on both while loops and for loops 
because of this sort of confusion.  All other things being equal, having an 
else section is fundamentally the same thing as simply omitting the 'else:' and 
out-denting the consequence, and makes it very clear just what is going on.


The difference, of course, appears when you have a 'break' within that loop 
that exits prematurely.   And that's where the structured-programming purist in 
me starts to gibber in fear and loathing.  My purist self says that if 'break' 
is applicable, than exception handling is just as applicable with a much 
cleaner model, since you can control where to go next, whether to continue 
repeating, etc.


If I don't expect to get all the way through a for loop, I simply don't write a 
for loop -- I'll substitute with a while, moving the logical negation of the 
break condition into the while loop condition, making it very clear that I 
don't plan to make it to the end.  Unfortunately, the for loop is such a common 
Pythonic thing to do that I really can't say 'for/break' is unPythonic -- I can 
only substitute Lovecraftian adjectives (in my opinion, of course) to describe 
that construct.


But this whole little tangent of mine doesn't seem to apply to your situation 
at all,

and I don't there is a 'single' language element in any programming language I 
can think

of that does what you ask -- so stick with the cleaner: if (__): # loop; else: 
#don't loop


Roger Christman

Pennsylvania State University

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Implement C's Switch in Python 3

2019-02-05 Thread John Sanders
On Saturday, February 2, 2019 at 6:47:49 PM UTC-6, Sayth Renshaw wrote:
> Hi
> 
> I am trying to convert a switch statement from C into Python. (why? 
> practising).
> 
> This is the C code. 
> 
> printf("Dated this %d", day);
>   switch (day) {
> case 1: case 21: case 31:
> printf("st"); break;
> case 2: case 22:
> printf("nd"); break;
> case 3: case 23:
> printf("rd"); break;
> default: printf("th"); break;
> 
>   }
>   printf(" day of ");
> 
> #Premise if the use enter an int as the date 21 for example it would print 
> 21st. It appends the correct suffix onto a date.
> 
> Reading and trying to implement a function that uses a dictionary. Not sure 
> how to supply list into it to keep it brief and with default case of 'th'.
> 
> This is my current code.
> 
> def f(x):
> return {
> [1, 21, 31]: "st",
> [2, 22]: "nd",
> [3, 23]: "rd",
> }.get(x, "th")
> 
> 
> print(f(21))
> 
> I have an unhashable type list. Whats the best way to go?
> 
> Cheers
> 
> Sayth

My best, readable attempt good for 1 - 99:

def sufx( day ):
if (day % 10) in [0,4,5,6,7,8,9] or (day / 10) == 1:  
return 'th'
return {1: 'st', 2: 'nd', 3: 'rd'}[day % 10]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Implement C's Switch in Python 3

2019-02-05 Thread MRAB

On 2019-02-06 00:25, John Sanders wrote:

On Saturday, February 2, 2019 at 6:47:49 PM UTC-6, Sayth Renshaw wrote:

Hi

I am trying to convert a switch statement from C into Python. (why? practising).

This is the C code. 


printf("Dated this %d", day);
  switch (day) {
case 1: case 21: case 31:
printf("st"); break;
case 2: case 22:
printf("nd"); break;
case 3: case 23:
printf("rd"); break;
default: printf("th"); break;

  }

  printf(" day of ");

#Premise if the use enter an int as the date 21 for example it would print 
21st. It appends the correct suffix onto a date.

Reading and trying to implement a function that uses a dictionary. Not sure how 
to supply list into it to keep it brief and with default case of 'th'.

This is my current code.

def f(x):
return {
[1, 21, 31]: "st",
[2, 22]: "nd",
[3, 23]: "rd",
}.get(x, "th")


print(f(21))

I have an unhashable type list. Whats the best way to go?

Cheers

Sayth


My best, readable attempt good for 1 - 99:

def sufx( day ):
 if (day % 10) in [0,4,5,6,7,8,9] or (day / 10) == 1:
 return 'th'
 return {1: 'st', 2: 'nd', 3: 'rd'}[day % 10]


In Python 3, 11/10 == 1.1, not 1.
--
https://mail.python.org/mailman/listinfo/python-list