Re: Which method to check if string index is queal to character.

2020-12-29 Thread jak

Il 29/12/2020 02:48, Bischoop ha scritto:

On 2020-12-28, Mats Wichmann  wrote:

On 12/28/20 10:46 AM, Marco Sulla wrote:

On Mon, 28 Dec 2020 at 17:37, Bischoop  wrote:


I'd like to check if there's "@" in a string and wondering if any method
is better/safer than others. I was told on one occasion that I should
use is than ==, so how would be on this example.

s = '[email protected]'


You could do simply

if "@" in s:

but probably what you really want is a regular expression.



Will add that Yes, you should always validate your inputs, but No, the
presence of an @ sign in a text string is not sufficient to know it's a
valid email address. Unfortunately validating that is hard.



Nah, by saying if is valid I meant exeactly if there's "@", I could add
yet if endswith() but at this point @ is enough.
Yes the only possible way for full validation would be just sending
email and waiting for reply.



you could try this way:

# ---
from dns import resolver as dns

emails=['[email protected]',
'[email protected]']

for ue in emails:
try:
mxl = dns.resolve(ue.split('@')[1], 'MX')
except:
print(f'{ue}: bad mail server')
else:
if len(mxl) > 0:
print(f'{ue}: valid mail server')
# ---

... so, having verified the sever, you should only worry about the name.
--
https://mail.python.org/mailman/listinfo/python-list


2 sample chi-square test

2020-12-29 Thread Priya Singh
Hi all,

I have two spectra with wavelength, flux, and error on flux. I want to find out 
the variability of these two spectra based on the 2 sample Chi-square test. 
I am using following code:

def compute_chi2_var(file1,file2,zemi,vmin,vmax):
w1,f1,e1,c1,vel1 = get_spec_vel(dir_data+file1,zemi)
id1 = np.where(np.logical_and(vel1 >= vmin, vel1 < vmax))[0]   
w2,f2,e2,c2,vel2 = get_spec_vel(dir_data+file2,zemi)
id2 = np.where(np.logical_and(vel2 >= vmin, vel2 < vmax))[0]
f_int = interp1d(w1[id1], f1[id1]/c1[id1], kind='cubic')
e_int = interp1d(w1[id1], e1[id1]/c1[id1], kind='cubic')  
f_obs,e_obs  = f_int(w2[id2]), e_int(w2[id2]) 
f_exp, e_exp = f2[id2]/c2[id2], e2[id2]/c2[id2]
e_net = e_obs**2 + e_exp**2
chi_square =  np.sum( (f_obs**2 -  f_exp**2)/e_net  )
dof = len(f_obs) - 1
pval = 1 - stats.chi2.cdf( chi_square, dof)
print('%.10E' % pval)

NN = 320
compute_chi2_var(file7[NN],file14[NN],zemi[NN],vmin[NN],vmax[NN])

I am running this code on many files, and I want to grab those pair of spectra 
where, the p-value of chi-squa is less than 10^(-8),  for the change to be 
unlikely due to a random occurrence.
Is my code right concept-wise? Because the chi-squ value is coming out to be 
very large (positive and negative), such that my p-value is always between 1 
and 0 which I know from other's results not correct.

Can anyone suggest me is the concept of 2-sample chi-squ applied by me is 
correct or not?

I thank you all in advance.
 
-- 
https://mail.python.org/mailman/listinfo/python-list


How to copy the entire outlook message content in python

2020-12-29 Thread nikhil k
Hello All,
I'm a beginner trying to achieve the below in my python script: Can anyone help 
me on this?  I'm stuck at step2.

1. Input: A locally saved outlook mail (*.msg) path
2. Go to the path and Copy the entire body of the mail 
3. Create a new mail and paste the contents into new mail 
4. send it manually

# 
# ===   Script Start ==
# 
import win32com.client as win32  

### Functions
def getMailBody(msgFile):
start_text = ""
end_text = ""
with open(msgFile) as f:
data=f.read()
return data[data.find(start_text):data.find(end_text)+len(end_text)]

def releaseMail(body, subject, recipient):
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = recipient
mail.Subject = subject
mail.HtmlBody = body
mail.Display(True)

### Main 
msgFile = "C:\\RELM\\testMsg.msg"
mailTo = "[email protected]"
mailSubject = "Test message"
mailBody = getMailBody(msgFile)
releaseMail(mailBody, mailSubject, mailRecipient)
# 
#  Script End ===  
# 


Below is the error I'm getting.
==
  File "C:\Python\Python38-32\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 924: 
character maps to 


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


