Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-19 Thread Steven D'Aprano
On Tue, 20 Jul 2010 09:53:25 am Steven D'Aprano wrote: > This is how you would do it with the asterisk: you need a > meta-template to make a template. Doh! I meant *without* the asterisk. > >>> meta = "(a) %%05d | (b) %%0%dd" > >>> template = meta % 5 > >>> template % (42, 42) > > '(a) 00042 | (

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-19 Thread Steven D'Aprano
On Tue, 20 Jul 2010 03:07:47 am Richard D. Moores wrote: > On Mon, Jul 19, 2010 at 09:58, ALAN GAULD wrote: > > Heres what I did: > > Search Google for "Python format strings" and from the first link > > click on String Formatting operations in the contents pane: > > > > http://docs.python.org/li

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-19 Thread Richard D. Moores
On Mon, Jul 19, 2010 at 10:18, ALAN GAULD wrote: > Wikipedia is a little more helpful but not Python oriented: > > http://en.wikipedia.org/wiki/Printf#printf_format_placeholders Yes, that's helpful. Say, I found a use for that asterisk in this little function I just wrote: def sig_digits(n,digit

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-19 Thread Alan Gauld
"Richard D. Moores" wrote The formatting operations described here are obsolete and may go away in future versions of Python. Use the new String Formatting in new code. I hope that use of '*' does disappear. It's the most confusing thing I've recently tried to get my mind around! But knowi

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-19 Thread ALAN GAULD
Cc: tutor@python.org > Sent: Monday, 19 July, 2010 18:07:47 > Subject: Re: [Tutor] A file containing a string of 1 billion random digits. > > On Mon, Jul 19, 2010 at 09:58, ALAN GAULD < > ymailto="mailto:alan.ga...@btinternet.com"; > href="mailto:alan.ga...@btinter

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-19 Thread Richard D. Moores
On Mon, Jul 19, 2010 at 09:58, ALAN GAULD wrote: > Heres what I did: > Search Google for "Python format strings" and from the first link click > on String Formatting operations in the contents pane: > > http://docs.python.org/library/stdtypes.html#string-formatting-operations > > Read item number

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-19 Thread ALAN GAULD
am website http://www.alan-g.me.uk/ - Original Message > From: Richard D. Moores > To: ALAN GAULD > Cc: tutor@python.org > Sent: Monday, 19 July, 2010 15:48:13 > Subject: Re: [Tutor] A file containing a string of 1 billion random digits. > > On Mon, Ju

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-19 Thread Richard D. Moores
On Mon, Jul 19, 2010 at 08:01, Steven D'Aprano wrote: > On Tue, 20 Jul 2010 12:48:13 am Richard D. Moores wrote: > You're looking in the wrong place. This is not part of format strings, > as it doesn't use the str.format() method. It uses the % string > interpolation operator. > > http://docs.pyt

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-19 Thread bob gailer
[snip] I did not read the documentation with enough understanding. I withdraw the use of sample. Sigh! -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.pyth

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-19 Thread bob gailer
On 7/19/2010 10:48 AM, Richard D. Moores wrote: I've been unable to find any mention of that use of the asterisk in the 3.1 docs http://docs.python.org/py3k/library/stdtypes.html#old-string-formatting -- Bob Gailer 919-636-4239 Chapel Hill NC

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-19 Thread Steven D'Aprano
On Tue, 20 Jul 2010 12:48:13 am Richard D. Moores wrote: > On Mon, Jul 19, 2010 at 07:14, ALAN GAULD wrote: > >> 4 and executed many times. Seems the 0 in 0 is > >> there when a is a 3-digit number such as 123. > >> In that case a zero is prefixed to 123 to produce > >> 0123. Or if just

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-19 Thread Richard D. Moores
On Mon, Jul 19, 2010 at 07:14, ALAN GAULD wrote: > > >> 4 and executed many times. Seems the 0 in 0 is >> there when a is a 3-digit number such as 123. >> In that case a zero is prefixed to 123 to produce >> 0123. Or if just 23, 2 zeros are prefixed, etc. >> Correct? > > Yes, the zero ind

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-19 Thread Richard D. Moores
On Mon, Jul 19, 2010 at 06:45, Peter Otten <__pete...@web.de> wrote: > No. If you fire up your python interpreter you can do > "0"*10 > '00' Ah, you're absolutely right. Sorry, I misunderstood you and your '*'. Good catch. Dick ___ Tutor m

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-19 Thread ALAN GAULD
> 4 and executed many times. Seems the 0 in 0 is > there when a is a 3-digit number such as 123. > In that case a zero is prefixed to 123 to produce > 0123. Or if just 23, 2 zeros are prefixed, etc. > Correct? Yes, the zero indicates that the string should be padded with zeros to the

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-19 Thread Peter Otten
Richard D. Moores wrote: > On Mon, Jul 19, 2010 at 04:51, Peter Otten <__pete...@web.de> wrote: >> bob gailer wrote: >> >>> Check this out: >>> >>> import random, time >>> s = time.time() >>> cycles = 1000 >>> d = "0123456789"*100 >>> f = open("numbers.txt", "w") >>> for i in xrange(n): >>> l = []

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-19 Thread Richard D. Moores
On Mon, Jul 19, 2010 at 04:51, Peter Otten <__pete...@web.de> wrote: > bob gailer wrote: > >> Check this out: >> >> import random, time >> s = time.time() >> cycles = 1000 >> d = "0123456789"*100 >> f = open("numbers.txt", "w") >> for i in xrange(n): >>    l = [] >>    l.extend(random.sample(d, 100

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-19 Thread Peter Otten
bob gailer wrote: > Check this out: > > import random, time > s = time.time() > cycles = 1000 > d = "0123456789"*100 > f = open("numbers.txt", "w") > for i in xrange(n): >l = [] >l.extend(random.sample(d, 1000)) >f.write(''.join(l)) > f.close() > print time.time() - s Note that this

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-19 Thread Richard D. Moores
On Mon, Jul 19, 2010 at 03:59, Alan Gauld wrote: > "Richard D. Moores" wrote >> >> Still, I understand yours, and not his (the return line). > > return "%0*d" % (n, random.randrange(10**n)) > > "%0*d" > > The asterisk is quite unusual but basically means substitute the next > argument but treat i

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-19 Thread Alan Gauld
"Richard D. Moores" wrote Still, I understand yours, and not his (the return line). return "%0*d" % (n, random.randrange(10**n)) "%0*d" The asterisk is quite unusual but basically means substitute the next argument but treat it as part of the format string. So: "%0*d" % (2,8) # becom

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-19 Thread Richard D. Moores
On Sun, Jul 18, 2010 at 11:07, bob gailer wrote: > Check this out: > > import random, time > s = time.time() > cycles = 1000 > d = "0123456789"*100 > f = open("numbers.txt", "w") > for i in xrange(n): > l = [] > l.extend(random.sample(d, 1000)) > f.write(''.join(l)) > f.close() > print time.ti

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-19 Thread Richard D. Moores
On Sun, Jul 18, 2010 at 16:11, Alan Gauld wrote: > "Richard D. Moores" wrote > I earlier reported that my laptop couldn't handle even 800 million. >>> >>> What do you mean, "couldn't handle"? Couldn't handle 800 million of >>> what? Obviously not bytes, >> >> I meant what the context implied

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-18 Thread Dave Angel
Steven D'Aprano wrote: On Sun, 18 Jul 2010 08:30:05 pm Richard D. Moores wrote: What do you mean, "couldn't handle"? Couldn't handle 800 million of what? Obviously not bytes, because your laptop *can* handle well over 800 million bytes. It has 4GB of memory, after all :) This is just

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-18 Thread Alan Gauld
"Richard D. Moores" wrote I earlier reported that my laptop couldn't handle even 800 million. What do you mean, "couldn't handle"? Couldn't handle 800 million of what? Obviously not bytes, I meant what the context implied. Bytes. Look back in this thread to see my description of my laptop's

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-18 Thread bob gailer
Check this out: import random, time s = time.time() cycles = 1000 d = "0123456789"*100 f = open("numbers.txt", "w") for i in xrange(n): l = [] l.extend(random.sample(d, 1000)) f.write(''.join(l)) f.close() print time.time() - s 1 million in ~1.25 seconds Therefore 1 billion in ~21 minutes

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-18 Thread R. Alan Monroe
> That's the goal of the latest version of my script at > . The best I've been able to do > so far is a file with 800 million digits. I don't think anyone else has suggested this: the numpy module can generate random bytes and has a built-in tofile() method.

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-18 Thread Richard D. Moores
On Sun, Jul 18, 2010 at 05:49, Steven D'Aprano wrote: > On Sun, 18 Jul 2010 08:30:05 pm Richard D. Moores wrote: > >> > Taking the string '555', you should get two digraphs: 55_ and _55. >> >> That seems wrong to me. When I search on '99' and there's a >> '999' I don't want to think I've f

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-18 Thread Steven D'Aprano
On Sun, 18 Jul 2010 08:30:05 pm Richard D. Moores wrote: > > Taking the string '555', you should get two digraphs: 55_ and _55. > > That seems wrong to me. When I search on '99' and there's a > '999' I don't want to think I've found 2 instances of '99'. > But that's just my preference.

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-18 Thread Richard D. Moores
On Sun, Jul 18, 2010 at 02:26, Steven D'Aprano wrote: > On Sun, 18 Jul 2010 06:49:39 pm Richard D. Moores wrote: > >> I might try >> trigraphs where the 2nd digit is 2 more than the first, and the third >> 2 more than the 2nd. E.g. '024', '135', '791', '802'. > > Why the restriction? There's only

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-18 Thread Dave Angel
Richard D. Moores wrote: On Sat, Jul 17, 2010 at 18:01, Steven D'Aprano wrote: import random def random_digits(n): "Return n random digits with one call to random." return "%0*d" % (n, random.randrange(10**n)) Thanks for implementing what I was suggesting, using zero-fill for

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-18 Thread Steven D'Aprano
On Sun, 18 Jul 2010 06:49:39 pm Richard D. Moores wrote: > I might try > trigraphs where the 2nd digit is 2 more than the first, and the third > 2 more than the 2nd. E.g. '024', '135', '791', '802'. Why the restriction? There's only 1000 different trigraphs (10*10*10), which is nothing. > Or

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-18 Thread Richard D. Moores
On Sat, Jul 17, 2010 at 18:01, Steven D'Aprano wrote: > Having generated the digits, it might be useful to look for deviations > from randomness. There should be approximately equal numbers of each > digit (100,000,000 each of 0, 1, 2, ..., 9), of each digraph > (10,000,000 each of 00, 01, 02, ..

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-18 Thread Steven D'Aprano
On Sun, 18 Jul 2010 11:51:17 am Richard D. Moores wrote: [...] > > On my not-even-close-to-high-end PC, this generates one billion > > digits in 22 minutes: > My took 218 secs. Lucky for some :) Your version, on my machine, took 16 minutes, an improvement

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Alan Gauld
"Richard D. Moores" wrote I don't fully understand your "you always have two [chunks] in memory at once". Does that take care of the overlap I think I need? I don't want you or anyone to give me the the whole answer, but could you clarify a bit about keeping 2 chunks in memory? Luke gave a g

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Luke Paireepinart
On Jul 17, 2010, at 7:19 PM, "Richard D. Moores" wrote: > I don't fully understand your "you always have two [chunks] in memory > at once". Does that take care of the overlap I think I need? I don't > want you or anyone to give me the the whole answer, but could you > clarify a bit about keeping

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Richard D. Moores
On Sat, Jul 17, 2010 at 18:01, Steven D'Aprano wrote: > Do you care about speed? If this is a script that just needs to run > once, it seems to me that the simplest, easiest to read solution is: > > import random > def random_digit(): >    return "0123456789"[random.randrange(10)] > > f = open('ra

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Richard D. Moores
On Sat, Jul 17, 2010 at 17:39, Hugo Arts wrote: > On Sun, Jul 18, 2010 at 2:24 AM, Richard D. Moores wrote: >> Alan, >> >> I should have added that file.seek and file.read don't show in the 3.x >> docs either. >> > > You just need to know where to look: > > http://docs.python.org/py3k/library/io.

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Steven D'Aprano
Do you care about speed? If this is a script that just needs to run once, it seems to me that the simplest, easiest to read solution is: import random def random_digit(): return "0123456789"[random.randrange(10)] f = open('rand_digits.txt', 'w') for i in xrange(10**9): f.write(random_dig

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Hugo Arts
On Sun, Jul 18, 2010 at 2:24 AM, Richard D. Moores wrote: > Alan, > > I should have added that file.seek and file.read don't show in the 3.x > docs either. > You just need to know where to look: http://docs.python.org/py3k/library/io.html#io.IOBase.tell read and seek are in there as well. There

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Richard D. Moores
Alan, I should have added that file.seek and file.read don't show in the 3.x docs either. Dick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Richard D. Moores
On Sat, Jul 17, 2010 at 15:36, ALAN GAULD wrote: >> Now that you see what I want to do with 1 billion random > digits, >> please give me your suggestion(s). As I mentioned > before, >> I'm very new to reading from and writing to files. > > The way old text editors used to do this - in the days whe

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Richard D. Moores
On Sat, Jul 17, 2010 at 15:39, Richard D. Moores wrote: > On Sat, Jul 17, 2010 at 15:07, Hugo Arts wrote: >> P.S.: I just thought of this: how about generating numbers that are >> one digit too long, and removing the first digit instead of adding an >> extra one? It just gets rid of the digit ex

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Richard D. Moores
On Sat, Jul 17, 2010 at 15:07, Hugo Arts wrote: > On Sat, Jul 17, 2010 at 11:48 PM, Richard D. Moores > wrote: >> On Sat, Jul 17, 2010 at 13:56, Dave Angel wrote: >>> Your concern over a leading zero applies equally to two leading zeroes, or >>> three, or whatever. >> >> I don't think so. Look

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread ALAN GAULD
> Now that you see what I want to do with 1 billion random digits, > please give me your suggestion(s). As I mentioned before, > I'm very new to reading from and writing to files. The way old text editors used to do this - in the days when we only had 16K RAM etc! was to use buffers and pages.

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Hugo Arts
On Sat, Jul 17, 2010 at 11:48 PM, Richard D. Moores wrote: > On Sat, Jul 17, 2010 at 13:56, Dave Angel wrote: >> Your concern over a leading zero applies equally to two leading zeroes, or >> three, or whatever. > > I don't think so. Look again at my function, randIntOfGivenLength(). > Try a for l

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Richard D. Moores
On Sat, Jul 17, 2010 at 13:56, Dave Angel wrote: > Your code is both far more complex than it need be, and inaccurate in the > stated goal of producing random digits. Dave, please see the code I posted in this thread more recently, . > There's no need to t

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Richard D. Moores
On Sat, Jul 17, 2010 at 11:06, Alan Gauld wrote: > > "Richard D. Moores" wrote > >> amount of memory my laptop has (4 GB). So my question is, how can I do >> this differently? I'm pretty brand new to opening and writing files. >> Here, I can't write many shorter lines, because the end result I se

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Dave Angel
Richard D. Moores wrote: That's the goal of the latest version of my script at . The best I've been able to do so far is a file with 800 million digits. But it seems the writing of 800 million digits is the limit for the amount of memory my laptop has (4 GB

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Alan Gauld
"Richard D. Moores" wrote amount of memory my laptop has (4 GB). So my question is, how can I do this differently? I'm pretty brand new to opening and writing files. Here, I can't write many shorter lines, because the end result I seek is one long string. But am I correct? Others have ans

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Richard D. Moores
Thanks, guys. See . It made the 1billion digits file in 213 seconds! And my laptop didn't begin to choke. You made my day! I'll consider all the binary info later. Dick ___ Tutor maillist - Tutor@python.org To un

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Hugo Arts
On Sat, Jul 17, 2010 at 3:34 PM, bob gailer wrote: > > You can only write strings to files. See 6.9 in the documentation: > file.write(str) Write a string to the file b mode only affects how line ends > are handled. See 2. Built-in Functions: > > The default is to use text mode, which may convert

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread bob gailer
On 7/17/2010 9:01 AM, Mark Lawrence wrote: [snip] As an alternative to the suggestions given so far, why not open the file for write in binary mode, i.e. use 'wb' instead of 'w', and don't bother converting to strings? Then when reading the file use 'rb' instead of 'r'. You can only write

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Mark Lawrence
On 17/07/2010 13:01, Richard D. Moores wrote: That's the goal of the latest version of my script at . The best I've been able to do so far is a file with 800 million digits. But it seems the writing of 800 million digits is the limit for the amount of memor

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread bob gailer
On 7/17/2010 8:01 AM, Richard D. Moores wrote: That's the goal of the latest version of my script at . The best I've been able to do so far is a file with 800 million digits. But it seems the writing of 800 million digits is the limit for the amount of memo

Re: [Tutor] A file containing a string of 1 billion random digits.

2010-07-17 Thread Hugo Arts
On Sat, Jul 17, 2010 at 2:01 PM, Richard D. Moores wrote: > That's the goal of the latest version of my script at > . The best I've been able to do > so far is a file with 800 million digits. > > But it seems the writing of 800 million digits is the limit for