Catching an exception in a variable

2017-08-04 Thread ast

Hello

try:
a = 1 / 0
except ZeroDivisionError as ex:
   pass


ex

Traceback (most recent call last):
 File "", line 1, in 
   ex
NameError: name 'ex' is not defined


Why variable ex doesn't exist ?
--
https://mail.python.org/mailman/listinfo/python-list


program that search string in text file and do something

2017-08-04 Thread alon . najman
Hi, I'm new to thing forum and to this programming in python!

can someone help me and write me how to write a program that do:
- search for a string in certain text file and if it founds the string it 
delete the file? and print something?

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


Re: Catching an exception in a variable

2017-08-04 Thread Ben Finney
"ast"  writes:

> Why variable ex doesn't exist ?

Because of a deliberate decision made to delete it. Silently.

This is documented:

When an exception has been assigned using as target, it is cleared
at the end of the except clause. This is as if

except E as N:
foo

was translated to

except E as N:
try:
foo
finally:
del N

This means the exception must be assigned to a different name to be
able to refer to it after the except clause. Exceptions are cleared
because with the traceback attached to them, they form a reference
cycle with the stack frame, keeping all locals in that frame alive
until the next garbage collection occurs.


https://docs.python.org/3/reference/compound_stmts.html#the-try-statement>

but I think this is a terrible idea. It silently deletes a name binding
from the current scope, without the code explicitly asking for that.