How to uninstall python 2 package specifically?

2020-12-29 Thread Chris Green
I have two (or maybe even three) versions of Click installed:-

/usr/local/lib/python2.7/dist-packages/click
/usr/local/lib/python3.7/dist-packages/click
/usr/lib/python3/dist-packages/click

I run [x]ubuntu.

How can I uninstall those extra versions of click (which have
presumably been installed by pip, how would I know?).

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


Is there any way to check/de-cruft/update Python packages installed using pip?

2020-12-29 Thread Chris Green
I seem to have quite a lot of old python packages installed over the
years using pip and would like, if I can. to clear some of them out.


Is there any way to tell if a python package was installed by me
directly using pip or was installed from the [x]ubuntu repositories?
'pip list' just tells me every python package that's installed and
this really isn't much help.


Can one uninstall older versions?  For example I seem to have
three versions of Click installed:-

/usr/local/lib/python2.7/dist-packages/click
/usr/local/lib/python3.7/dist-packages/click
/usr/lib/python3/dist-packages/click

Presumably the ones in /usr/local have been pulled in as dependencies
when installing something else using pip, the /usr/lib one has been
installed from the Ubuntu repositories (again as a dependency). How
can I remove the ones in /usr/local?


If one updates a package using pip will it move it as appropriate from
(for example) /usr/local/lib/python3.7 to /usr/local/lib/python3.8?


Can pip (or some other tool) tell me what other python packages depend
on one I'm considering uninstalling?


If there are any tools/utilities one can install to check these things
out I'd love to know about them.

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


Why do I have both /usr/lib/python3 and /usr/lib/python3.8?

2020-12-29 Thread Chris Green
Why are there both /usr/lib/python3 and /usr/lib/python3.8 on my
x[ubuntu] system?

/usr/lib/python3 has just the dist-packages directory in it,
/usr/lib/python3.8 has lots of individual python files in it as well
as quite a number of directories.  

There's also a /usr/lib/python3.9 directory even though Ubuntu hasn't
moved to python3.9 yet.


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


Re: 2 sample chi-square test

2020-12-29 Thread Peter Pearson
On Tue, 29 Dec 2020 02:52:15 -0800 (PST), Priya Singh wrote:
[snip]
> I have two spectra with wavelength, flux, and error on flux. I want to
> find out the variability of these two spectra based on the 2 sample
> Chi-square test.  I am using following code:
>
> def compute_chi2_var(file1,file2,zemi,vmin,vmax):
> w1,f1,e1,c1,vel1 = get_spec_vel(dir_data+file1,zemi)
> id1 = np.where(np.logical_and(vel1 >= vmin, vel1 < vmax))[0]   
> w2,f2,e2,c2,vel2 = get_spec_vel(dir_data+file2,zemi)
> id2 = np.where(np.logical_and(vel2 >= vmin, vel2 < vmax))[0]
> f_int = interp1d(w1[id1], f1[id1]/c1[id1], kind='cubic')
> e_int = interp1d(w1[id1], e1[id1]/c1[id1], kind='cubic')  
> f_obs,e_obs  = f_int(w2[id2]), e_int(w2[id2]) 
> f_exp, e_exp = f2[id2]/c2[id2], e2[id2]/c2[id2]
> e_net = e_obs**2 + e_exp**2
> chi_square =  np.sum( (f_obs**2 -  f_exp**2)/e_net  )
> dof = len(f_obs) - 1
> pval = 1 - stats.chi2.cdf( chi_square, dof)
> print('%.10E' % pval)
>
> NN = 320
> compute_chi2_var(file7[NN],file14[NN],zemi[NN],vmin[NN],vmax[NN])
>
>
> I am running this code on many files, and I want to grab those pair of
> spectra where, the p-value of chi-squa is less than 10^(-8), for the
> change to be unlikely due to a random occurrence.
>
> Is my code right concept-wise? Because the chi-squ value is coming out
> to be very large (positive and negative), such that my p-value is
> always between 1 and 0 which I know from other's results not correct.
>
> Can anyone suggest me is the concept of 2-sample chi-squ applied by me
> is correct or not?

1. This is not really a Python question, is it?

2. Recommendation: test your chi-squared code on simpler sample data.

3. Observation: P-values *are* normally between 0 and 1.

4. Observation: chi-squared values are never negative.

5. Recommendation: Learn a little about the chi-squared distribution
   (but not on a Python newsgroup).  The chi-squared distribution with
   N degrees of freedom is the distribution expected for a quantity
   that is the sum of the squares of N normally distributed random
   variables with mean 0 and standard deviation 1.  If you expect
   f_obs to equal f_exp plus some normally distributed noise with
   mean 0 and standard deviation sigma, then (f_obs-f_exp)/sigma
   should be normally distributed with mean 0 and standard deviation 1.

