RE: Subtracting dates to get hours and minutes

2022-12-12 Thread Mike Dewhirst
I have seen vast conversations on this topic but if everything is in the same 
time-zone and daylight saving switchovers are not involved it is relatively 
straightforward.Check the timedelta docs. Or convert datetimes to ordinals and 
subtract then convert the result to whatever units please you.M--(Unsigned mail 
from my phone)
 Original message From: Steve GS  Date: 
12/12/22  17:34  (GMT+10:00) To: [email protected] Subject: Subtracting 
dates to get hours and minutes How do I subtract two time/dates and calculate 
the hours and minutesbetween?Steve-- 
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-12 Thread Chris Green
Stefan Ram  wrote:
> Chris Green  writes:
> >import sys, termios, tty
> 
>   There might be some versions of Python and the Microsoft®
>   Windows operating system where "termios" is not available.
> 

Ah, I did originally say that this was a Unix/Linux only solution but
that was in my first response that got lost somewhere.

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


Re: Subtracting dates to get hours and minutes

2022-12-12 Thread Marc Lucke
my approach would be to convert your two date/times to seconds from 
epoch - e.g. 
https://www.geeksforgeeks.org/convert-python-datetime-to-epoch/ - then 
subtract the number, divide the resultant by 3600 (hours) & get the 
modulus for minutes.  There's probably a standard function - it should 
be /very/ easy to do.


- Marc

On 12/12/2022 5:01 pm, Steve GS wrote:

How do I subtract two time/dates and calculate the hours and minutes
between?
Steve


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


Re: How to convert a raw string r'\xdd' to '\xdd' more gracefully?

2022-12-12 Thread Jach Feng
moi 在 2022年12月12日 星期一下午5:38:50 [UTC+8] 的信中寫道:
> >>> ast.literal_eval("r'\x7a'") == ast.literal_eval("r'z'") 
> True 
> >>> ast.literal_eval("r'\xe0'") == ast.literal_eval("r'à'") 
> True 
> >>> ast.literal_eval("r'\x9c'") == ast.literal_eval("r'œ'") 
> False 
> 
> - 
> 
> 
> >>> print(codecs.decode(r'z', 'unicode-escape')) 
> z 
> >>> print(codecs.decode(r'g\hz', 'unicode-escape')) 
> g\hz 
> >>> print(codecs.decode(r'g\az', 'unicode-escape')) 
> g\u0007z 
> >>> print(codecs.decode(r'g\nz', 'unicode-escape')) 
> g 
> z 
> >>> 
> print(codecs.decode(r'abcü', 'unicode-escape')) 
> abcü 
> >>>
I have a different result:-)

>>> print(codecs.decode(r'g\hz', 'unicode-escape'))
:1: DeprecationWarning: invalid escape sequence '\h'
g\hz
>>> print(codecs.decode(r'g\az', 'unicode-escape'))
gz  # with a companioning bell
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2022-12-12 Thread Chris Green
Barry Scott  wrote:
> 
> 
> > 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
> 
Since it's just for me to use the above doesn't worry me! :-)  I'd
have to try quite hard to enter a multi-byte character from my
keyboard so it's very unlikely to happen by mistake.

-- 
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-12 Thread Alan Gauld
On 11/12/2022 21:07, dn wrote:
> 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...)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: Subtracting dates to get hours and minutes

2022-12-12 Thread Weatherby,Gerard
The difference between two datetime objects is a timedelta object. 
https://docs.python.org/3/library/datetime.html#timedelta-objects . It has a 
total_seconds() method.

This is a simple task, unless one of the datetimes has a time zone specified 
and the other doesn’t.

From: Python-list  on 
behalf of Marc Lucke 
Date: Monday, December 12, 2022 at 11:37 AM
To: [email protected] 
Subject: Re: Subtracting dates to get hours and minutes
*** Attention: This is an external email. Use caution responding, opening 
attachments or clicking on links. ***

my approach would be to convert your two date/times to seconds from
epoch - e.g.
https://urldefense.com/v3/__https://www.geeksforgeeks.org/convert-python-datetime-to-epoch/__;!!Cn_UX_p3!hBXQeMkZ3QYS6BI0yTHsADseWTXcDXOhKFkg35NnRMicvYQvwLo9c_ihSaTrG60LywsKQm6UNd7mAAYr$
  - then
subtract the number, divide the resultant by 3600 (hours) & get the
modulus for minutes.  There's probably a standard function - it should
be /very/ easy to do.

- Marc

On 12/12/2022 5:01 pm, Steve GS wrote:
> How do I subtract two time/dates and calculate the hours and minutes
> between?
> Steve
>
--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!hBXQeMkZ3QYS6BI0yTHsADseWTXcDXOhKFkg35NnRMicvYQvwLo9c_ihSaTrG60LywsKQm6UNSbh5q0S$
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2022-12-12 Thread Grant Edwards
On 2022-12-11, Chris Green  wrote:

> Is the only way to read single characters from the keyboard to use
> curses.cbreak() or curses.raw()?

No.

> If so how do I then read characters,

Use a termios.tcsetattr() to put fd 0 into raw mode and then use
os.read().

Recent versions of Python include a "tty" module that has conveniece
functions to handle that:

--
Grant





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


sqlite3 double quote behavior

2022-12-12 Thread John K. Parejko
Asking here before I file an improvement request issue on the python GitHub:

sqlite has a known misfeature with double-quoted strings, whereby they will be 
interpreted as string literals if they don’t match a valid identifier [1]. The 
note in the sqlite docs describe a way to disable this misfeature at compile 
time or by calling an `sqlite3_db_config` C-function, but I don’t see any way 
to do that in the python sqlite library [2].

Am I missing a way to manage this setting, or is it not available within 
python? This would be very useful to enable, so that python’s sqlite library 
will treat queries more like standard sql, instead of this particular version 
of MySQL. I was just burned by this, where some tests I’d written against an 
sqlite database did not fail in the way that they “should” have, because of 
this double-quoted string issue.

It doesn’t look like `sqlite3_db_config` is used within the python sqlite3 
codebase at all, so this might not be a trivial change? I only see two 
references to it in the cpython github.

Thank you in advance for any suggestions,
John

1: https://www.sqlite.org/quirks.html#double_quoted_string_literals_are_accepted
2: https://docs.python.org/3/library/sqlite3.html
-- 
https://mail.python.org/mailman/listinfo/python-list