Re: IDLE: clearing the screen

2024-06-10 Thread Michael F. Stemper via Python-list

On 08/06/2024 14.18, Rob Cliffe wrote:

OK, here is the advanced version:
import os
class _cls(object):
     def __repr__(self):
         os.system('cls')
         return ''
cls = _cls()

Now when you type
cls
it clears the screen.  The only flaw is that there is a blank line at the very top of the screen, 
and the ">>>" prompt appears on the SECOND line.
(This blank line is because the IDLE prints the blank value returned by "return 
''" and adds a newline to it, as it does when printing the value of any expression.)


Why have it return anything at all?

--
Michael F. Stemper
Indians scattered on dawn's highway bleeding;
Ghosts crowd the young child's fragile eggshell mind.

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


Re: IDLE: clearing the screen

2024-06-10 Thread Michael F. Stemper via Python-list

On 10/06/2024 09.32, Stefan Ram wrote:

"Michael F. Stemper"  wrote or quoted:

On 08/06/2024 14.18, Rob Cliffe wrote:

OK, here is the advanced version:
import os
class _cls(object):
      def __repr__(self):
          os.system('cls')
          return ''
cls = _cls()

...

Why have it return anything at all?


   Because __repr__ needs to return a str.


Got it. Thanks for clarifying.

--
Michael F. Stemper
87.3% of all statistics are made up by the person giving them.

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


Re: A technique from a chatbot

2024-04-03 Thread Michael F. Stemper via Python-list

On 03/04/2024 13.45, Gilmeh Serda wrote:

On 2 Apr 2024 17:18:16 GMT, Stefan Ram wrote:


first_word_beginning_with_e


Here's another one:


def ret_first_eword():

... return [w for w in ['delta', 'epsilon', 'zeta', 'eta', 'theta'] if 
w.startswith('e')][0]
...

ret_first_eword()

'epsilon'


Doesn't work in the case where there isn't a word starting with 'e':

 >>> def find_e( l ):
 ...   return [w for w in l if w.startswith('e')][0]
 ...
 >>> l = ['delta', 'epsilon', 'zeta', 'eta', 'theta']
 >>> find_e(l)
 'epsilon'
 >>> l = ['The','fan-jet','airline']
 >>> find_e(l)
 Traceback (most recent call last):
   File "", line 1, in 
   File "", line 2, in find_e
 IndexError: list index out of range
 >>>


--
Michael F. Stemper
If it isn't running programs and it isn't fusing atoms, it's just bending space.

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


Re: Popping key causes dict derived from object to revert to object

2024-03-25 Thread Michael F. Stemper via Python-list

On 25/03/2024 01.56, Loris Bennett wrote:

Grant Edwards  writes:


On 2024-03-22, Loris Bennett via Python-list  wrote:


Yes, I was mistakenly thinking that the popping the element would
leave me with the dict minus the popped key-value pair.


It does.


Indeed, but I was thinking in the context of

   dict_list = [d.pop('a') for d in dict_list]

and incorrectly expecting to get a list of 'd' without key 'a', instead
of a list of the 'd['a]'.

I apologize if this has already been mentioned in this thread, but are
you aware of "d.keys()" and "d.values"?

 >>> d = {}
 >>> d['do'] = 'a deer, a female deer'
 >>> d['re'] = 'a drop of golden sunshine'
 >>> d['mi'] = 'a name I call myself'
 >>> d['fa'] = 'a long, long way to run'
 >>> d.keys()
 ['fa', 'mi', 'do', 're']
 >>> d.values()
 ['a long, long way to run', 'a name I call myself', 'a deer, a female deer', 
'a drop of golden sunshine']
 >>>


--
Michael F. Stemper
Exodus 22:21

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


Re: RE: Newline (NuBe Question)

2023-11-26 Thread Michael F. Stemper via Python-list

On 24/11/2023 21.45, avi.e.gr...@gmail.com wrote:

Grizz[l]y,

I think the point is not about a sorted list or sorting in general It is
about reasons why maintaining a data structure such as a list in a program
can be useful beyond printing things once. There are many possible examples
such as having a list of lists containing a record where the third item is a
GPA for the student and writing a little list comprehension that selects a
smaller list containing only students who are Magna Cum Laude or Summa Cum
Laude.

studs = [
   ["Peter", 82, 3.53],
   ["Paul", 77, 2.83],
   ["Mary", 103, 3.82]
]


I've seen Mary, and she didn't look like a "stud" to me.


Of course, for serious work, some might suggest avoiding constructs like a
list of lists and switch to using modules and data structures [...]


Those who would recommend that approach do not appear to include Mr.
Rossum, who said:
  
  Avoid overengineering data structures. Tuples are better than

  objects (try namedtuple too though). Prefer simple fields over
  getter/setter functions... Built-in datatypes are your friends.
  Use more numbers, strings, tuples, lists, sets, dicts. Also
  check out the collections library, eps. deque.[1]
  