6. Observation: (f_obs**2 -  f_exp**2)/e_net is probably not what
   you want, since it can be negative.  You probably want something
   like (f_obs-f_exp)**2/e_net.  But don't take my word for it.

Good luck.

-- 
To email me, substitute nowhere->runbox, invalid->com.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to copy the entire outlook message content in python

2020-12-29 Thread Peter Pearson
On Tue, 29 Dec 2020 05:38:53 -0800 (PST), nikhil k wrote:
...[snip]...
> import win32com.client as win32  
>
> ### Functions
> def getMailBody(msgFile):
> start_text = ""
> end_text = ""
> with open(msgFile) as f:
> data=f.read()
> return data[data.find(start_text):data.find(end_text)+len(end_text)]
...[snip]...
>
>
> Below is the error I'm getting.
>==
>   File "C:\Python\Python38-32\lib\encodings\cp1252.py", line 23, in decode
> return codecs.charmap_decode(input,self.errors,decoding_table)[0]
> UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 924: 
> character maps to 

I'm not completely sure that it's the f.read() call that produces
the error, because you cut out the earlier lines of the error
message, but . . .

I think the problem is that the data in the file you're reading
do not represent a valid "encoding" of any character string,
using whatever encoding convention Python thinks you want to use.
Maybe Python is assuming you're using ASCII encoding; 0x81 is
certainly not a valid ASCII character.

I don't know how Outlook represents messages in its .msg files;
it's possible it packs ASCII text along with binary data.

Maybe you can find a Python package that reads .msg files.

Maybe you could read the file as a bytestring instead of as a
character string.


-- 
To email me, substitute nowhere->runbox, invalid->com.
-- 
https://mail.python.org/mailman/listinfo/python-list


Learning why module will not load

2020-12-29 Thread Rich Shepard

Running Slackware-14.2/x86_64 and python-3.9.1. Installed are
six-1.14.0-x86_64-1_SBo and python3-six-1.13.0-x86_64-1_SBo (I don't know if
the latter is required because six is supposed to be available for python2
and python3.)

However, python3 doesn't find either one:

$ python3
Python 3.9.1 (default, Dec 26 2020, 11:21:00) 
[GCC 5.5.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

import six

Traceback (most recent call last):
  File "", line 1, in 
ModuleNotFoundError: No module named 'six'

import python3_six

Traceback (most recent call last):
  File "", line 1, in 
ModuleNotFoundError: No module named 'python3_six'

How do I diagnose why neither module is found?

TIA,

Rich


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


Re: Is there any way to check/de-cruft/update Python packages installed using pip?

2020-12-29 Thread Mats Wichmann

On 12/29/20 7:48 AM, Chris Green wrote:

I seem to have quite a lot of old python packages installed over the
years using pip and would like, if I can. to clear some of them out.


Is there any way to tell if a python package was installed by me
directly using pip or was installed from the [x]ubuntu repositories?
'pip list' just tells me every python package that's installed and
this really isn't much help.


Can one uninstall older versions?  For example I seem to have
three versions of Click installed:-

 /usr/local/lib/python2.7/dist-packages/click
 /usr/local/lib/python3.7/dist-packages/click
 /usr/lib/python3/dist-packages/click

Presumably the ones in /usr/local have been pulled in as dependencies
when installing something else using pip, the /usr/lib one has been
installed from the Ubuntu repositories (again as a dependency). How
can I remove the ones in /usr/local?


If one updates a package using pip will it move it as appropriate from
(for example) /usr/local/lib/python3.7 to /usr/local/lib/python3.8?


Can pip (or some other tool) tell me what other python packages depend
on one I'm considering uninstalling?


If there are any tools/utilities one can install to check these things
out I'd love to know about them.


There are some tools (on pypi naturally) for fiddling with installed 
packages, but for starters try these two:


python -m pip help list
python -m pip help show

show will list dependencies, among other bits of info.

For older versions, you to use the matching version of Python

python2.7 -m pip show click

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


Re: Is there any way to check/de-cruft/update Python packages installed using pip?

2020-12-29 Thread Chris Green
Mats Wichmann  wrote:
> On 12/29/20 7:48 AM, Chris Green wrote:
> > 
> > If there are any tools/utilities one can install to check these things
> > out I'd love to know about them.
> 
> There are some tools (on pypi naturally) for fiddling with installed 
> packages, but for starters try these two:
> 
> python -m pip help list
> python -m pip help show
> 
> show will list dependencies, among other bits of info.
> 
> For older versions, you to use the matching version of Python
> 
> python2.7 -m pip show click
> 
Ah, of course, run pip using the python version that matches what you
want to remove, obvious really! :-)   Thanks.

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