-- 
 \ “For certain people, after fifty, litigation takes the place of |
  `\ sex.” —Gore Vidal |
_o__)  |
Ben Finney

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


Re: SSL/TLS support in Pyro4

2017-08-04 Thread Robin Becker

On 03/08/2017 19:30, Irmen de Jong wrote:
.


I wonder if any current (or new) users of Pyro4 want to check this out? The 
biggest
concern I have is that I only have dummy (self-signed) certificates so I can't 
test it
with "real" certs to see if the validation works correctly.

..

I've used self created authorities with mariadb and mongo to secure local 
clusters. Could this provide private secure certs for pyro?

--
Robin Becker

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


Re: program that search string in text file and do something

2017-08-04 Thread ast


 a écrit dans le message de 
news:b6cc4ee5-71be-4550-be3e-59ebeee7a...@googlegroups.com...

Hi, I'm new to thing forum and to this programming in python!

can someone help me and write me how to write a program that do:
- search for a string in certain text file and if it founds the string it delete the file? and 
print something?


thanks.


import os
pattern = "azerty"
found = False

with open("foo.txt", "r") as f:
   for line in f:
   if pattern in line:
   found = True
   break

if found:
   os.remove("foo.txt")




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


Re: Catching an exception in a variable

2017-08-04 Thread ast
"Ben Finney"  a écrit dans le message de 
news:mailman.55.1501834898.28999.python-l...@python.org...

"ast"  writes:



Ok, ty 


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


Re: program that search string in text file and do something

2017-08-04 Thread Peter Otten
alon.naj...@gmail.com wrote:

> Hi, I'm new to thing forum and to this programming in python!
> 
> can someone help me and write me how to write a program that do:
> - search for a string in certain text file and if it founds the string it
> delete the file? and print something?

Programming is mostly about splitting a complex task into baby steps.

Do you know how to open a file?
Do you know how to iterate over the lines of that file?
Do you know how to search for a string in that line (i. e. another string)?
Do you know how to delete a file?

Work through a Python tutorial or the first chapters of beginner's textbook, 
then try your hand at each of the subtasks outlined above. Consult the 
documentation for missing parts. 

Once you have some code we will help you improve or even fix it.

What we won't do is write a program for you ready to present to your 
teacher.

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


Re: program that search string in text file and do something

2017-08-04 Thread Peter Otten
Peter Otten wrote:

> What we won't do is write a program for you ready to present to your
> teacher.

I should have known better :( 


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


Re: program that search string in text file and do something

2017-08-04 Thread alon . najman
On Friday, August 4, 2017 at 12:27:02 PM UTC+3, ast wrote:
>  a écrit dans le message de 
> news:b6cc4ee5-71be-4550-be3e-59ebeee7a...@googlegroups.com...
> > Hi, I'm new to thing forum and to this programming in python!
> >
> > can someone help me and write me how to write a program that do:
> > - search for a string in certain text file and if it founds the string it 
> > delete the file? and 
> > print something?
> >
> > thanks.
> 
> import os
> pattern = "azerty"
> found = False
> 
> with open("foo.txt", "r") as f:
> for line in f:
> if pattern in line:
> found = True
> break
> 
> if found:
> os.remove("foo.txt")

thanks man! that works
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: SSL/TLS support in Pyro4

2017-08-04 Thread Irmen de Jong
On 04/08/2017 10:26, Robin Becker wrote:
> On 03/08/2017 19:30, Irmen de Jong wrote:
> .
>>
>> I wonder if any current (or new) users of Pyro4 want to check this out? The 
>> biggest
>> concern I have is that I only have dummy (self-signed) certificates so I 
>> can't test it
>> with "real" certs to see if the validation works correctly.
> ..
> 
> I've used self created authorities with mariadb and mongo to secure local 
> clusters.
> Could this provide private secure certs for pyro?

Hi Robin

I am not sure how this is any benefit over the self-signed root certs that I 
now use?

Except for the fact that these are a root cert as well and don't use any CA 
trust chain.
To be able to validate this cert, I have to load it as a CA cert on the 
validating side.
Which isn't bad perse.

I've used openssl as mentioned here to create my certs:
https://docs.python.org/3.7/library/ssl.html#self-signed-certificates


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


Re: Catching an exception in a variable

2017-08-04 Thread Steve D'Aprano
On Fri, 4 Aug 2017 06:21 pm, Ben Finney wrote about the unbinding of exception
variable on leaving the except block:

> I think this is a terrible idea.


I agree it is a terrible idea. I'd go so far as to say it is the worst possible
way for dealing with the program of exception reference loops, except for all
the others.

*wink*



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: program that search string in text file and do something

2017-08-04 Thread ast


 a écrit dans le message de 
news:f705c092-de18-4c37-bde1-42316e8de...@googlegroups.com...

On Friday, August 4, 2017 at 12:27:02 PM UTC+3, ast wrote:

 a écrit dans le message de
news:b6cc4ee5-71be-4550-be3e-59ebeee7a...@googlegroups.com...



thanks man! that works


I hope it is not a school homework 


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


how to guess the number of cluster when do not know?

2017-08-04 Thread Ho Yeung Lee
i find kmeans has to input number of cluster
but i do not know how many words in photo before recognization in application 
of robots vision recognition

if separate high, will it become letters instead of word?


from pylab import plot,show
from numpy import vstack,array
from numpy.random import rand
from scipy.cluster.vq import kmeans,vq

# data generation
data = vstack((rand(150,2) + array([.5,.5]),rand(150,2)))

# computing K-Means with K = 2 (2 clusters)
centroids,_ = kmeans(data,2)
-- 
https://mail.python.org/mailman/listinfo/python-list


[OT] Re: how to guess the number of cluster when do not know?

2017-08-04 Thread Alain Ketterlin
Ho Yeung Lee  writes:

> i find kmeans has to input number of cluster
[...]

https://en.wikipedia.org/wiki/Determining_the_number_of_clusters_in_a_data_set

Completely off-topic on this group/list, please direct your questions
elsewhere.

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


how to grep

2017-08-04 Thread Iranna Mathapati
Hi Team,

How to grep values from below out put string.

pattern should include  "Fabric Module".

grepping Fabric module values only

str = '''
22   0Fabric Module J8N-C9508-FM  ok

24   0Fabric ModuleJ8N-C9508-FM  ok

26   0Fabric ModuleJ8N-C9508-FM  ok

22   016-slot Fabric Module J8N-C9516-FM-Eok

24   016-slot Fabric Module J8N-C9516-FM-Eok'''

output = [22,24,26,22,24]


Thanks
Iranna M
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to grep

2017-08-04 Thread Steve D'Aprano
On Fri, 4 Aug 2017 10:33 pm, Iranna Mathapati wrote:

> Hi Team,
> 
> How to grep values from below out put string.
> 
> pattern should include  "Fabric Module".
> 
> grepping Fabric module values only
> 
> str = '''
> 22   0Fabric Module J8N-C9508-FM  ok
> 24   0Fabric ModuleJ8N-C9508-FM  ok
> 26   0Fabric ModuleJ8N-C9508-FM  ok
> 22   016-slot Fabric Module J8N-C9516-FM-Eok
> 24   016-slot Fabric Module J8N-C9516-FM-Eok'''
> 
> output = [22,24,26,22,24]


output = int(line[:2]) for line in string.split('\n') if line]



Does that help?

If not, you will need to ask a better question, because I cannot understand the
question you are asking now.





-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: SSL/TLS support in Pyro4

2017-08-04 Thread Irmen de Jong
On 03/08/2017 20:30, Irmen de Jong wrote:

> Alternatively, is there a cheap way to get an 'official' SSL certificate for 
> testing
> purposes.  I don't think letsencrypt can help here because it is only for web 
> sites?
> (and their certs are only valid for a very short period)

With some host file trickery (had to fool my dev machine into thinking it is my 
web
server) I managed to get it all to work with a letsencrypt cert as well.

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


Re: SSL/TLS support in Pyro4

2017-08-04 Thread Robin Becker

..


Hi Robin

I am not sure how this is any benefit over the self-signed root certs that I 
now use?

Except for the fact that these are a root cert as well and don't use any CA 
trust chain.
To be able to validate this cert, I have to load it as a CA cert on the 
validating side.
Which isn't bad perse.

I've used openssl as mentioned here to create my certs:
https://docs.python.org/3.7/library/ssl.html#self-signed-certificates
.Welle I was thinking perhaps you had trouble with self signed certs for 
some reason. I only used CA type setup because some recipe for mongo clusters 
seems to want that. I think the mariadb clusters were fine with simple self 
signed certs. However, if I control the cluster can I not just distribute the 
cert to all members and have them validate it against itself or does python 
refuse to do that? I vaguely remember some python apis allow the authority chain 
to be specified.

--
Robin Becker

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


Re: program that search string in text file and do something

2017-08-04 Thread Grant Edwards
On 2017-08-04, Peter Otten <__pete...@web.de> wrote:

> What we won't do is write a program for you ready to present to your 
> teacher.

Or if we do, it will be subtly sabotaged in a manner that will make it
obvious to an experienced Python programmer that you didn't write it.

My favorite is to use some combination of particularly obscure and
obtuse mechanisms that will actually product the correct results but
do so in a way that nobody (including myself a few days later) will be
able to explain without some intense study.

-- 
Grant Edwards   grant.b.edwardsYow! It don't mean a
  at   THING if you ain't got
  gmail.comthat SWING!!

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


Re: how to sort a list of tuples with custom function

2017-08-04 Thread Ho Yeung Lee
i had changed to use kmeans

https://gist.github.com/hoyeunglee/2475391ad554e3d2b2a40ec24ab47940

i do not know whether write it correctly
but it seems can cluster to find words in window, but not perfect


On Wednesday, August 2, 2017 at 3:06:40 PM UTC+8, Peter Otten wrote:
> Glenn Linderman wrote:
> 
> > On 8/1/2017 2:10 PM, Piet van Oostrum wrote:
> >> Ho Yeung Lee  writes:
> >>
> >>> def isneighborlocation(lo1, lo2):
> >>>  if abs(lo1[0] - lo2[0]) < 7  and abs(lo1[1] - lo2[1]) < 7:
> >>>  return 1
> >>>  elif abs(lo1[0] - lo2[0]) == 1  and lo1[1] == lo2[1]:
> >>>  return 1
> >>>  elif abs(lo1[1] - lo2[1]) == 1  and lo1[0] == lo2[0]:
> >>>  return 1
> >>>  else:
> >>>  return 0
> >>>
> >>>
> >>> sorted(testing1, key=lambda x: (isneighborlocation.get(x[0]), x[1]))
> >>>
> >>> return something like
> >>> [(1,2),(3,3),(2,5)]
> 
> >> I think you are trying to sort a list of two-dimensional points into a
> >> one-dimensiqonal list in such a way thet points that are close together
> >> in the two-dimensional sense will also be close together in the
> >> one-dimensional list. But that is impossible.
> 
> > It's not impossible, it just requires an appropriate distance function
> > used in the sort.
> 
> That's a grossly misleading addition. 
> 
> Once you have an appropriate clustering algorithm
> 
> clusters = split_into_clusters(items) # needs access to all items
> 
> you can devise a key function
> 
> def get_cluster(item, clusters=split_into_clusters(items)):
> return next(
> index for index, cluster in enumerate(clusters) if item in cluster
> )
> 
> such that
> 
> grouped_items = sorted(items, key=get_cluster)
> 
> but that's a roundabout way to write
> 
> grouped_items = sum(split_into_clusters(items), [])
> 
> In other words: sorting is useless, what you really need is a suitable 
> approach to split the data into groups. 
> 
> One well-known algorithm is k-means clustering:
> 
> https://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.vq.kmeans.html
> 
> Here is an example with pictures:
> 
> https://dzone.com/articles/k-means-clustering-scipy

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


Re: [OT] Re: how to guess the number of cluster when do not know?

2017-08-04 Thread Ho Yeung Lee
actually i am using python's kmeans library. it is relevant in python

i had changed to use kmeans

https://gist.github.com/hoyeunglee/2475391ad554e3d2b2a40ec24ab47940

i do not know whether write it correctly
but it seems can cluster to find words in window, but not perfect



On Friday, August 4, 2017 at 8:24:54 PM UTC+8, Alain Ketterlin wrote:
> Ho Yeung Lee  writes:
> 
> > i find kmeans has to input number of cluster
> [...]
> 
> https://en.wikipedia.org/wiki/Determining_the_number_of_clusters_in_a_data_set
> 
> Completely off-topic on this group/list, please direct your questions
> elsewhere.
> 
> -- Alain.

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


Re: SSL/TLS support in Pyro4

2017-08-04 Thread Irmen de Jong
On 04/08/2017 15:44, Robin Becker wrote:
> ..
>>
>> Hi Robin
>>
>> I am not sure how this is any benefit over the self-signed root certs that I 
>> now use?
>>
>> Except for the fact that these are a root cert as well and don't use any CA 
>> trust chain.
>> To be able to validate this cert, I have to load it as a CA cert on the 
>> validating side.
>> Which isn't bad perse.
>>
>> I've used openssl as mentioned here to create my certs:
>> https://docs.python.org/3.7/library/ssl.html#self-signed-certificates
> .Welle I was thinking perhaps you had trouble with self signed certs 
> for some
> reason. I only used CA type setup because some recipe for mongo clusters 
> seems to want
> that. I think the mariadb clusters were fine with simple self signed certs. 
> However, if
> I control the cluster can I not just distribute the cert to all members and 
> have them
> validate it against itself or does python refuse to do that? I vaguely 
> remember some
> python apis allow the authority chain to be specified.

You can specify a CAcert using load_verify_locations on the ssl context. Is 
that what
you meant? I figured out that if you set that to the peer's certificate it will 
then be
accepted.  I understand it as much as "hey openssl here is a root cert that you 
should
trust if you encounter it".
Without doing this, the cert is denied on the SSL level (unless you set the ssl 
options
to no-cert-required but that is definitely not what I wanted)

Bottom line is I learned something new :)

And also that Python's standard ssl library isn't as bad as I remember it to be 
a few
years ago.  Is there still a reason to use, say, PyOpenSSL anymore?


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


Re: [OT] Re: how to guess the number of cluster when do not know?

2017-08-04 Thread Ho Yeung Lee
i use number of clusters = 120

https://drive.google.com/file/d/0Bxs_ao6uuBDUZFByNVgzd0Jrdm8/view?usp=sharing

seems better, but still has a long distance to be perfect

On Friday, August 4, 2017 at 10:09:59 PM UTC+8, Ho Yeung Lee wrote:
> actually i am using python's kmeans library. it is relevant in python
> 
> i had changed to use kmeans
> 
> https://gist.github.com/hoyeunglee/2475391ad554e3d2b2a40ec24ab47940
> 
> i do not know whether write it correctly
> but it seems can cluster to find words in window, but not perfect
> 
> 
> 
> On Friday, August 4, 2017 at 8:24:54 PM UTC+8, Alain Ketterlin wrote:
> > Ho Yeung Lee  writes:
> > 
> > > i find kmeans has to input number of cluster
> > [...]
> > 
> > https://en.wikipedia.org/wiki/Determining_the_number_of_clusters_in_a_data_set
> > 
> > Completely off-topic on this group/list, please direct your questions
> > elsewhere.
> > 
> > -- Alain.

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


Re: how to fast processing one million strings to remove quotes

2017-08-04 Thread Tim Daneliuk
On 08/04/2017 01:52 AM, Peter Otten wrote:


> It looks like Python is fairly competetive:
> 
> $ wc -l hugequote.txt 
> 100 hugequote.txt612250
> $ cat unquote.py 
> import csv
> 
> with open("hugequote.txt") as instream:
> for field, in csv.reader(instream):
> print(field)
> 
> $ time python3 unquote.py > /dev/null
> 
> real0m3.773s
> user0m3.665s
> sys 0m0.082s
> 
> $ time cat hugequote.txt | sed 's/"""/"/g;s/""/"/g' > /dev/null
> 
> real0m4.862s
> user0m4.721s
> sys 0m0.330s
> 
> Run on ancient AMD hardware ;)
> 

It's actually better than sed.  What you're seeing is - I believe -
load time dominating the overall time.  I reran this with a 20M line
file:

time cat superhuge.txt | sed 's/"""/"/g;s/""/"/g' >/dev/null

real0m53.091s
user0m52.861s
sys 0m0.820s


time python unquote.py >/dev/null

real0m22.377s
user0m22.021s
sys 0m0.352s

Note that this is with python2, not python3.  Also, I confirmed that the
cat and pipe into sed was not a factor in the performance.

My guess is that delimiter recognition logic in the csv module is far
more efficient than the general purpose regular expression/dfa
implementaton in sed.

Extra Credit Assignment:

Reimplement in python using:

- string substitution
- regular expressions


Tschüss...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to group by function if one of the group has relationship with another one in the group?

2017-08-04 Thread Ho Yeung Lee
On Wednesday, August 2, 2017 at 5:25:21 AM UTC+8, Piet van Oostrum wrote:
> Ho Yeung Lee  writes:
> 
> > which function should be used for this problem?
> >
> I think it is a kind if clustering, or a connectivity problem. There are 
> special algorithms for that, not just a simple function. Maybe scikit-learn 
> has a suitable algorithm for it.
> -- 
> Piet van Oostrum 
> WWW: http://piet.vanoostrum.org/
> PGP key: [8DAE142BE17999C4]


i use number of clusters = 120

https://gist.github.com/hoyeunglee/2475391ad554e3d2b2a40ec24ab47940
https://drive.google.com/file/d/0Bxs_ao6uuBDUZFByNVgzd0Jrdm8/view?usp=sharing

seems better, but still has a long distance to be perfect
is there any more that can do for perfect
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to sort a list of tuples with custom function

2017-08-04 Thread Ho Yeung Lee
On Friday, August 4, 2017 at 10:08:56 PM UTC+8, Ho Yeung Lee wrote:
> i had changed to use kmeans
> 
> https://gist.github.com/hoyeunglee/2475391ad554e3d2b2a40ec24ab47940
> 
> i do not know whether write it correctly
> but it seems can cluster to find words in window, but not perfect
> 
> 
> On Wednesday, August 2, 2017 at 3:06:40 PM UTC+8, Peter Otten wrote:
> > Glenn Linderman wrote:
> > 
> > > On 8/1/2017 2:10 PM, Piet van Oostrum wrote:
> > >> Ho Yeung Lee  writes:
> > >>
> > >>> def isneighborlocation(lo1, lo2):
> > >>>  if abs(lo1[0] - lo2[0]) < 7  and abs(lo1[1] - lo2[1]) < 7:
> > >>>  return 1
> > >>>  elif abs(lo1[0] - lo2[0]) == 1  and lo1[1] == lo2[1]:
> > >>>  return 1
> > >>>  elif abs(lo1[1] - lo2[1]) == 1  and lo1[0] == lo2[0]:
> > >>>  return 1
> > >>>  else:
> > >>>  return 0
> > >>>
> > >>>
> > >>> sorted(testing1, key=lambda x: (isneighborlocation.get(x[0]), x[1]))
> > >>>
> > >>> return something like
> > >>> [(1,2),(3,3),(2,5)]
> > 
> > >> I think you are trying to sort a list of two-dimensional points into a
> > >> one-dimensiqonal list in such a way thet points that are close together
> > >> in the two-dimensional sense will also be close together in the
> > >> one-dimensional list. But that is impossible.
> > 
> > > It's not impossible, it just requires an appropriate distance function
> > > used in the sort.
> > 
> > That's a grossly misleading addition. 
> > 
> > Once you have an appropriate clustering algorithm
> > 
> > clusters = split_into_clusters(items) # needs access to all items
> > 
> > you can devise a key function
> > 
> > def get_cluster(item, clusters=split_into_clusters(items)):
> > return next(
> > index for index, cluster in enumerate(clusters) if item in cluster
> > )
> > 
> > such that
> > 
> > grouped_items = sorted(items, key=get_cluster)
> > 
> > but that's a roundabout way to write
> > 
> > grouped_items = sum(split_into_clusters(items), [])
> > 
> > In other words: sorting is useless, what you really need is a suitable 
> > approach to split the data into groups. 
> > 
> > One well-known algorithm is k-means clustering:
> > 
> > https://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.vq.kmeans.html
> > 
> > Here is an example with pictures:
> > 
> > https://dzone.com/articles/k-means-clustering-scipy


i use number of clusters = 120

https://gist.github.com/hoyeunglee/2475391ad554e3d2b2a40ec24ab47940
https://drive.google.com/file/d/0Bxs_ao6uuBDUZFByNVgzd0Jrdm8/view?usp=sharing

using my previous is not suitable for english words
but using kmeans is better, however not perfect to cluster words, 
some words are missing
-- 
https://mail.python.org/mailman/listinfo/python-list


Challenge: find the first value where two functions differ

2017-08-04 Thread Steve D'Aprano
This is a challenge for which I don't have a complete answer, only a partial
answer.

Here are two functions for calculating the integer square root of a non-negative
int argument. The first is known to be exact but may be a bit slow:

def isqrt_newton(n):
"""Integer sqrt using Newton's Method."""
if n == 0:
return 0
bits = n.bit_length()
a, b = divmod(bits, 2)
x = 2**(a+b)
while True:
y = (x + n//x)//2
if y >= x:
return x
x = y


The second is only exact for some values of n, and for sufficiently large values
of n, is will fail altogether:


import math

def isqrt_float(n):
"""Integer square root using floating point sqrt."""
return int(math.sqrt(n))



We know that:

- for n <= 2**53, isqrt_float(n) is exact;

- for n >= 2**1024, isqrt_float(n) will raise OverflowError;

- between those two values, 2**53 < n < 2**1024, isqrt_float(n) 
  will sometimes be exact, and sometimes not exact;

- there is some value, let's call it M, which is the smallest
  integer where isqrt_float is not exact.


Your mission, should you choose to accept it, is to find M.

Hint: a linear search starting at 2**53 will find it -- eventually. But it might
take a long time. Personally I gave up after five minutes, but for all I know
if I had a faster computer I'd already have the answer.

(But probably not.)


Another hint: if you run this code:


for i in range(53, 1024):
n = 2**i
if isqrt_newton(n) != isqrt_float(n):
print(n)
break


you can find a much better upper bound for M:

2**53 < M <= 2**105

which is considerable smaller that the earlier upper bound of 2**1024. But even
so, that's still 40564819207303331840695247831040 values to be tested.

On the assumption that we want a solution before the return of Halley's Comet,
how would you go about finding M?




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: SSL/TLS support in Pyro4

2017-08-04 Thread Robin Becker

On 04/08/2017 15:12, Irmen de Jong wrote:

On 04/08/2017 15:44, Robin Becker wrote:

..

You can specify a CAcert using load_verify_locations on the ssl context. Is 
that what
you meant? I figured out that if you set that to the peer's certificate it will 
then be


yes I think so. Certainly the self signed certs I tried with python3 urllib 
seemed to require valid hostnames. If I just use this as server



from http.server import HTTPServer, BaseHTTPRequestHandler, 
SimpleHTTPRequestHandler
import ssl


httpd = HTTPServer(('localhost', 4443), SimpleHTTPRequestHandler)

httpd.socket = ssl.wrap_socket (httpd.socket,
keyfile="/home/rptlab/tmp/key.pem",
certfile='/home/rptlab/tmp/cert.pem', server_side=True)

httpd.serve_forever()

and this as requester

from urllib import request
req = request.urlopen('https://localhost:4443',
   cafile='/home/rptlab/tmp/cert.pem')
print(req.read())


then provided the self signed cert has the name localhost requests can be made 
OK.

I'm guessing this would also work OK if the cert had multiple names embedded in 
it which would allow a small cluster to be used.


I don't know which part of the socket does the host name checking, but perhaps 
that can be turned off somewhere.




accepted.  I understand it as much as "hey openssl here is a root cert that you 
should
trust if you encounter it".
Without doing this, the cert is denied on the SSL level (unless you set the ssl 
options
to no-cert-required but that is definitely not what I wanted)

Bottom line is I learned something new :)

And also that Python's standard ssl library isn't as bad as I remember it to be 
a few
years ago.  Is there still a reason to use, say, PyOpenSSL anymore?


it's getting better any how.



Irmen



--
Robin Becker

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


PyYaml not using Yaml 1.2?

2017-08-04 Thread leam hall
Getting in to Ansible and back into Python. Ansible uses pyyaml which says
it parses yaml version 1.1. Is there a reason it doesn't do yaml version
1.2?

Thanks!

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


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread Chris Angelico
On Sat, Aug 5, 2017 at 12:51 AM, Steve D'Aprano
 wrote:
> def isqrt_float(n):
> """Integer square root using floating point sqrt."""
> return int(math.sqrt(n))
>
>
>
> We know that:
>
> - for n <= 2**53, isqrt_float(n) is exact;
>
> - for n >= 2**1024, isqrt_float(n) will raise OverflowError;
>
> - between those two values, 2**53 < n < 2**1024, isqrt_float(n)
>   will sometimes be exact, and sometimes not exact;
>
> - there is some value, let's call it M, which is the smallest
>   integer where isqrt_float is not exact.
>
>
> Your mission, should you choose to accept it, is to find M.

Hmm. Thinking aloud a bit here. We know that isqrt_float(n) is not
technically *exact* for any n that is not a square. So what I'd do is
assume that for (n*n+1) to (n+1)*(n+1)-1, it's going to return the
same value. I would then probe every perfect square, and the values
one above and one below it. Now, that's still probably too many to
check, but it's going to be a lot faster; for starters, we don't
actually need to use isqrt_newton to compare against it.

for root in range(2**26, 2**53):
square = root * root
if isqrt_float(square - 1) != root - 1:
print(square - 1)
break
if isqrt_float(square) != root:
print(square)
break
if isqrt_float(square + 1) != root:
print(square + 1)
break

That gave me this result almost instantaneously:

4503599761588224

which has been rounded up instead of down. I don't know if that counts
as sufficiently wrong?

>>> isqrt_float(4503599761588224)
67108865
>>> isqrt_newton(4503599761588224)
67108864
>>> 67108865 ** 2
4503599761588225

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


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread Chris Angelico
On Sat, Aug 5, 2017 at 1:44 AM, Chris Angelico  wrote:
> That gave me this result almost instantaneously:
>
> 4503599761588224
>
> which has been rounded up instead of down. I don't know if that counts
> as sufficiently wrong?

Oh, and I forgot to say: I have no actual *proof* that this is the
lowest number for which this will occur. It does not occur with
4503599761588223 (the next lower integer), but could happen with
something between two squares, for all I know. However, I suspect that
it wouldn't.

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


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread Rhodri James

On 04/08/17 15:51, Steve D'Aprano wrote:

Another hint: if you run this code:


for i in range(53, 1024):
 n = 2**i
 if isqrt_newton(n) != isqrt_float(n):
 print(n)
 break


you can find a much better upper bound for M:

 2**53 < M <= 2**105

which is considerable smaller that the earlier upper bound of 2**1024. But even
so, that's still 40564819207303331840695247831040 values to be tested.

On the assumption that we want a solution before the return of Halley's Comet,
how would you go about finding M?


I tried a variation of what you used to cut down the search space further:

for j in range(0, 53):
  for i in range(53, 105):
n = 2**i + 2**j
if isqrt_newton(n) != isqrt_float(n):
  print(n, i, j)
  sys.exit(1)

which gave me a new upper bound of 2**54+2**28.  Reversing the order of 
the loops would probably have been more sensible, but what the heck. 
That's only (!) 9007199523176448 values to test, which my machine seemed 
to be quite happy with, but you could repeat the exercise with more 
layers if you really wanted to.


--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread Ian Kelly
On Fri, Aug 4, 2017 at 8:51 AM, Steve D'Aprano
 wrote:
> This is a challenge for which I don't have a complete answer, only a partial
> answer.
>
> Here are two functions for calculating the integer square root of a 
> non-negative
> int argument. The first is known to be exact but may be a bit slow:
>
> def isqrt_newton(n):
> """Integer sqrt using Newton's Method."""
> if n == 0:
> return 0
> bits = n.bit_length()
> a, b = divmod(bits, 2)
> x = 2**(a+b)
> while True:
> y = (x + n//x)//2
> if y >= x:
> return x
> x = y
>
>
> The second is only exact for some values of n, and for sufficiently large 
> values
> of n, is will fail altogether:
>
>
> import math
>
> def isqrt_float(n):
> """Integer square root using floating point sqrt."""
> return int(math.sqrt(n))
>
>
>
> We know that:
>
> - for n <= 2**53, isqrt_float(n) is exact;
>
> - for n >= 2**1024, isqrt_float(n) will raise OverflowError;
>
> - between those two values, 2**53 < n < 2**1024, isqrt_float(n)
>   will sometimes be exact, and sometimes not exact;
>
> - there is some value, let's call it M, which is the smallest
>   integer where isqrt_float is not exact.
>
>
> Your mission, should you choose to accept it, is to find M.
>
> Hint: a linear search starting at 2**53 will find it -- eventually. But it 
> might
> take a long time. Personally I gave up after five minutes, but for all I know
> if I had a faster computer I'd already have the answer.
>
> (But probably not.)
>
>
> Another hint: if you run this code:
>
>
> for i in range(53, 1024):
> n = 2**i
> if isqrt_newton(n) != isqrt_float(n):
> print(n)
> break
>
>
> you can find a much better upper bound for M:
>
> 2**53 < M <= 2**105
>
> which is considerable smaller that the earlier upper bound of 2**1024. But 
> even
> so, that's still 40564819207303331840695247831040 values to be tested.
>
> On the assumption that we want a solution before the return of Halley's Comet,
> how would you go about finding M?

Here's a much smaller upper bound:

>>> n = 2 ** 53
>>> s = isqrt_newton(n)
>>> n
9007199254740992
>>> s
94906265
>>> m = (s+1)**2 - 1
>>> m
9007199326062755
>>> isqrt_newton(m) == isqrt_float(m)
False
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: PyYaml not using Yaml 1.2?

2017-08-04 Thread Skip Montanaro
> Getting in to Ansible and back into Python. Ansible uses pyyaml which says
> it parses yaml version 1.1. Is there a reason it doesn't do yaml version
> 1.2?

Nobody's done the work? Note that on the PyPI page:

https://pypi.python.org/pypi/PyYAML

the last release was almost a year ago. That said, 1.2 has been out
for awhile. There is an open ticket, nearly four years old:

https://bitbucket.org/xi/pyyaml/issues/23/support-yaml-12

Perhaps you can help move that forward.

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


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread Robin Becker

Well I tried a handomatic binary search and this number seems to differ

33347481357698898183343210233857L

whereas this does not

33347481357698898183343210233856L



On 04/08/2017 15:51, Steve D'Aprano wrote:

This is a challenge for which I don't have a complete answer, only a partial
answer.

Here are two functions for calculating the integer square root of a non-negative
int argument. The first is known to be exact but may be a bit slow:

def isqrt_newton(n):
 """Integer sqrt using Newton's Method."""
 if n == 0:
 return 0
 bits = n.bit_length()
 a, b = divmod(bits, 2)
 x = 2**(a+b)
 while True:
 y = (x + n//x)//2
 if y >= x:
 return x
 x = y


The second is only exact for some values of n, and for sufficiently large values
of n, is will fail altogether:


import math

def isqrt_float(n):
 """Integer square root using floating point sqrt."""
 return int(math.sqrt(n))



We know that:

- for n <= 2**53, isqrt_float(n) is exact;

- for n >= 2**1024, isqrt_float(n) will raise OverflowError;

- between those two values, 2**53 < n < 2**1024, isqrt_float(n)
   will sometimes be exact, and sometimes not exact;

- there is some value, let's call it M, which is the smallest
   integer where isqrt_float is not exact.


Your mission, should you choose to accept it, is to find M.

Hint: a linear search starting at 2**53 will find it -- eventually. But it might
take a long time. Personally I gave up after five minutes, but for all I know
if I had a faster computer I'd already have the answer.

(But probably not.)


Another hint: if you run this code:


for i in range(53, 1024):
 n = 2**i
 if isqrt_newton(n) != isqrt_float(n):
 print(n)
 break


you can find a much better upper bound for M:

 2**53 < M <= 2**105

which is considerable smaller that the earlier upper bound of 2**1024. But even
so, that's still 40564819207303331840695247831040 values to be tested.

On the assumption that we want a solution before the return of Halley's Comet,
how would you go about finding M?







--
Robin Becker

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


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread Ian Kelly
On Fri, Aug 4, 2017 at 10:00 AM, Ian Kelly  wrote:
> On Fri, Aug 4, 2017 at 9:47 AM, Chris Angelico  wrote:
>> On Sat, Aug 5, 2017 at 1:44 AM, Chris Angelico  wrote:
>>> That gave me this result almost instantaneously:
>>>
>>> 4503599761588224
>>>
>>> which has been rounded up instead of down. I don't know if that counts
>>> as sufficiently wrong?
>>
>> Oh, and I forgot to say: I have no actual *proof* that this is the
>> lowest number for which this will occur. It does not occur with
>> 4503599761588223 (the next lower integer), but could happen with
>> something between two squares, for all I know. However, I suspect that
>> it wouldn't.
>
> Your example demonstrates that the "2**53 < M" assumption was wrong,
> which is interesting. It may be worth checking values less than 2**26
> as well.

Less than (2**26)**2, I mean.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread Ian Kelly
On Fri, Aug 4, 2017 at 9:47 AM, Chris Angelico  wrote:
> On Sat, Aug 5, 2017 at 1:44 AM, Chris Angelico  wrote:
>> That gave me this result almost instantaneously:
>>
>> 4503599761588224
>>
>> which has been rounded up instead of down. I don't know if that counts
>> as sufficiently wrong?
>
> Oh, and I forgot to say: I have no actual *proof* that this is the
> lowest number for which this will occur. It does not occur with
> 4503599761588223 (the next lower integer), but could happen with
> something between two squares, for all I know. However, I suspect that
> it wouldn't.

Your example demonstrates that the "2**53 < M" assumption was wrong,
which is interesting. It may be worth checking values less than 2**26
as well.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: PyYaml not using Yaml 1.2?

2017-08-04 Thread leam hall
On Fri, Aug 4, 2017 at 11:52 AM, Skip Montanaro 
wrote:

> > Getting in to Ansible and back into Python. Ansible uses pyyaml which
> says
> > it parses yaml version 1.1. Is there a reason it doesn't do yaml version
> > 1.2?
>
> Nobody's done the work? Note that on the PyPI page:
>
> https://pypi.python.org/pypi/PyYAML
>
> the last release was almost a year ago. That said, 1.2 has been out
> for awhile. There is an open ticket, nearly four years old:
>
> https://bitbucket.org/xi/pyyaml/issues/23/support-yaml-12
>
> Perhaps you can help move that forward.
>
> Skip
>

Hey Skip, thanks!

Tracked down the GitHub repo (https://github.com/yaml/pyyaml) and it seems
to be gearing back up. I'll see what I can do to help.

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


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread MRAB

On 2017-08-04 15:51, Steve D'Aprano wrote:

This is a challenge for which I don't have a complete answer, only a partial
answer.

Here are two functions for calculating the integer square root of a non-negative
int argument. The first is known to be exact but may be a bit slow:

def isqrt_newton(n):
 """Integer sqrt using Newton's Method."""
 if n == 0:
 return 0
 bits = n.bit_length()
 a, b = divmod(bits, 2)
 x = 2**(a+b)
 while True:
 y = (x + n//x)//2
 if y >= x:
 return x
 x = y


The second is only exact for some values of n, and for sufficiently large values
of n, is will fail altogether:


import math

def isqrt_float(n):
 """Integer square root using floating point sqrt."""
 return int(math.sqrt(n))



We know that:

- for n <= 2**53, isqrt_float(n) is exact;

- for n >= 2**1024, isqrt_float(n) will raise OverflowError;

- between those two values, 2**53 < n < 2**1024, isqrt_float(n)
   will sometimes be exact, and sometimes not exact;

- there is some value, let's call it M, which is the smallest
   integer where isqrt_float is not exact.


Your mission, should you choose to accept it, is to find M.

Hint: a linear search starting at 2**53 will find it -- eventually. But it might
take a long time. Personally I gave up after five minutes, but for all I know
if I had a faster computer I'd already have the answer.

(But probably not.)


Another hint: if you run this code:


for i in range(53, 1024):
 n = 2**i
 if isqrt_newton(n) != isqrt_float(n):
 print(n)
 break


you can find a much better upper bound for M:

 2**53 < M <= 2**105

which is considerable smaller that the earlier upper bound of 2**1024. But even
so, that's still 40564819207303331840695247831040 values to be tested.

On the assumption that we want a solution before the return of Halley's Comet,
how would you go about finding M?

Why would isqrt_float not give the correct answer? Probably because of 
truncation (or rounding up?) of the floating point. I'd expect it to 
fail first near a square.

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


Re: SSL/TLS support in Pyro4

2017-08-04 Thread Christian Heimes
On 2017-08-04 17:11, Robin Becker wrote:
> On 04/08/2017 15:12, Irmen de Jong wrote:
>> On 04/08/2017 15:44, Robin Becker wrote:
> ..
>> You can specify a CAcert using load_verify_locations on the ssl
>> context. Is that what
>> you meant? I figured out that if you set that to the peer's
>> certificate it will then be
> 
> yes I think so. Certainly the self signed certs I tried with python3
> urllib seemed to require valid hostnames. If I just use this as server
> 
> 
> from http.server import HTTPServer, BaseHTTPRequestHandler,
> SimpleHTTPRequestHandler
> import ssl
> 
> 
> httpd = HTTPServer(('localhost', 4443), SimpleHTTPRequestHandler)
> 
> httpd.socket = ssl.wrap_socket (httpd.socket,
> keyfile="/home/rptlab/tmp/key.pem",
> certfile='/home/rptlab/tmp/cert.pem', server_side=True)
> 
> httpd.serve_forever()
> 
> and this as requester
> 
> from urllib import request
> req = request.urlopen('https://localhost:4443',
>cafile='/home/rptlab/tmp/cert.pem')
> print(req.read())
> 
> 
> then provided the self signed cert has the name localhost requests can
> be made OK.
> 
> I'm guessing this would also work OK if the cert had multiple names
> embedded in it which would allow a small cluster to be used.
> 
> I don't know which part of the socket does the host name checking, but
> perhaps that can be turned off somewhere.

This approach works but requires a carefully crafted certificate. The
certificate must be a valid CA and EE certificate at the same time.
Either you must not include any X509v3 extensions or correctly pick the
right combination of BasicConstraint, Key Usage and Extended Key Usage.

For my tests I use my own project CA. For example
https://github.com/latchset/custodia/tree/master/tests/ca/ contains a
script to generate a CA and two EE certs. The server cert is valid for
localhost and 127.0.0.1. You can easily extend the configuration to
include one or multiple intermediate CAs.

Christian

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


Basic sample code, pandas and cython

2017-08-04 Thread dnmalkin1
Does anyone have a really basic cython example where you create a dataframe 
(pandas) of random numbers 1 to 5 for 100 rows and cythonize the code? Is it 
already cythonized by the Pandas pd module?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread Steve D'Aprano
On Sat, 5 Aug 2017 01:44 am, Chris Angelico wrote:

> On Sat, Aug 5, 2017 at 12:51 AM, Steve D'Aprano
>  wrote:
>> def isqrt_float(n):
>> """Integer square root using floating point sqrt."""
>> return int(math.sqrt(n))
[...]

> Hmm. Thinking aloud a bit here. We know that isqrt_float(n) is not
> technically *exact* for any n that is not a square. 

Actually we do. You seem to be thinking about the "true" square root, not the
integer square root.

I'm sorry, I should have posted a link to the definition of integer square root,
that's my mistake. But I thought that everyone would either already know, or
know how to use Wikipedia :-)

https://en.wikipedia.org/wiki/Integer_square_root

Mea culpa.

The integer square root is, *by definition*, the floor (round down to nearest
integer, which for positive values is the same as just truncating) of the true
square root. So the first few integer square roots are:

n=1 isqrt=1
n=2 isqrt=1
n=3 isqrt=1
n=4 isqrt=2

and isqrt_float is exact for those n. It's definitely[1] exact for all n up to
2**53, and many more beyond that. By exact I mean that it returns the same
(correct) root as isqrt_newton, rather than the root plus (or minus) a bit.

An example of where it is *not* exact:

py> isqrt_float(2**119)
815238614083298944
py> isqrt_newton(2**119)
81523861408329

That's a difference of just 56 or one part in 14557 trillion, which is
*approximately* correct :-)

Up to 2**53, there is no rounding error when converting from int to float, which
is why I am confident that int(math.sqrt(n)) will always give the exact value
of integer square root up to at least 2**53. We have math.sqrt(n) return the
correctly rounded true square root, and int() truncates it, which is the
definition of integer square root.

For example:

py> isqrt_float(2**53)
94906265
py> isqrt_newton(2**53)
94906265

They're the same, and I stated earlier that you can take isqrt_newton as always
exact. (Perhaps I should have said "correct" rather than exact?)

You may be concerned that 94906265**2 != 2**53. That's not a problem. All that
matters is that 94906265 is the largest integer which, when squared, is less
than or equal to the original 2**53. And that is the case:

py> 94906265**2 <= 2**53
True
py> 94906266**2 > 2**53
True







[1] I have only proved this is correct, not tested it.



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread Steve D'Aprano
On Sat, 5 Aug 2017 01:47 am, Ian Kelly wrote:

> Here's a much smaller upper bound:
> 
 n = 2 ** 53
 s = isqrt_newton(n)
 n
> 9007199254740992
 s
> 94906265
 m = (s+1)**2 - 1
 m
> 9007199326062755
 isqrt_newton(m) == isqrt_float(m)
> False

Oooh, interesting. How did you get that? By luck, or do you have some reason for
picking (s+1)**2 - 1?


I have managed to find an upper bound *almost* as small:

9008783381281320

by running a script for the last 15 or 16 hours, which randomly tests ints
between lower and upper bound. If it finds a miss, it reduces the upper bound.

That started off very promised, and was extremely fast at first, but as the
range of values to check gets tighter, it also becomes harder to find any
misses.



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread Chris Angelico
On Sat, Aug 5, 2017 at 3:37 AM, Steve D'Aprano
 wrote:
> On Sat, 5 Aug 2017 01:44 am, Chris Angelico wrote:
>
>> On Sat, Aug 5, 2017 at 12:51 AM, Steve D'Aprano
>>  wrote:
>>> def isqrt_float(n):
>>> """Integer square root using floating point sqrt."""
>>> return int(math.sqrt(n))
> [...]
>
>> Hmm. Thinking aloud a bit here. We know that isqrt_float(n) is not
>> technically *exact* for any n that is not a square.
>
> Actually we do. You seem to be thinking about the "true" square root, not the
> integer square root.
>
> I'm sorry, I should have posted a link to the definition of integer square 
> root,
> that's my mistake. But I thought that everyone would either already know, or
> know how to use Wikipedia :-)
>
> https://en.wikipedia.org/wiki/Integer_square_root
>
> Mea culpa.

So my assumption turned out correct, albeit for slightly incorrect
reasons. In any case, I based things on a discrepancy between
isqrt_float and isqrt_newton.

> and isqrt_float is exact for those n. It's definitely[1] exact for all n up to
> 2**53, and many more beyond that. By exact I mean that it returns the same
> (correct) root as isqrt_newton, rather than the root plus (or minus) a bit.
>
> An example of where it is *not* exact:
>
> py> isqrt_float(2**119)
> 815238614083298944
> py> isqrt_newton(2**119)
> 81523861408329
>
> [1] I have only proved this is correct, not tested it.

And that's what I looked at.

>>> isqrt_float(4503599761588224)
67108865
>>> isqrt_newton(4503599761588224)
67108864

This number is (2**52 + 2**27), and is one less than a square. The
floating-point function is rounding it up, and as such is not
returning the integer square root, which should be rounded down. Is my
analysis correct?

This value is lower than 2**53.

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


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread Chris Angelico
On Sat, Aug 5, 2017 at 3:44 AM, Steve D'Aprano
 wrote:
> On Sat, 5 Aug 2017 01:47 am, Ian Kelly wrote:
>
>> Here's a much smaller upper bound:
>>
> n = 2 ** 53
> s = isqrt_newton(n)
> n
>> 9007199254740992
> s
>> 94906265
> m = (s+1)**2 - 1
> m
>> 9007199326062755
> isqrt_newton(m) == isqrt_float(m)
>> False
>
> Oooh, interesting. How did you get that? By luck, or do you have some reason 
> for
> picking (s+1)**2 - 1?
>
>
> I have managed to find an upper bound *almost* as small:
>
> 9008783381281320
>
> by running a script for the last 15 or 16 hours, which randomly tests ints
> between lower and upper bound. If it finds a miss, it reduces the upper bound.
>
> That started off very promised, and was extremely fast at first, but as the
> range of values to check gets tighter, it also becomes harder to find any
> misses.

My logic was that floating point rounding is easiest to notice when
you're working with a number that's very close to something, and since
we're working with square roots, "something" should be a perfect
square. The integer square root of n**2 is n, the ISR of n**2+1 is
also n, and the ISR of n**2-1 should be n-1. I actually wanted to
start at 2**53, but being an odd power, that doesn't have an integer
square root, so I started at 2**52, which has an ISR of 2**26.

The phenomenon MAY depend on the current FPU rounding rules.

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


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread Steve D'Aprano
On Sat, 5 Aug 2017 02:31 am, MRAB wrote:

> Why would isqrt_float not give the correct answer? Probably because of
> truncation (or rounding up?) of the floating point. I'd expect it to
> fail first near a square.

Assuming your math.sqrt() is an IEEE-754 correctly-rounded square root, and that
int() correctly truncates, the only source of errors is that not all integers
are representable as floats.

No integer larger than or equal to 2**1024 can be represented as a float.

No *odd* integer larger than 2**53 can be represented as a float.

Between 2**53 and 2**54, float can only represent multiples of 2.

Between 2**54 and 2**55, float can only represent multiples of 4.

Between 2**55 and 2**56, float can only represent multiples of 8.

And so on. 



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread Ian Kelly
On Fri, Aug 4, 2017 at 11:44 AM, Steve D'Aprano
 wrote:
> On Sat, 5 Aug 2017 01:47 am, Ian Kelly wrote:
>
>> Here's a much smaller upper bound:
>>
> n = 2 ** 53
> s = isqrt_newton(n)
> n
>> 9007199254740992
> s
>> 94906265
> m = (s+1)**2 - 1
> m
>> 9007199326062755
> isqrt_newton(m) == isqrt_float(m)
>> False
>
> Oooh, interesting. How did you get that? By luck, or do you have some reason 
> for
> picking (s+1)**2 - 1?

Partly luck. I reasoned that a mismatch would be due to incorrect
rounding, which would most likely happen just before a perfect square.
So I tried the value one less than the first perfect square after 2 **
53 and it happened to work. I also tried the value two less, which did
not.

In any case, Chris's finding beats mine by a factor of two.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread Ian Kelly
On Fri, Aug 4, 2017 at 11:50 AM, Chris Angelico  wrote:
> My logic was that floating point rounding is easiest to notice when
> you're working with a number that's very close to something, and since
> we're working with square roots, "something" should be a perfect
> square. The integer square root of n**2 is n, the ISR of n**2+1 is
> also n, and the ISR of n**2-1 should be n-1. I actually wanted to
> start at 2**53, but being an odd power, that doesn't have an integer
> square root, so I started at 2**52, which has an ISR of 2**26.

A slight irony here is that it actually would have taken your script a
*very* long time to get to 2**53 having started at 2**52, even only
iterating over the perfect squares.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread Ian Kelly
On Fri, Aug 4, 2017 at 11:59 AM, Ian Kelly  wrote:
> On Fri, Aug 4, 2017 at 11:50 AM, Chris Angelico  wrote:
>> My logic was that floating point rounding is easiest to notice when
>> you're working with a number that's very close to something, and since
>> we're working with square roots, "something" should be a perfect
>> square. The integer square root of n**2 is n, the ISR of n**2+1 is
>> also n, and the ISR of n**2-1 should be n-1. I actually wanted to
>> start at 2**53, but being an odd power, that doesn't have an integer
>> square root, so I started at 2**52, which has an ISR of 2**26.
>
> A slight irony here is that it actually would have taken your script a
> *very* long time to get to 2**53 having started at 2**52, even only
> iterating over the perfect squares.

Never mind, I just tried it and it's a few seconds. I guess there are
not as many perfect squares in that range as I thought.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread Steve D'Aprano
On Sat, 5 Aug 2017 01:44 am, Chris Angelico wrote:

> Hmm. Thinking aloud a bit here. We know that isqrt_float(n) is not
> technically *exact* for any n that is not a square. 

I got bogged down in answering that misapprehension, but it may not actually
have mattered. You then said:


> So what I'd do is 
> assume that for (n*n+1) to (n+1)*(n+1)-1, it's going to return the
> same value. I would then probe every perfect square, and the values
> one above and one below it. Now, that's still probably too many to
> check, but it's going to be a lot faster; for starters, we don't
> actually need to use isqrt_newton to compare against it.

That's interesting. I think I need to think more about that when it's not stupid
o'clock in the morning, because although I'm not sure your logic is sound I
can't deny that you've hit on a *huge* improvement in the upper bound. You
have:

> That gave me this result almost instantaneously:
> 
> 4503599761588224
> 
> which has been rounded up instead of down. I don't know if that counts
> as sufficiently wrong?

It certainly does!

 isqrt_float(4503599761588224)
> 67108865
 isqrt_newton(4503599761588224)
> 67108864


After running a random search for something like 18 hours, I managed to get the
bound down to 9008783381281320. Ian managed to pick 9007199326062755 (perhaps
by luck?). But you beat us both, handsomely.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread Chris Angelico
On Sat, Aug 5, 2017 at 3:51 AM, Steve D'Aprano
 wrote:
> On Sat, 5 Aug 2017 02:31 am, MRAB wrote:
>
>> Why would isqrt_float not give the correct answer? Probably because of
>> truncation (or rounding up?) of the floating point. I'd expect it to
>> fail first near a square.
>
> Assuming your math.sqrt() is an IEEE-754 correctly-rounded square root

To clarify: What you're assuming here is that the floating-point value
that math.sqrt returns is exactly the value that you get by rounding
the true square root (even if transcendental) to the available
precision. I *believe* that that's guaranteed for a floating-point
input value, but is it for integer input? Is there any way that (say)
2**53+1 could sqrt to a different value depending on whether you
convert the input to float first?

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


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread Chris Angelico
On Sat, Aug 5, 2017 at 4:03 AM, Ian Kelly  wrote:
> On Fri, Aug 4, 2017 at 11:59 AM, Ian Kelly  wrote:
>> On Fri, Aug 4, 2017 at 11:50 AM, Chris Angelico  wrote:
>>> My logic was that floating point rounding is easiest to notice when
>>> you're working with a number that's very close to something, and since
>>> we're working with square roots, "something" should be a perfect
>>> square. The integer square root of n**2 is n, the ISR of n**2+1 is
>>> also n, and the ISR of n**2-1 should be n-1. I actually wanted to
>>> start at 2**53, but being an odd power, that doesn't have an integer
>>> square root, so I started at 2**52, which has an ISR of 2**26.
>>
>> A slight irony here is that it actually would have taken your script a
>> *very* long time to get to 2**53 having started at 2**52, even only
>> iterating over the perfect squares.
>
> Never mind, I just tried it and it's a few seconds. I guess there are
> not as many perfect squares in that range as I thought.

There are about thirty million of them. Python can calculate square
roots in a handful of nanoseconds on my system, so "a few seconds"
sounds about right.

>>> math.sqrt(2**53) - math.sqrt(2**52)
27797401.62425156
>>> 2**26 * 0.41421356
27797401.46499584

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


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread Ian Kelly
On Fri, Aug 4, 2017 at 10:03 AM, Ian Kelly  wrote:
> On Fri, Aug 4, 2017 at 10:00 AM, Ian Kelly  wrote:
>> On Fri, Aug 4, 2017 at 9:47 AM, Chris Angelico  wrote:
>>> On Sat, Aug 5, 2017 at 1:44 AM, Chris Angelico  wrote:
 That gave me this result almost instantaneously:

 4503599761588224

 which has been rounded up instead of down. I don't know if that counts
 as sufficiently wrong?
>>>
>>> Oh, and I forgot to say: I have no actual *proof* that this is the
>>> lowest number for which this will occur. It does not occur with
>>> 4503599761588223 (the next lower integer), but could happen with
>>> something between two squares, for all I know. However, I suspect that
>>> it wouldn't.
>>
>> Your example demonstrates that the "2**53 < M" assumption was wrong,
>> which is interesting. It may be worth checking values less than 2**26
>> as well.
>
> Less than (2**26)**2, I mean.

I modified Chris's code to check every square below 2**52.
4503599761588224 is the smallest example that is at most one off from
a perfect square.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread Steve D'Aprano
On Sat, 5 Aug 2017 02:00 am, Ian Kelly wrote:

> On Fri, Aug 4, 2017 at 9:47 AM, Chris Angelico  wrote:
>> On Sat, Aug 5, 2017 at 1:44 AM, Chris Angelico  wrote:
>>> That gave me this result almost instantaneously:
>>>
>>> 4503599761588224
>>>
>>> which has been rounded up instead of down. I don't know if that counts
>>> as sufficiently wrong?
[...]

> Your example demonstrates that the "2**53 < M" assumption was wrong,
> which is interesting.

It sure is.

You're right, of course, I hadn't noticed that Chris' M was approximately half
2**53.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread Terry Reedy

On 8/4/2017 11:44 AM, Chris Angelico wrote:

On Sat, Aug 5, 2017 at 12:51 AM, Steve D'Aprano
 wrote:

def isqrt_float(n):
 """Integer square root using floating point sqrt."""
 return int(math.sqrt(n))


The operations in the integer version are well-defined and so the answer 
should be the same on any Python 3 and indeed in any language that does 
proper integer arithmetic (but most do not).


In CPython, Math.sqrt wraps the C compiler's double sqrt function.  To 
make it deterministic, we must specify the float representation, 
rounding rule, and accuracy of the sqrt function.  Let's assume that the 
float answer is IEEE binary64, with 53 binary digits, with rounding rule 
'round to nearest, ties to even' and that the algorithm is as exact as 
possible, which is to say, the exact mathematical answer rounded as 
specified.


With 53 binary digits, all counts from 0 to 2**53 - 1 are exactly 
represented with a exponent of 0,  2**53 = 2**52 * 2, so it is exactly 
represented with an exponent of 1. Many other higher counts can be 
exactly represented with various exponents, but 2**53 + 1 requires 54 
digits.  So it is the first count that does not have an exact binary64 
representation.


Under these assumptions, how can isqrt_float(n) fail?

Suppose 0 <= m <= 2**26.5, so that m*m <= 2**53.  Under the assumptions 
that m.sqrt is as exact as possible, sqrt(m*m) == float(m) and 
int(float(m)) == m.  The true square root of m*m -1 will be m - e (e < 
1) and the integer square root is m-1.  However, if e is less than (and 
I believe equal) to 1/2 of the interval between n and the next lowest 
float, m - e will be rounded up to float(m) and ifloat(m*m-1) will 
incorrectly return m instead of m-1.


As m increases, e decreases while the interval between floats increases, 
so we should expect an eventual error


In this range of m, sqrt(m*m +1) will always be greater than m, so 
rounding down cannot lead to an error.  For larger m, where float(m*m) 
is inaccurate enough, it might.


As m increases, e decreases while the interval between floats increases, 
so we should expect an eventual error.  Whether it occurs for m <= 2* or 
not might be determined by careful analysis, but Chris Angelico already 
gave us the brute force answer.



We know that:

- for n <= 2**53, isqrt_float(n) is exact;


but it is not ;-).  In this range, the only possibility is 
math.sqrt(m*m-1) being m instead of m - delta and that is the case for
Chris' answer 4503599761588224 = 67108865**2 - 1. 
int(math.float(4503599761588224) is 67108865 instead of 67108864.



- for n >= 2**1024, isqrt_float(n) will raise OverflowError >> - between those two 
values, 2**53 < n < 2**1024, isqrt_float(n)
   will sometimes be exact, and sometimes not exact;
- there is some value, let's call it M, which is the smallest
   integer where isqrt_float is not exact.
Your mission, should you choose to accept it, is to find M.




Hmm. Thinking aloud a bit here. We know that isqrt_float(n) is not
technically *exact* for any n that is not a square. So what I'd do is
assume that for (n*n+1) to (n+1)*(n+1)-1, it's going to return the
same value. I would then probe every perfect square, and the values
one above and one below it. Now, that's still probably too many to
check, but it's going to be a lot faster; for starters, we don't
actually need to use isqrt_newton to compare against it.

for root in range(2**26, 2**53):
 square = root * root
 if isqrt_float(square - 1) != root - 1:
 print(square - 1)
 break
 if isqrt_float(square) != root:
 print(square)
 break
 if isqrt_float(square + 1) != root:
 print(square + 1)
 break

That gave me this result almost instantaneously:

4503599761588224


Your limit was barely low enough ;-).

>>> math.log2(math.sqrt(4503599761588224))
26.00021497833

If you had started with, say int(2**26.5)-1, you would have missed this.
I reran with range(2, 2**26) and there is nothing lower.

>>> math.log2(4503599761588224)
52.0004299566

>>> import decimal as d
>>> I = d.Decimal(4503599761588224)
>>> I.sqrt()
Decimal('67108864.254941951410')
>>> float(I.sqrt())
67108865.0


which has been rounded up instead of down. I don't know if that counts
as sufficiently wrong?


The isqrt_float answer is definitely wrong, and so is the claim for n < 
2**53.



isqrt_float(4503599761588224)

67108865

isqrt_newton(4503599761588224)

67108864

67108865 ** 2

4503599761588225


--
Terry Jan Reedy

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


Re: PyYaml not using Yaml 1.2?

2017-08-04 Thread Lele Gaifax
leam hall  writes:

> Tracked down the GitHub repo (https://github.com/yaml/pyyaml) and it seems
> to be gearing back up. I'll see what I can do to help.

See also https://bitbucket.org/ruamel/yaml, a fork of PyYAML, it seems more
actively maintained and already supports format 1.2.

ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
l...@metapensiero.it  | -- Fortunato Depero, 1929.

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


Code for addition

2017-08-04 Thread Ode Idoko via Python-list
Can anyone help with the python code that can add 101, 102, 103...2033 please? 
As I said before, I'm new to python and need assistance in this regard. 
Thanks for always assisting. 

Sent from my iPhone
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Code for addition

2017-08-04 Thread Bob Gailer
On Aug 4, 2017 5:27 PM, "Ode Idoko via Python-list" 
wrote:
>
> Can anyone help with the python code that can add 101, 102, 103...2033
please?

We are here to help but we don't have crystal balls to interpret your
request.

The best thing you can do is show us what attempts you have made to write
code for this problem and the difficulties you have encountered.

We always assume when given a problem like this that it's a homework
assignment. So it's a good idea to state that upfront.

Have you had any success in writing and running any python programs?
Assuming that you are in a class, what aspects of python have you already
learned? Can you apply any of that to this problem? The more you give us
the easier it is for us to help.

> As I said before, I'm new to python and need assistance in this regard.
> Thanks for always assisting.
>
> Sent from my iPhone
> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: PyYaml not using Yaml 1.2?

2017-08-04 Thread Marko Rauhamaa
Lele Gaifax :

> leam hall  writes:
>
>> Tracked down the GitHub repo (https://github.com/yaml/pyyaml) and it seems
>> to be gearing back up. I'll see what I can do to help.
>
> See also https://bitbucket.org/ruamel/yaml, a fork of PyYAML, it seems more
> actively maintained and already supports format 1.2.

BTW, happened to land on this blog posting that mentions a security
warning regarding PyYAML:

   A suggested fix is to always use yaml.safe_load for handling YAML
   serialization you can't trust. Still, the current PyYAML default
   feels somewhat provoking considering other serialization libraries
   tend to use dump/load function names for similar purposes, but in a
   safe manner.

   https://access.redhat.com/blogs/766093/posts/2592591>


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


Question About When Objects Are Destroyed

2017-08-04 Thread Jon Forrest

Consider the following Python shell session (Python 3.6.2, Win64):

>>> def givemetwo():
... x = 'two'
... print(id(x))
...
>>> givemetwo()
1578505988392

So far fine. My understanding of object existence made me
think that the object referred to by x would be deleted when
the givemetwo() function returned, like a local variable in C.

However, this isn't true, as shown by the following in the same
session:

>>> import ctypes
>>> print (ctypes.cast(1578505988392, ctypes.py_object).value)
two

This shows that the object still exists, which was a surprise.

Will this object ever be deleted? I'm learning about function
decorators which, as my early studies tell me, depend on calling
a function defined inside another function. This suggests that
objects defined inside functions are never deleted, otherwise
function decorators would never work. (I'm not 100% sure
my understanding of function decorators is correct since I'm
still learning about them).

What's the right way to think about this?

Cordially,
Jon Forrest

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


Question About When Objects Are Destroyed

2017-08-04 Thread gst
'two' is a so called constant or literal value .. (of that function).

Why not attach it, as a const value/object, to the function itself ? So that a 
new string object has not to be created each time the function is called. 
Because anyway strings are immutable. So what would be the point to recreate 
such object every time the function is called ?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question About When Objects Are Destroyed

2017-08-04 Thread Jon Forrest

On 8/4/2017 4:34 PM, gst wrote:

'two' is a so called constant or literal value .. (of that
function).

Why not attach it, as a const value/object, to the function itself ?
So that a new string object has not to be created each time the
function is called. Because anyway strings are immutable. So what
would be the point to recreate such object every time the function is
called ?


This was just an example program, not meant to do anything
meaningful. I would think that the same object behavior would
occur if I dynamically created an object in that function.

Jon




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


Linux/Windows GUI programming: tk or wx?

2017-08-04 Thread Ulli Horlacher
I have to transfer a python 2.7 CLI programm into one with a (simple) GUI.
The program must run on Linux and Windows and must be compilable with
pyinstall, because I have to ship a standalone windows.exe
Any kind of installer is not acceptable.

Reading https://github.com/pyinstaller/pyinstaller/wiki/Supported-Packages
supported GUI packages are PyGTK, PyQt4, PyQt5, wxPython
I have tested tkinter by myself and it works, too.
I do not like GTK and Qt, because they are too complex.

I want to do VERY simple things and I prefer a simple GUI toolkit :-)

Is there a recommendation for using tk or wx?


-- 
Ullrich Horlacher  Server und Virtualisierung
Rechenzentrum TIK 
Universitaet Stuttgart E-Mail: horlac...@tik.uni-stuttgart.de
Allmandring 30aTel:++49-711-68565868
70569 Stuttgart (Germany)  WWW:http://www.tik.uni-stuttgart.de/
-- 
https://mail.python.org/mailman/listinfo/python-list


Question About When Objects Are Destroyed (continued)

2017-08-04 Thread Jon Forrest

Perhaps the reason the variable isn't destroyed is
shown by the following (again, in the same session):

>>> import sys
>>> sys.getrefcount(1578505988392)
3

So, maybe it's not destroyed because there are still
references to it. But, what are these references?
Will the reference count ever go to zero?

Jon

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


Re: Question About When Objects Are Destroyed

2017-08-04 Thread Chris Angelico
On Sat, Aug 5, 2017 at 9:42 AM, Jon Forrest  wrote:
> On 8/4/2017 4:34 PM, gst wrote:
>>
>> 'two' is a so called constant or literal value .. (of that
>> function).
>>
>> Why not attach it, as a const value/object, to the function itself ?
>> So that a new string object has not to be created each time the
>> function is called. Because anyway strings are immutable. So what
>> would be the point to recreate such object every time the function is
>> called ?
>
>
> This was just an example program, not meant to do anything
> meaningful. I would think that the same object behavior would
> occur if I dynamically created an object in that function.

Python doesn't have pointers, so what you have is an arbitrary number.
Even if the object had been freed from memory, you could quite
probably do the same shenanigans to get a value out of it; the data
would still be there.

Basically, don't think about object lifetimes. Objects will be flushed
from memory once they're not needed any more, and no sooner; so you
can safely return anything from any function.

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


Re: Question About When Objects Are Destroyed (continued)

2017-08-04 Thread Chris Angelico
On Sat, Aug 5, 2017 at 9:47 AM, Jon Forrest  wrote:
> Perhaps the reason the variable isn't destroyed is
> shown by the following (again, in the same session):
>
 import sys
 sys.getrefcount(1578505988392)
> 3
>
> So, maybe it's not destroyed because there are still
> references to it. But, what are these references?
> Will the reference count ever go to zero?

That's the reference count for the integer object. Nothing to do with
the original. Again, don't stress about exactly when objects get
disposed of; it doesn't matter.

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


Re: Linux/Windows GUI programming: tk or wx?

2017-08-04 Thread Michael Torrie
On 08/04/2017 05:45 PM, Ulli Horlacher wrote:
> I have to transfer a python 2.7 CLI programm into one with a (simple) GUI.
> The program must run on Linux and Windows and must be compilable with
> pyinstall, because I have to ship a standalone windows.exe
> Any kind of installer is not acceptable.
> 
> Reading https://github.com/pyinstaller/pyinstaller/wiki/Supported-Packages
> supported GUI packages are PyGTK, PyQt4, PyQt5, wxPython
> I have tested tkinter by myself and it works, too.
> I do not like GTK and Qt, because they are too complex.
> 
> I want to do VERY simple things and I prefer a simple GUI toolkit :-)
> 
> Is there a recommendation for using tk or wx?

Well tk is already an optional part of the Python standard library,
whereas wx is an external package.  So for your simple requirements, Tk
may be the way to go.  I'm guessing the tk would result in the smallest
executable as well, though I could be very wrong.

As for Qt, it's a large library, but it's not as complex as you think.
In fact for simple GUIs it's no more complicated than wx or tk.  So
don't discount PyQt as being unworkable, though you do need to be aware
of PyQt's license, which might be incompatible with your needs (it's GPL
only unless you pay for it).  PySide is largely compatible with PyQt
code and is LGPL instead of GPL, so that could be an option as well.


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


Re: Code for addition

2017-08-04 Thread Steve D'Aprano
On Sat, 5 Aug 2017 12:43 pm, Ode Idoko wrote:

> Can anyone help with the python code that can add 101, 102, 103...2033 please?


Sounds like homework. Here are some hints for you to play around with and see if
you can get the right answer:


sum([101, 102, 103])

range(101, 104)

sum(range(1, 4))


What does sum() do?

What does range() do?

Can you put them together to get the answer you need?



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: Question About When Objects Are Destroyed

2017-08-04 Thread Steve D'Aprano
On Sat, 5 Aug 2017 09:11 am, Jon Forrest wrote:

> Consider the following Python shell session (Python 3.6.2, Win64):
> 
>  >>> def givemetwo():
> ... x = 'two'
> ... print(id(x))
> ...
>  >>> givemetwo()
> 1578505988392
> 
> So far fine. My understanding of object existence made me
> think that the object referred to by x would be deleted when
> the givemetwo() function returned, like a local variable in C.

Not necessarily.

Objects are destroyed when they are no longer referenced by any other object.
That may happen when the function exits, but it may not. For example, if you
return x, and the caller assigns it to a name, then the object will still be
referenced.

However, when you exit the function, what is guaranteed is that all local
variables will go out of scope and clear *their* references to whatever objects
they are bound to. Not necessarily *all* references, but just the ones from
local variables.

Consider the object "spam and eggs", a string. If I say:

s = "spam and eggs"  # first reference

def func():
t = s  # t is a local variable, so now two refs
u = t  # third ref
return None

func()

While func() is executing, there are three references to the object: s, a
global, and t and u, locals. When the function exits, the *names* (variables) t
and u go out of scope and those references cease to exist, but the s reference
still exists and so the object (string "spam and eggs") still exists.

If you then re-bind the name s to something else:

s = "foo bar"

or delete the name:

del s

that will remove the last reference to the object and it can be garbage
collected.


> However, this isn't true, as shown by the following in the same
> session:
> 
>  >>> import ctypes
>  >>> print (ctypes.cast(1578505988392, ctypes.py_object).value)
> two
> 
> This shows that the object still exists, which was a surprise.

You may be right about the object still existing, but for the wrong reasons.

The Python interpreter is free to cache objects that it thinks have a good
chance of being re-created. This is obviously implementation dependent: it will
depend on the specific interpreter (CPython, Jython, IronPython, PyPy,
Stackless, Nuika), the specific version, and potentially any other factor the
interpreter wishes to take into account, up to and including the phase of the
moon.

In this case, CPython caches short strings that look like identifiers. It does
this because variables are implemented as string keys in dicts, so when you
create a variable

two = 2

the interpreter creates a string object 'two' to use as a key in the globals()
dict. Since object creation is relatively costly, the interpreter caches that
string and keeps it around.

So your test has accidentally hit an implementation-dependent feature of
CPython. If you had used a string that didn't look like an identifier, say

"two not three, okay?"

you may have seen different results.

Or maybe not. This is all implementation dependent.

By using ctypes, you are poking around in the internals of the Python
interpreter, you aren't looking at what *Python the language* guarantees, but
merely whatever this specific version of CPython happens to do, today.

For example, you are already on shaky ground by using the ID of an object, which
is an opaque integer, as if it were a memory address. Object IDs aren't memory
addresses, they are opaque integers.

It just happens that for speed, the CPython interpreter uses the address of the
object as its ID. But it can only do that because the CPython garbage collector
is very simple, and it never relocates objects from place to place. But Jython
and IronPython have more sophisticated garbage collectors which do, so they use
a different scheme for generating IDs which are consecutive integers.

So... you've effectively grabbed an arbitrary address in memory, which may have
previously contained a certain string object. You use ctypes to interpret that
chunk of memory as an object. Since CPython doesn't move memory around, you
might be okay:

- either that address actually does point to a live object, and you're safe;

- or it points to what *was* a live object, but the memory hasn't been used,
  and so all the fields are still correctly allocated, and you're safe;

but you might not be. What if some other object has re-used that piece of
memory? You might now be jumping halfway into some other object, and trying to
interpret that as the start of an object.

You can segfault CPython with ctypes.


> Will this object ever be deleted?

The specific object 'two'? Maybe, maybe not. It might be cached for the lifetime
of this interpreter session. Or there may be circumstances where cached objects
age-out and are deleted. It depends on the implementation of the cache. That
isn't a question about Python the language.


> I'm learning about function 
> decorators which, as my early studies tell me, depend on calling
> a function defined inside another function. This suggests that
> objects defined

Re: Question About When Objects Are Destroyed

2017-08-04 Thread Terry Reedy

On 8/4/2017 7:11 PM, Jon Forrest wrote:

Consider the following Python shell session (Python 3.6.2, Win64):

 >>> def givemetwo():
... x = 'two'
... print(id(x))
...
 >>> givemetwo()
1578505988392

So far fine. My understanding of object existence made me
think that the object referred to by x would be deleted when
the givemetwo() function returned, like a local variable in C.


Python does not specifically delete objects.  It deletes references to 
objects, such as an association between a name and an object, or between 
a key and an object, or between a sequence index and an object.  When a 
function returns, associations between local names and object are 
deleted. Python on a machine does not necessarily spend time 'cleaning' 
memory by overwriting it with 0s.



However, this isn't true, as shown by the following in the same
session:

 >>> import ctypes


Most everything you see in the manuals (and on this list) has a little * 
after it.


* unless one imports and uses ctypes (or 3rd party modules).

For instance, 'Python code, even if buggy, should not crash the 
interpreter'* and 'If you find such code, it is a bug'*, 'Please open an 
issue on the tracker'*.  If you use ctypes, please don't.



 >>> print (ctypes.cast(1578505988392, ctypes.py_object).value)
two

This shows that the object still exists, which was a surprise.
Will this object ever be deleted? 


What do you mean deleted? At this point, you are not looking at a normal 
Python object, as such, but rather the contents of a segment of machine 
memory at a particular address, extracted into a ctypes.py_object 
object.  I don't think you want that chunk of memory destroyed.


The only public attribute of the ctypes.py_object object is .value.  It 
has a few undocumented private attributes, such as ._b_needsfree_ which, 
when I tried it, is 1.


--
Terry Jan Reedy

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


pip requirements file

2017-08-04 Thread Ethan Furman

  pip freeze

will output a list of current packages and their requirements.  I have one package that falsely [1] lists another 
package as a requirement, which was blocking installation as the false requirement wasn't available.


Is there a way to modify that output (which would be piped to, for example, 
requirements.txt) to have

  pip install -r requirements.txt

so pip ignores that one (and only that one) dependency?

--
~Ethan~


[1] For the curious, the package is ZSA, and it list PyXML even though one of its modules says it was created 
specifically so PyXML is no longer needed -- and none of the modules import PyXML anywhere.

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


Re: pip requirements file

2017-08-04 Thread Chris Angelico
On Sat, Aug 5, 2017 at 12:42 PM, Ethan Furman  wrote:
>   pip freeze
>
> will output a list of current packages and their requirements.  I have one
> package that falsely [1] lists another package as a requirement, which was
> blocking installation as the false requirement wasn't available.
>
> Is there a way to modify that output (which would be piped to, for example,
> requirements.txt) to have
>
>   pip install -r requirements.txt
>
> so pip ignores that one (and only that one) dependency?

I'd just edit the file afterwards and delete the line. But if the
package claims to need PyXML, it'll still be installed.

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


Re: pip requirements file

2017-08-04 Thread Ethan Furman

On 08/04/2017 07:46 PM, Chris Angelico wrote:

On Sat, Aug 5, 2017 at 12:42 PM, Ethan Furman wrote:



   pip freeze

will output a list of current packages and their requirements.  I have one
package that falsely [1] lists another package as a requirement, which was
blocking installation as the false requirement wasn't available.

Is there a way to modify that output (which would be piped to, for example,
requirements.txt) to have

   pip install -r requirements.txt

so pip ignores that one (and only that one) dependency?


I'd just edit the file afterwards and delete the line. But if the
package claims to need PyXML, it'll still be installed.


Exactly my point.  Is there any way, requirements.txt or otherwise, to tell pip to ignore what a certain package is 
claiming it needs?


I am aware of --no-dependencies, but that (I think) is an all-or-nothing approach, whilst [1] I desire an all-except-one 
approach.


--
~Ethan~


[1] I blame words like 'whilst' on my new Paperback game by Tim Fowler.  The Smarter-AI uses words from Middle-English 
(!) and spellings not seen for at least 300 years!  But hey, my vocabulary is (uselessly) expanding!

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


Re: [OT] Re: how to guess the number of cluster when do not know?

2017-08-04 Thread dieter
Ho Yeung Lee  writes:
> actually i am using python's kmeans library. it is relevant in python

I agree that it was not bad to ask the question here.

However, the provided answer (apart from the "off topic" -- i.e.
the reference to 
"https://en.wikipedia.org/wiki/Determining_the_number_of_clusters_in_a_data_set";)
 was excellent.
It tells you that the cluster number is a general parameter for
the "kmeans" algorithm (independent of any implementation) and how you
can get some approximations.

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


Re: how to grep

2017-08-04 Thread dieter
Iranna Mathapati  writes:

> How to grep values from below out put string.
>
> pattern should include  "Fabric Module".

One possible way would be to use a regular expression --
see the documentation for the "re" module.

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


Re: Code for addition

2017-08-04 Thread Rustom Mody
On Saturday, August 5, 2017 at 6:55:09 AM UTC+5:30, Steve D'Aprano wrote:
> On Sat, 5 Aug 2017 12:43 pm, Ode Idoko wrote:
> 
> > Can anyone help with the python code that can add 101, 102, 103...2033 
> > please?
> 
> 
> Sounds like homework. Here are some hints for you to play around with and see 
> if
> you can get the right answer:
> 
> 
> sum([101, 102, 103])
> 
> range(101, 104)
> 
> sum(range(1, 4))
> 
> 
> What does sum() do?
> 
> What does range() do?
> 
> Can you put them together to get the answer you need?
> 

Or study math-history… Gauss’ early years
https://brilliant.org/wiki/gauss-the-prince-of-mathematics/

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


Re: Linux/Windows GUI programming: tk or wx?

2017-08-04 Thread Christian Gollwitzer

Am 05.08.17 um 01:45 schrieb Ulli Horlacher:

I have to transfer a python 2.7 CLI programm into one with a (simple) GUI.
The program must run on Linux and Windows and must be compilable with
pyinstall, because I have to ship a standalone windows.exe
Any kind of installer is not acceptable.


TkInter works with pyinstaller, done that before. Also I suspect you are 
continuing your work with the file transfer service, and in that case, 
congratulations that you came to the conclusion to do a proper GUI ;)


Back then I even created a "getting started" version for you, see here:

https://groups.google.com/d/msg/comp.lang.python/AB42dD5xB_U/pYG6-rnMBQAJ

Maybe it is useful now. If you replace the "askopenfilename" by 
"askopenfilenames", it will allow to select multiple files and return 
them as a list.


Christian

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


Re: Challenge: find the first value where two functions differ

2017-08-04 Thread Steve D'Aprano
On Sat, 5 Aug 2017 06:17 am, Terry Reedy wrote:

[...]
> With 53 binary digits, all counts from 0 to 2**53 - 1 are exactly
> represented with a exponent of 0,  2**53 = 2**52 * 2, so it is exactly
> represented with an exponent of 1. Many other higher counts can be
> exactly represented with various exponents, but 2**53 + 1 requires 54
> digits.  So it is the first count that does not have an exact binary64
> representation.

Indeed. But although 2**53 + 1 cannot be represented as a float, there is no
difference between its integer square root and the integer square root of
2**53. Both are 94906265.


[...]
>>> We know that:
>>>
>>> - for n <= 2**53, isqrt_float(n) is exact;
> 
> but it is not ;-).  In this range, the only possibility is
> math.sqrt(m*m-1) being m instead of m - delta and that is the case for
> Chris' answer 4503599761588224 = 67108865**2 - 1.
> int(math.float(4503599761588224) is 67108865 instead of 67108864.

Indeed. I had forgotten one possibility.


I was thinking that there are only two cases when n is less than 2**53:

(1) n is a perfect square, in which case sqrt(n) is also a whole number which
can be represented exactly, and sqrt(n) is the same as integer sqrt(n);

(2) or n is not a perfect square, in which case sqrt(n) returns a number of the
form a.b, where a is the integer sqrt of n.

But in fact there's a third possibility: if the .b part of a.b is sufficiently
large that it rounds up to (a+1).0. It was this case that I had forgotten
about: the possibility of rounding up to the next whole number.

That's exactly what happens here with Chris' case. The exact mathematically
correct value of sqrt(4503599761588224) is a surd, but we can use Decimal to
get an approximate answer:


py> from decimal import Decimal
py> m = Decimal(4503599761588224)
py> m.sqrt()
Decimal('67108864.254941951410')

Now 67108864.254941951410... is not exactly representable as a binary
float. The two possibilities are:

67108864.  # round down
67108865.  # round up

Rounding down is *closer* to the correct value, which suggests that rounding up
is a bug:

py> m.sqrt() - Decimal('67108864.')  # round down
Decimal('2.54941951410E-9')
py> m.sqrt() - Decimal('67108865.0')  # round up
Decimal('-7.45058048590E-9')


Am I right that math.sqrt(4503599761588224) should round down?


-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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