I was nodding along with the people saying "list of lists" until I

reread this quote. A list of tuples seems most appropriate to me.

  
[1] , as quoted by Bill

Lubanovic in _Introducing Python_

--
Michael F. Stemper
This sentence no verb.

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


Re: Question(s)

2023-10-25 Thread Michael F. Stemper via Python-list

On 25/10/2023 05.45, o1bigtenor wrote:

On Tue, Oct 24, 2023 at 8:35 PM Chris Angelico via Python-list
 wrote:



3. Catch the failure before you commit and push. Unit tests are great for this.


Where might I find such please.


You don't "find" unit tests; you write them. A unit test tests
a specific function or program.

Ideally, you write each unit test *before* you write the function
that it tests.

For instance, suppose that you were writing a function to calculate
the distance between two points. We know the following things about
distance:
1. The distance from a point to itself is zero.
2. The distance between two distinct points is positive.
3. The distance from A to B is equal to the distance from B to A.
4. The distance from A to B plus the distance from B to C is at
   least as large as the distance from A to C.

You would write unit tests that generate random points and apply
your distance function to them, checking that each of these
conditions is satisfied. You'd also write a few tests of hard-coded
points,such as:
- Distance from (0,0) to (0,y) is y
- Distance from (0,0) to (x,0) is x
- Distance from (0,0) to (3,4) is 5
- Distance from (0,0) to (12,5) is 13

The python ecosystem provides many tools to simplify writing and
running unit tests. Somebody has already mentioned "unittest". I
use this one all of the time. There are also "doctest", "nose",
"tox", and "py.test" (none of which I've used).

--
Michael F. Stemper
Life's too important to take seriously.

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


Re: Question(s)

2023-10-25 Thread Michael F. Stemper via Python-list

On 24/10/2023 18.15, o1bigtenor wrote:



What is interesting about this is the absolute certainty that it is impossible
to program so that that program is provably correct.


Not entirely true. If I was to write a program to calculate Fibonacci
numbers, or echo back user input, that program could be proven correct.
But, there is a huge set of programs for which it is not possible to
prove correctness.

In fact, there is a huge (countably infinite) set of programs for which it
is not even possible to prove that the program will halt.

Somebody already pointed you at a page discussing "The Halting Problem".
You really should read up on this.


Somehow - - - well - - to me that sounds that programming is illogical.

If I set up a set of mathematical problems (linked) I can prove that the
logic structure of my answer is correct.


Exactly the same situation. There are many (again, countably infinite)
mathematical statements where it is not possible to prove that the statement
is either true or false. I want to be clear that this is not the same as
"we haven't figured out how to do it yet." It is a case of "it is mathematically
possible to show that we can't either prove or disprove statement ."

Look up Kurt Gödel's work on mathematical incompleteness, and some of the
statements that fall into this category, such as the Continuum Hypothesis
or the Parallel Postulate.

As I said at the beginning, there are a lot of programs that can be
proven correct or incorrect. But, there are a lot more that cannot.

--
Michael F. Stemper
Outside of a dog, a book is man's best friend.
Inside of a dog, it's too dark to read.

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


Re: Question(s)

2023-10-25 Thread Michael F. Stemper via Python-list

On 24/10/2023 17.50, Thomas Passin wrote:



   The programming team for the Apollo moon mission developed a system which,> 
if you would write your requirements in a certain way, could generate correct
C code for them.

Since the last Apollo mission was in 1972, when C was first being developed, I
find this hard to believe.

--
Michael F. Stemper
Outside of a dog, a book is man's best friend.
Inside of a dog, it's too dark to read.

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


Re: error of opening Python

2023-09-26 Thread Michael F. Stemper via Python-list

On 26/09/2023 07.27, Abdelkhelk ashref salay eabakh wrote:

Dear Python team,

This is my not first time using Python, I tried to launch Python and it showed
"Python 3.11.3 (tags/v3.11.3:f3909b8, Apr  4 2023, 23:49:59) [MSC v.1934 64 bit 
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information." I
don't know what this meant and how to fix this. Could you please help me?


What error did you encounter? Aside from the lack of line breaks, it looks
quite similar to what I get when I start up python:

 Python 3.6.9 (default, Mar 10 2023, 16:46:00)
 [GCC 8.4.0] on linux
 Type "help", "copyright", "credits" or "license" for more information.
 >>>

Didn't you get the ">>> " prompt? Once you get it, it shows that the
Read, Evaluate, Print Loop (REPL) is ready for you to type some python
statements. For instance:

 >>> x = 2**3
 >>> print(x)
 8
 >>>

--
Michael F. Stemper
Outside of a dog, a book is man's best friend.
Inside of a dog, it's too dark to read.

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