Re: Learning why module will not load

2020-12-29 Thread Terry Reedy

On 12/29/2020 1:11 PM, Rich Shepard wrote:

Running Slackware-14.2/x86_64 and python-3.9.1. Installed are
six-1.14.0-x86_64-1_SBo and python3-six-1.13.0-x86_64-1_SBo (I don't 
know if

the latter is required because six is supposed to be available for python2
and python3.)


Packages have to be installed for a particular Python binary in order 
for that binary to import the package.



However, python3 doesn't find either one:

$ python3
Python 3.9.1 (default, Dec 26 2020, 11:21:00) [GCC 5.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

import six

Traceback (most recent call last):
   File "", line 1, in 
ModuleNotFoundError: No module named 'six'

import python3_six

Traceback (most recent call last):
   File "", line 1, in 
ModuleNotFoundError: No module named 'python3_six'

How do I diagnose why neither module is found?


Those packages apparently are not installed for that 3.9.1 binary.


--
Terry Jan Reedy


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


Re: Is there any way to check/de-cruft/update Python packages installed using pip?

2020-12-29 Thread Matt Wheeler
On 29 Dec 2020, 14:48 +, Chris Green , wrote:
> I seem to have quite a lot of old python packages installed over the
> years using pip and would like, if I can. to clear some of them out.
>
>
> Is there any way to tell if a python package was installed by me
> directly using pip or was installed from the [x]ubuntu repositories?
> 'pip list' just tells me every python package that's installed and
> this really isn't much help.
>
>
> Can one uninstall older versions? For example I seem to have
> three versions of Click installed:-
>
> /usr/local/lib/python2.7/dist-packages/click
> /usr/local/lib/python3.7/dist-packages/click
> /usr/lib/python3/dist-packages/click

Everything under one of these directories comes from apt/dpkg packages.
Packages installed using pip will be installed in a directory called 
site-packages, not dist-packages

To confirm this you can try the command `dpkg -S `, which will tell you 
which package a file belongs to


>
> If one updates a package using pip will it move it as appropriate from
> (for example) /usr/local/lib/python3.7 to /usr/local/lib/python3.8?

No, each version of pip will completely ignore any packages installed for a 
different version of Python
> Can pip (or some other tool) tell me what other python packages depend
> on one I'm considering uninstalling?

pipdeptree can do this
> If there are any tools/utilities one can install to check these things
> out I'd love to know about them.
>
> --
> Chris Green
> ·
> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to uninstall python 2 package specifically?

2020-12-29 Thread Pankaj Jangid
Chris Green  writes:

> I have two (or maybe even three) versions of Click installed:-
>
> /usr/local/lib/python2.7/dist-packages/click
> /usr/local/lib/python3.7/dist-packages/click
> /usr/lib/python3/dist-packages/click
>
> I run [x]ubuntu.
>
> How can I uninstall those extra versions of click (which have
> presumably been installed by pip, how would I know?).

For python2 package use pip, for python3 packages use pip3.

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


Re: Learning why module will not load

2020-12-29 Thread Rich Shepard

On Tue, 29 Dec 2020, Terry Reedy wrote:

Packages have to be installed for a particular Python binary in order for 
that binary to import the package.


Terry,

I forgot about this as I don't often upgrade Python.


Those packages apparently are not installed for that 3.9.1 binary.


This will be corrected today.

Thanks.

Stay well,

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


Re: Is there any way to check/de-cruft/update Python packages installed using pip?

2020-12-29 Thread Chris Green
Matt Wheeler  wrote:
> On 29 Dec 2020, 14:48 +, Chris Green , wrote:
> > I seem to have quite a lot of old python packages installed over the
> > years using pip and would like, if I can. to clear some of them out.
> >
> >
> > Is there any way to tell if a python package was installed by me
> > directly using pip or was installed from the [x]ubuntu repositories?
> > 'pip list' just tells me every python package that's installed and
> > this really isn't much help.
> >
> >
> > Can one uninstall older versions? For example I seem to have
> > three versions of Click installed:-
> >
> > /usr/local/lib/python2.7/dist-packages/click
> > /usr/local/lib/python3.7/dist-packages/click
> > /usr/lib/python3/dist-packages/click
> 
> Everything under one of these directories comes from apt/dpkg packages.
> Packages installed using pip will be installed in a directory called 
> site-packages, 
> not dist-packages 
> 
> To confirm this you can try the command `dpkg -S `, which will tell 
> you which package a file belongs to 
> 
Aaaah  :-)   I hadn't realised *that* was the significance of
dist-packages and site-packages, thank you very much, that helps a
huge amount.


