Subtracting dates to get hours and minutes

2022-12-11 Thread Steve GS
How do I subtract two time/dates and calculate the hours and minutes
between?
Steve

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


Re: Does one have to use curses to read single characters from keyboard?

2022-12-11 Thread Cameron Simpson

On 11Dec2022 22:22, Barry Scott  wrote:
   # Get a single character, setcbreak rather than setraw meands 
   CTRL/C

   etc. still work
   #
   def getch():
   sys.stdout.flush()
   tty.setcbreak(fdInput)
   ch = sys.stdin.buffer.raw.read(1).decode(sys.stdin.encoding)


Will not work for uncode code points above 255.


Aye. But one could write a little loop to collect bytes until a complete 
character was received. A little experiment:


>>> try: bytes((0xf0,)).decode('utf8')
... except UnicodeDecodeError as e:
...   e2=e
...
>>> e2
UnicodeDecodeError('utf-8', b'\xf0', 0, 1, 'unexpected end of data')
>>> e2.reason
'unexpected end of data'

Keep collecting while you get `UnicodeDecodeError`s with a `.reason` of 
'unexpected end of data'. Can be encoding agnostic (obviously you need 
to _choose_ an encoding, it is needn't be utf-8).


(For the OP: `UnicodeDecodeError` doesn't necessarily mean you're 
decoding Unicode data, you're decoding _into_ a Python string which is a 
Unicode string.)


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: Does one have to use curses to read single characters from keyboard?

2022-12-11 Thread Chris Angelico
On Mon, 12 Dec 2022 at 09:24, Barry Scott  wrote:
> You would need to have a loop that collected all the utf-8 bytes of a single 
> code point.
> You can to look at the first byte of know if the utf-8 is 1, 2, 3 or 4 bytes 
> for a code point.

And cope with escape sequences too - if you press an arrow key, for
instance, you'll get a multi-character string to signal that.

This is why it's probably easier to let someone else do the work.

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


Re: Does one have to use curses to read single characters from keyboard?

2022-12-11 Thread Barry Scott


> On 11 Dec 2022, at 18:50, Chris Green  wrote:
> 
> My solution in the end was copied from one I found that was much
> simpler and straightforward than most.  I meant to post this earlier
> but it got lost somewhere:-
> 
>import sys, termios, tty
>#
>#
># Read a single character from teminal, specifically for 'Y/N' 
>#
>fdInput = sys.stdin.fileno()
>termAttr = termios.tcgetattr(0)
>#
>#
># Get a single character, setcbreak rather than setraw meands CTRL/C
>etc. still work
>#
>def getch():
>sys.stdout.flush()
>tty.setcbreak(fdInput)
>ch = sys.stdin.buffer.raw.read(1).decode(sys.stdin.encoding)

Will not work for uncode code points above 255.

This is what happened when I typed € key:

:>>> a.getch()
Traceback (most recent call last):
  File "", line 1, in 
  File "/private/var/folders/ll/08dwwqkx6v9bcd15sh06x14wgn/T/a.py", line 
15, in getch
ch = sys.stdin.buffer.raw.read(1).decode(sys.stdin.encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe2 in position 0: 
unexpected end of data

You would need to have a loop that collected all the utf-8 bytes of a single 
code point.
You can to look at the first byte of know if the utf-8 is 1, 2, 3 or 4 bytes 
for a code point.

Barry

>termios.tcsetattr(fdInput, termios.TCSAFLUSH, termAttr)
>sys.stdout.write(ch)
>return ch
>#
>#
># Get a y or n answer, ignore other characters
>#
>def getyn():
>ch = 'x'
>while ch != 'y' and ch != 'n':
>ch = getch().lower()
>return ch
> 
> So getyn() reads a y or an n, ignores anything else and doesn't wait
> for a return key.  Keyboard input operation is restored to normal
> after doing this. Using tty.setcbreak() rather than tty.setraw() means
> that CTRL/C etc. still work if things go really wrong.

> 
> 
> -- 
> Chris Green
> ·
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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


Re: Does one have to use curses to read single characters from keyboard?

2022-12-11 Thread dn

On 11/12/2022 23.09, Chris Green wrote:

Is the only way to read single characters from the keyboard to use
curses.cbreak() or curses.raw()?  If so how do I then read characters,
it's not at all obvious from the curses documentation as that seems to
think I'm using a GUI in some shape or form.

All I actually want to do is get 'Y' or 'N' answers to questions on
the command line.

Searching for ways to do this produces what seem to me rather clumsy
ways of doing it.


You may like to re-ask this question over on the Python-Tutor list. The 
ListAdmin over there (literally) wrote the book on Python+curses...



Did such research include the keyboard module?
https://pypi.org/project/keyboard/

This project includes an (Enter-free) "hot-key" feature which firstly 
detects the specific key or key-combination, and then calls an action 
if/when True.

(amongst other functionality)

Quick read tutorial: 
https://www.thepythoncode.com/article/control-keyboard-python


Disclaimer: have had it on my 'radar', but never actually employed.
(if you have considered, will be interested to hear conclusions...)
--
Regards,
=dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: on the python paradox

2022-12-11 Thread Abdur-Rahmaan Janhangeer
I choose Python and still stick with it as default as I choose
Python because of its design beauty. Typing does not mean
mandatory braces. There can be an indentation-based language
that is strongly typed.

Python is beautiful in itself. Beautiful to look at. Source code should
be easy for the average human to relate to and connect with easily.
Being more alien-ware-like does not magically increase performance.
Our taste and quality bar should be high after so much time has passed.

I don't see a language as beautiful as it is front-end wise.

"And people don't learn Python because it will
get them a job; they learn it because they genuinely like to program
and aren't satisfied with the languages they already know."

Kind Regards,

Abdur-Rahmaan Janhangeer
about  | blog

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


Re: Does one have to use curses to read single characters from keyboard?

2022-12-11 Thread Chris Green
My solution in the end was copied from one I found that was much
simpler and straightforward than most.  I meant to post this earlier
but it got lost somewhere:-

import sys, termios, tty
#
#
# Read a single character from teminal, specifically for 'Y/N' 
#
fdInput = sys.stdin.fileno()
termAttr = termios.tcgetattr(0)
#
#
# Get a single character, setcbreak rather than setraw meands CTRL/C
etc. still work
#
def getch():
sys.stdout.flush()
tty.setcbreak(fdInput)
ch = sys.stdin.buffer.raw.read(1).decode(sys.stdin.encoding)
termios.tcsetattr(fdInput, termios.TCSAFLUSH, termAttr)
sys.stdout.write(ch)
return ch
#
#
# Get a y or n answer, ignore other characters
#
def getyn():
ch = 'x'
while ch != 'y' and ch != 'n':
ch = getch().lower()
return ch

So getyn() reads a y or an n, ignores anything else and doesn't wait
for a return key.  Keyboard input operation is restored to normal
after doing this. Using tty.setcbreak() rather than tty.setraw() means
that CTRL/C etc. still work if things go really wrong.


-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: on the python paradox

2022-12-11 Thread Sabrina Almodóvar
On 11/12/2022 10:57, Martin Di Paola wrote:
>> On Mon, Dec 05, 2022 at 10:37:39PM -0300, Sabrina Almodóvar wrote:
>>>     The Python Paradox
>>>    Paul Graham
>>>    August 2004
>>>
>>> [SNIP]
>>>
>>> Hence what, for lack of a better name, I'll call the Python paradox:
>>> if a company chooses to write its software in a comparatively
>>> esoteric language, they'll be able to hire better programmers,
>>> because they'll attract only those who cared enough to learn it. And
>>> for programmers the paradox is even more pronounced: the language to
>>> learn, if you want to get a good job, is a language that people don't
>>> learn merely to get a job.
>>>
>>> [SNIP]
> 
> I don't think that an esoteric language leads to better programmers.

When you say this, I interpret it as a theorem, A implies B, but surely
nobody would be so foolish to claim such thing, so perhaps you can
review your reading or writing.

> I know really good people that work mostly in assembly which by today
> standard would be considered "esoteric".

So, I wouldn't consider assembly esoteric, but I certainly would not try
to define esoteric.

> They are really good at their field but they write shitty code in higher
> languages as python.

I bet.  If all they know is assembly, then they master very few
linguistic abstractions.

> That same goes for the other direction: I saw Ruby programmers writing C
> code and trust me, it didn't result in good quality code.

A Ruby person who doesn't know C must also know very little about
machines and operating systems, so that is bound to failure in C.

> I would be more inclined to think that a good programmer is not the one
> that knows an esoteric language but the one that can jump from one
> programming paradigm to another.

That makes a lot of sense.  Such person knows so many ways of
expression, which most likely implies mastery of linguistic abstractions
and expression.

> And when I say "jump" I mean that he/she can understand the problem to
> solve, find the best tech stack to solve it and do it in an efficient
> manner using that tech stack correctly.

Got ya.

> It is in the "using that tech stack correctly" where some programmers
> that "think" they know languages A, B and C get it wrong.

I agree with that too.

> Just writing code that "compiles" and "it does not immediately crash" is
> not enough to say that "you are using the tech stack correctly".

So true.

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


Re: Does one have to use curses to read single characters from keyboard?

2022-12-11 Thread Antoon Pardon




Op 11/12/2022 om 12:32 schreef Stefan Ram:

r...@zedat.fu-berlin.de (Stefan Ram) writes:

Curses is not portable IIRC. A more portable means would
be to use tkinter with the "bind" function to bind keys.

import tkinter

text = tkinter.Text()
text.pack()
text.bind\
( "",
   lambda event:
   text.insert
   ( tkinter.END, "Y\nFormatting drive C:\n...\n" )or "break" )
text.insert( tkinter.END, "Format drive C:?\n" )
text.focus()

tkinter.mainloop()

   Not allowing users to edit their keypresses before confirming
   them with [Return]. What could possibly go wrong?


Nothing that can't go wrong otherwise. It is my experience that
when a [Return] is needed, people just type in a two key combination.
They don't type one key, then check, then type [Return].

So in practice the same things go wrong, either way.

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


Re: Does one have to use curses to read single characters from keyboard?

2022-12-11 Thread Chris Nyland
You should try the input function. I use it all the time. It does require
the user to hit enter but that is pretty typical of that kind of interface.
So I would write a loop like

while True:
   answer = input("Please answer the question (y/n):")
  if answer == 'y':
break

Chris

On Sun, Dec 11, 2022 at 11:03 AM Stefan Ram  wrote:

> r...@zedat.fu-berlin.de (Stefan Ram) writes:
> >import tkinter
>
>   This two-liner allows to answer with just one keypress ([Y]/[N]) here.
>
> import tkinter.messagebox
> tkinter.messagebox.askyesno( "Question", "Format harddisk?" )
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Does one have to use curses to read single characters from keyboard?

2022-12-11 Thread Oscar Benjamin
On Sun, 11 Dec 2022 at 15:55, Chris Green  wrote:
>
> Is the only way to read single characters from the keyboard to use
> curses.cbreak() or curses.raw()?  If so how do I then read characters,
> it's not at all obvious from the curses documentation as that seems to
> think I'm using a GUI in some shape or form.
>
> All I actually want to do is get 'Y' or 'N' answers to questions on
> the command line.
>
> Searching for ways to do this produces what seem to me rather clumsy
> ways of doing it.

What you are asking for is known as getch. On Windows Python's msvcrt
module provides precisely this function. There are ways to do it on
non-Windows platforms but nothing as simple as the getch function is
in the stdlib. Some modules and recipes are available which I guess it
what you've already seen:

https://pypi.org/project/getch/
https://github.com/joeyespo/py-getch
https://stackoverflow.com/questions/510357/how-to-read-a-single-character-from-the-user

This seems to be the most well maintained option:
https://pypi.org/project/readchar/

I've often thought that getch was a good candidate for the stdlib.
There are plenty of recipes around but it would ideally just be
available as a cross platform function. Using curses would have been
overkill in any of my use cases where I really just wanted getch to
make a more efficient interface for a terminal program having some
limited interactivity. Actually slightly more than getch is readchar's
readkey which also works for pressing non-character keys. There were
times in the past where I might have used that if I'd known about it.

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


Re: Does one have to use curses to read single characters from keyboard?

2022-12-11 Thread Chris Green
Stefan Ram  wrote:
> Chris Green  writes:
> >Is the only way to read single characters from the keyboard to use
> >curses.cbreak() or curses.raw()?  If so how do I then read characters,
> 
>   It seems that you want to detect keypresses and not read
>   characters from a line-buffered console with editing
>   features.
> 
>   Curses is not portable IIRC. A more portable means would 
>   be to use tkinter with the "bind" function to bind keys.
> 
> >All I actually want to do is get 'Y' or 'N' answers to questions on
> >the command line.
> 
>   answer = input( 'Format drive C: (Y/N)?' )
> 
... and therein lies the fundamental problem, you have to type Y or N
followed by Return.  See my own follow-up though.

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Does one have to use curses to read single characters from keyboard?

2022-12-11 Thread DFS

On 12/11/2022 5:09 AM, Chris Green wrote:

Is the only way to read single characters from the keyboard to use
curses.cbreak() or curses.raw()?  If so how do I then read characters,
it's not at all obvious from the curses documentation as that seems to
think I'm using a GUI in some shape or form.

All I actually want to do is get 'Y' or 'N' answers to questions on
the command line.

Searching for ways to do this produces what seem to me rather clumsy
ways of doing it.



resp = 'x'
while resp.lower() not in 'yn':
resp = input("Did you say Y or did you say N?: ")




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


Does one have to use curses to read single characters from keyboard?

2022-12-11 Thread Chris Green
Is the only way to read single characters from the keyboard to use
curses.cbreak() or curses.raw()?  If so how do I then read characters,
it's not at all obvious from the curses documentation as that seems to
think I'm using a GUI in some shape or form.

All I actually want to do is get 'Y' or 'N' answers to questions on
the command line.

Searching for ways to do this produces what seem to me rather clumsy
ways of doing it.

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: on the python paradox

2022-12-11 Thread Martin Di Paola

On Mon, Dec 05, 2022 at 10:37:39PM -0300, Sabrina Almodóvar wrote:

The Python Paradox
   Paul Graham
   August 2004

[SNIP]

Hence what, for lack of a better name, I'll call the Python paradox: 
if a company chooses to write its software in a comparatively 
esoteric language, they'll be able to hire better programmers, 
because they'll attract only those who cared enough to learn it. And 
for programmers the paradox is even more pronounced: the language to 
learn, if you want to get a good job, is a language that people 
don't learn merely to get a job.


[SNIP]


I don't think that an esoteric language leads to better programmers.

I know really good people that work mostly in assembly which by today
standard would be considered "esoteric".

They are really good at their field but they write shitty code in higher
languages as python.

That same goes for the other direction: I saw Ruby programmers writing C
code and trust me, it didn't result in good quality code.

I would be more inclined to think that a good programmer is not the one
that knows an esoteric language but the one that can jump from one
programming paradigm to another.

And when I say "jump" I mean that he/she can understand the problem to
solve, find the best tech stack to solve it and do it in an efficient
manner using that tech stack correctly.

It is in the "using that tech stack correctly" where some programmers
that "think" they know languages A, B and C get it wrong.

Just writing code that "compiles" and "it does not immediately crash" is
not enough to say that "you are using the tech stack correctly".


On Wed, Dec 07, 2022 at 10:58:09AM -0500, David Lowry-Duda wrote:

On Mon, Dec 05, 2022 at 10:37:39PM -0300, Sabrina Almodóvar wrote:

The Python Paradox
   Paul Graham
   August 2004

[SNIP]

Hence what, for lack of a better name, I'll call the Python paradox: 
if a company chooses to write its software in a comparatively 
esoteric language, they'll be able to hire better programmers, 
because they'll attract only those who cared enough to learn it. And 
for programmers the paradox is even more pronounced: the language to 
learn, if you want to get a good job, is a language that people 
don't learn merely to get a job.


[SNIP]


I wonder what the appropriately esoteric language is today?

We can sort of think of go/rust as esoteric versions of C/C++. But 
what would be the esoteric python?


Perhaps Julia? I don't know of any large software projects happening 
in julia world that aren't essentially scientific computing libraries 
(but this is because *I* work mostly with scientific computing 
libraries and sometimes live under a rock).


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

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