Re: [Tutor] pickle problems

2012-08-24 Thread Richard D. Moores
Case Van Horsen wrote the following to me about gmpy2.is_prime. I post it with his permission. Dick Moores The summary: gmpy2.is_prime() just provides direct access to the underlying library (GMP or MPIR) function. Depending on the library and version, the behavior is subtly different. With the

Re: [Tutor] pickle problems

2012-08-23 Thread Dave Angel
On 08/23/2012 04:43 AM, Steven D'Aprano wrote: > On Wed, Aug 22, 2012 at 11:39:46PM -0400, Dave Angel wrote: >> On 08/22/2012 07:32 PM, Richard D. Moores wrote: >>> >>> >>> My code uses gmpy2.is_prime() (lines 79 and 89). is_prime() is VERY fast. >> You do know that this gmpy2 function is only st

Re: [Tutor] pickle problems

2012-08-23 Thread Richard D. Moores
On Wed, Aug 22, 2012 at 8:39 PM, Dave Angel wrote: > On 08/22/2012 07:32 PM, Richard D. Moores wrote: >> >> >> My code uses gmpy2.is_prime() (lines 79 and 89). is_prime() is VERY fast. > > You do know that this gmpy2 function is only statistically correct ? Yes. See Steven's reply for the proba

Re: [Tutor] pickle problems

2012-08-23 Thread Steven D'Aprano
On Wed, Aug 22, 2012 at 11:39:46PM -0400, Dave Angel wrote: > On 08/22/2012 07:32 PM, Richard D. Moores wrote: > > > > > > My code uses gmpy2.is_prime() (lines 79 and 89). is_prime() is VERY fast. > > You do know that this gmpy2 function is only statistically correct ? it > can false positive.

Re: [Tutor] pickle problems

2012-08-22 Thread Dave Angel
On 08/22/2012 07:32 PM, Richard D. Moores wrote: > > > My code uses gmpy2.is_prime() (lines 79 and 89). is_prime() is VERY fast. You do know that this gmpy2 function is only statistically correct ? it can false positive. I don't know what the probs are, but it uses Miller-Rabin, with a default

Re: [Tutor] pickle problems

2012-08-22 Thread Richard D. Moores
On Wed, Aug 22, 2012 at 11:54 AM, Steven D'Aprano wrote: > On 23/08/12 02:17, Richard D. Moores wrote: >> >> I've incorporated many of the suggestions I've received here, plus some >> important tips from Case Van Horsen, the gmpy2 maintainer, and I believe one >> of its developers. >> >> Here's

Re: [Tutor] pickle problems

2012-08-22 Thread Steven D'Aprano
On 23/08/12 02:17, Richard D. Moores wrote: I've incorporated many of the suggestions I've received here. Here's a function, factor_integer(), for quickly factoring any integer up to 1e17:. That relies on a pre-existing cache of prime numbers. If you don't use tho

Re: [Tutor] pickle problems

2012-08-22 Thread Richard D. Moores
I've incorporated many of the suggestions I've received here. Here's a function, factor_integer(), for quickly factoring any integer up to 1e17: . Larger integers will be factored eventually -- the wait can be long or short. Probably long if they require the services

Re: [Tutor] pickle problems

2012-08-12 Thread Richard D. Moores
On Sun, Aug 12, 2012 at 10:49 AM, eryksun wrote: > On Sun, Aug 12, 2012 at 10:58 AM, Dave Angel wrote: > Good catch in Kent Johnson's code. Maybe he'll search for his name and > find this. It should be `r = r // factor`. The fault was probably mine. Kent wrote this in 2004, in Python 2.x. When

Re: [Tutor] pickle problems

2012-08-12 Thread Peter Otten
Richard D. Moores wrote: > f = open("factors.txt", 'rb') > data = pickle.load(f) > f.close f.close looks up the close method but doesn't invoke it; you need f.close(). Alternatively use a with statement: with open("factors.txt", "rb") as f: data = pickle.load(f) This will close the file eve

Re: [Tutor] pickle problems

2012-08-12 Thread eryksun
On Sun, Aug 12, 2012 at 10:58 AM, Dave Angel wrote: > > 2) You wind up with a floating point number. If you're getting floats, > then you're limited to their precision, maybe 18 digits, and limited to > their speed. Perhaps you need to use the // divide rather than the / > one. And perhaps it'd

Re: [Tutor] pickle problems

2012-08-12 Thread Dave Angel
On 08/12/2012 04:44 AM, Richard D. Moores wrote: > > > One caveat is that I have yet to fix the problem with lines 75-78. > > One thing I'd like to implement is a monitor of the time > factorsOfInteger(n) takes to process some of the 18-digit ints (line > 153). Most are processed within a second

Re: [Tutor] pickle problems

2012-08-12 Thread eryksun
On Sun, Aug 12, 2012 at 8:46 AM, eryksun wrote: > > t = threading.Thread(target=factorsOfInteger, args=(n, qin, qout)).start() Sorry I need to proofread better. That should be the following: t = threading.Thread(target=factorsOfInteger, args=(n, qin, qout)) t.start()