> 
> >
> > If one updates a package using pip will it move it as appropriate from
> > (for example) /usr/local/lib/python3.7 to /usr/local/lib/python3.8?
> 
> No, each version of pip will completely ignore any packages installed for 
> a different version of Python 

Again something I hadn't really understood before but obvious when you
think about it.  Thank you again.

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


I'm finally disentangled from Python 2, thank you everyone

2020-12-29 Thread Chris Green
Well, it has taken me a while, but I now seem to have finally detached
myself from any Python 2 dependencies on my various systems.

Firstly may I say thank you to everyone who has helped me with this
(and with other issues) here on the Python list, you are a friendly
and helpful group of people.

I run (mostly) xubuntu systems and, as of xubuntu 20.04 Python 2
should have gone.

First I changed all of my code to use Python 3, this was mostly pretty
trivial (like my code in many ways!), the only slightly difficult
areas were the changes to Gtk and the string->bytes thing which I had
a problem with but, with help from here, was sorted fairly easily.

As regards installed software on my laptop it was easy, I don't quite
know when it all went but I didn't have to do anything, the current
xubuntu 20.10 installation has no Python 2 on it at all.

On my desktop machine it was a bit more difficult because of the Oki
scanner utility which I have asked about quite a lot here.  It has a
.so file built for Python 2 which I couldn't convert.  In the end
(following ideas from here) I have packaged it using cx-freeze so that
the utility and all the libraries etc. that it needs are run 'as a
package' with all the old Python 2 dependencies inside the package. It
took me a while to get it all packaged but it is now done and the
utility runs successfully on my desktop which is 'Python 2 free'
(except of course for files within the Oki utility package).

Phew! :-)


... and thanks again for all the help and support here.

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


Re: Learning why module will not load

2020-12-29 Thread Barry


> On 29 Dec 2020, at 18:14, Rich Shepard  wrote:
> 
> Running Slackware-14.2/x86_64 and python-3.9.1. Installed are
> six-1.14.0-x86_64-1_SBo and python3-six-1.13.0-x86_64-1_SBo (I don't know if
> the latter is required because six is supposed to be available for python2
> and python3.)
> 
> However, python3 doesn't find either one:
> 
> $ python3
> Python 3.9.1 (default, Dec 26 2020, 11:21:00) [GCC 5.5.0] on linux
> Type "help", "copyright", "credits" or "license" for more information.
 import six
> Traceback (most recent call last):
>  File "", line 1, in 
> ModuleNotFoundError: No module named 'six'
 import python3_six
> Traceback (most recent call last):
>  File "", line 1, in 
> ModuleNotFoundError: No module named 'python3_six'
> 
> How do I diagnose why neither module is found?

I do this to to debug odd import issues:

$ python3.9 -v
...
>>> import six

The output will show the attempts python makes to find the six module.

Barry

> 
> TIA,
> 
> Rich
> 
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 

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


Re: Learning why module will not load

2020-12-29 Thread Rich Shepard

On Tue, 29 Dec 2020, Barry wrote:


I do this to to debug odd import issues:
$ python3.9 -v
...

import six

The output will show the attempts python makes to find the six module.


Barry,

Thank you. It's not finding six and I am rebuilding all python3 packages
since almost none were build using python-3.9.1.

Stay well,

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


Re: I'm finally disentangled from Python 2, thank you everyone

2020-12-29 Thread Cameron Simpson
On 29Dec2020 21:20, Chris Green  wrote:
>Well, it has taken me a while, but I now seem to have finally detached
>myself from any Python 2 dependencies on my various systems.
[...]
>On my desktop machine it was a bit more difficult because of the Oki
>scanner utility which I have asked about quite a lot here.  It has a
>.so file built for Python 2 which I couldn't convert.  In the end
>(following ideas from here) I have packaged it using cx-freeze so that
>the utility and all the libraries etc. that it needs are run 'as a
>package' with all the old Python 2 dependencies inside the package.  
>[...]

Could I ask you to write up a post on what you did here? I've never used 
cx-freeze but it sounds like a useful thing for keeping legacy stuff 
functioning. A writeup from someone who's actually used it for that 
would be welcome.

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