RE: how to let argument be optional falling back to certain integer

2020-06-22 Thread David Raymond


> This is true.  I have written 0 as false in C so many times.  But
> clearly for me times have changed...  I now look at numbers as a thing
> in their own special class not to be confused as truth-values.  (So much
> so that I fell for this.)  But I confess I still think of numbers as all
> TRUE.  (Even zero!)


Also remember that in Python bool is a subclass of int:
>>> isinstance(False, int)
True
>>> 0 == False
True
>>> 1 == True
True
>>> ["A", "B"][False]
'A'
>>> ["A", "B"][True]
'B'

So if you're trying to do something slightly different based on the type of the 
input you might fall into this trap

if isinstance(foo, float):
do float stuff
elif isinstance(foo, int):
do int stuff
elif isinstance(foo, bool):
this line will never run because it would have triggered the int line

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


Re: how to let argument be optional falling back to certain integer

2020-06-22 Thread Boris Dorestand
David Raymond  writes:

>> This is true.  I have written 0 as false in C so many times.  But
>> clearly for me times have changed...  I now look at numbers as a thing
>> in their own special class not to be confused as truth-values.  (So much
>> so that I fell for this.)  But I confess I still think of numbers as all
>> TRUE.  (Even zero!)
>
> Also remember that in Python bool is a subclass of int:
 isinstance(False, int)
> True
 0 == False
> True
 1 == True
> True
 ["A", "B"][False]
> 'A'
 ["A", "B"][True]
> 'B'
>
> So if you're trying to do something slightly different based on the
> type of the input you might fall into this trap
>
> if isinstance(foo, float):
> do float stuff
> elif isinstance(foo, int):
> do int stuff
> elif isinstance(foo, bool):
> this line will never run because it would have triggered the int line

In my case I was only interested in ints, so I actually did try
isinstance(k, int) and it seemed to work because I didn't try 

  k = False 

I settled for the explicit check done by Chris Angelico.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Problem installing Python

2020-06-22 Thread MRAB

On 2020-06-21 18:31, Aisha Akintola wrote:

Good day,
I downloaded python(python3.8.3) on my system (windows 8) to enable me use
PyCharm. However, I realised the python wasn't well installed. I get
message "the program can't start because api-ms-win-crt-runtime-I1-1-0.dll
is missing from your computer. Try reinstalling the program to fix
this problem'.
I have reinstalled but got the same error message; what should I do?
Thank you


You need the Windows Universal C Runtime:

http://www.microsoft.com/en-us/download/details.aspx?id=48234
--
https://mail.python.org/mailman/listinfo/python-list


parsing encrypted netrc file

2020-06-22 Thread Seb
Hello,

What's the pythonic way to do this without polluting the user's
directory with the decrypted file?  I wrongly thought this should do it:

import os.path as osp
import gnupg
import netrc
import tempfile

gpg = gnupg.GPG()

with open(osp.expanduser("~/.authinfo.gpg"), "rb") as f:
with tempfile.NamedTemporaryFile("w+") as tf:
status = gpg.decrypt_file(f, output=tf.name)
info = netrc.netrc(tf.name)

which fails as the temporary file doesn't even get created.

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


Re: parsing encrypted netrc file

2020-06-22 Thread MRAB

On 2020-06-22 23:38, Seb wrote:

Hello,

What's the pythonic way to do this without polluting the user's
directory with the decrypted file?  I wrongly thought this should do it:

import os.path as osp
import gnupg
import netrc
import tempfile

gpg = gnupg.GPG()

with open(osp.expanduser("~/.authinfo.gpg"), "rb") as f:
 with tempfile.NamedTemporaryFile("w+") as tf:
 status = gpg.decrypt_file(f, output=tf.name)
 info = netrc.netrc(tf.name)

which fails as the temporary file doesn't even get created.


Are you sure it doesn't get created?

I'm wondering whether you need to flush the file before passing it to 
netrc to ensure that all of the decrypted data is there on disk for reader.


I'm also wondering whether the file is shareable.
--
https://mail.python.org/mailman/listinfo/python-list


Re: parsing encrypted netrc file

2020-06-22 Thread Seb
On Tue, 23 Jun 2020 00:40:28 +0100,
MRAB  wrote:

> On 2020-06-22 23:38, Seb wrote:
>> Hello,

>> What's the pythonic way to do this without polluting the user's
>> directory with the decrypted file?  I wrongly thought this should do
>> it:

>> import os.path as osp import gnupg import netrc import tempfile

>> gpg = gnupg.GPG()

>> with open(osp.expanduser("~/.authinfo.gpg"), "rb") as f: with
>> tempfile.NamedTemporaryFile("w+") as tf: status = gpg.decrypt_file(f,
>> output=tf.name) info = netrc.netrc(tf.name)

>> which fails as the temporary file doesn't even get created.

> Are you sure it doesn't get created?

Without using tempfile:

with open(osp.expanduser("~/.authinfo.gpg"), "rb") as f:
status = gpg.decrypt_file(f, output=".authinfo.txt")
info = netrc.netrc(".authinfo.txt")

I get the error:

NetrcParseError: bad follower token 'port' (.authinfo.txt, line 1)

which is interesting.  The structure of ~/.authinfo.gpg is:

machine my.server.com login [email protected] password mypasswd port 587

so it seems this is not what netrc.netrc expects.

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


Re: parsing encrypted netrc file

2020-06-22 Thread MRAB

On 2020-06-23 01:47, Seb wrote:

On Tue, 23 Jun 2020 00:40:28 +0100,
MRAB  wrote:


On 2020-06-22 23:38, Seb wrote:

Hello,



What's the pythonic way to do this without polluting the user's
directory with the decrypted file?  I wrongly thought this should do
it:



import os.path as osp import gnupg import netrc import tempfile



gpg = gnupg.GPG()



with open(osp.expanduser("~/.authinfo.gpg"), "rb") as f: with
tempfile.NamedTemporaryFile("w+") as tf: status = gpg.decrypt_file(f,
output=tf.name) info = netrc.netrc(tf.name)



which fails as the temporary file doesn't even get created.



Are you sure it doesn't get created?


Without using tempfile:

with open(osp.expanduser("~/.authinfo.gpg"), "rb") as f:
 status = gpg.decrypt_file(f, output=".authinfo.txt")
 info = netrc.netrc(".authinfo.txt")

I get the error:

NetrcParseError: bad follower token 'port' (.authinfo.txt, line 1)

which is interesting.  The structure of ~/.authinfo.gpg is:

machine my.server.com login [email protected] password mypasswd port 587

so it seems this is not what netrc.netrc expects.


Here's a page I found about ".netrc":

https://ec.haxx.se/usingcurl/usingcurl-netrc

and here's a page I found about ".authinfo.gpg":

https://www.emacswiki.org/emacs/GnusAuthinfo

Can you see the subtle difference?
--
https://mail.python.org/mailman/listinfo/python-list