Re: [Tutor] pickle problems

2012-08-12 Thread eryksun
On Sun, Aug 12, 2012 at 7:44 AM, Richard D. Moores wrote: > > I just discovered > os.path.getsize('factors.txt') > and that factors.txt has a size of 2 bytes when "empty". > (I changed the file extension to .txt so I could delete the contents.) No, an empty file has no data; the size is 0. You mu

Re: [Tutor] pickle problems

2012-08-12 Thread eryksun
On Sun, Aug 12, 2012 at 4:44 AM, Richard D. Moores wrote: > > OK, thanks for the code, which I will duly study. However, I just > pasted my new version, pickle_attempt_for_web2.py at > . I've tested it and tested it, and it > does exactly what I wanted (thanks to you!

Re: [Tutor] pickle problems

2012-08-12 Thread Richard D. Moores
On Sun, Aug 12, 2012 at 2:13 AM, Richard D. Moores wrote: . > But what > about case where factors.dat is empty? Is there a test for that? I just discovered os.path.getsize('factors.txt') and that factors.txt has a size of 2 bytes when "empty". (I changed the file extension to .txt so I could dele

Re: [Tutor] pickle problems

2012-08-12 Thread Richard D. Moores
On Sun, Aug 12, 2012 at 1:00 AM, Alan Gauld wrote: > On 12/08/12 03:43, Richard D. Moores wrote: > >> === >> if "factors.dat": > > This is testing if the string is True, which it always is. > I assume you intended something like > > if os.path.exists('factors.dat'): > > >> f = open("f

Re: [Tutor] pickle problems

2012-08-12 Thread Richard D. Moores
On Sat, Aug 11, 2012 at 9:51 PM, eryksun wrote: > On Sat, Aug 11, 2012 at 11:19 PM, Richard D. Moores > wrote: >> >>> To clarify, you can store multiple pickles in a file, but each needs >>> its own load. So you'd have to maintain a session dictionary for the >>> factors of new integers. Then ap

Re: [Tutor] pickle problems

2012-08-12 Thread Alan Gauld
On 12/08/12 03:43, Richard D. Moores wrote: === if "factors.dat": This is testing if the string is True, which it always is. I assume you intended something like if os.path.exists('factors.dat'): f = open("factors.dat", 'rb') data = pickle.load(f) f.close D = dat

Re: [Tutor] pickle problems

2012-08-11 Thread eryksun
On Sat, Aug 11, 2012 at 11:19 PM, Richard D. Moores wrote: > >> To clarify, you can store multiple pickles in a file, but each needs >> its own load. So you'd have to maintain a session dictionary for the >> factors of new integers. Then append the pickled session to the file >> when the user quit

Re: [Tutor] pickle problems

2012-08-11 Thread eryksun
On Sat, Aug 11, 2012 at 10:43 PM, Richard D. Moores wrote: > > Changing to 'wb' mode and using the file extension .dat completely > corrected the problems, it seems. Line 69 now reads, > f = open("factors.dat", 'wb') The extension doesn't affect anything about the file. It's just .txt indicates a

Re: [Tutor] pickle problems

2012-08-11 Thread Richard D. Moores
On Sat, Aug 11, 2012 at 6:58 PM, eryksun wrote: > On Sat, Aug 11, 2012 at 9:18 PM, eryksun wrote: >> > To clarify, you can store multiple pickles in a file, but each needs > its own load. So you'd have to maintain a session dictionary for the > factors of new integers. Then append the pickled se

Re: [Tutor] pickle problems

2012-08-11 Thread Richard D. Moores
On Sat, Aug 11, 2012 at 6:18 PM, eryksun wrote: > On Sat, Aug 11, 2012 at 6:30 PM, Richard D. Moores wrote: >> >> I wrote pickle_attempt.py as an exercise to try to learn to use the >> pickle module. See the version I edited for Tutor, >> pickle_attempt_for_web.py at >>

Re: [Tutor] pickle problems

2012-08-11 Thread eryksun
On Sat, Aug 11, 2012 at 9:18 PM, eryksun wrote: > > On line 68 you open the file in 'ab' mode. A pickle stream ends with > an ASCII period (\x2e). Anything appended after that is ignored. Use > 'wb' mode. Also, Python 3.x defaults to protocol 3, which is binary, > so you might want a file extensio

Re: [Tutor] pickle problems

2012-08-11 Thread eryksun
On Sat, Aug 11, 2012 at 6:30 PM, Richard D. Moores wrote: > > I wrote pickle_attempt.py as an exercise to try to learn to use the > pickle module. See the version I edited for Tutor, > pickle_attempt_for_web.py at > . > > But after closing the program and restart

[Tutor] pickle problems

2012-08-11 Thread Richard D. Moores
64-bit Win 7 Python 3.2.3 (default, Apr 11 2012, 07:12:16) [MSC v.1500 64 bit (AMD64)] I wrote pickle_attempt.py as an exercise to try to learn to use the pickle module. See the version I edited for Tutor, pickle_attempt_for_web.py at . To show the problems, I've pas