Re: [Tutor] reading random line from a file

2007-07-23 Thread Tiger12506
> Significance of number 4096 : > file is stored in blocks of size 2K/4K/8K (depending > upon the machine). file seek for an offset goes block > by block rather than byte by byte. Hence for file size > < 4096 (assuming you have 4K block size), you will > anyway end up scanning it entirely so as wel

Re: [Tutor] reading random line from a file

2007-07-20 Thread Aditya Lal
A bug: The function random.randint(a,b) include both ends i.e. b is also included. Thus for file with single line a=0,b=1 my algo will give an IndexError. Significance of number 4096 : file is stored in blocks of size 2K/4K/8K (depending upon the machine). file seek for an offset goes block by blo

Re: [Tutor] reading random line from a file

2007-07-19 Thread Tiger12506
> I think the best strategy for this problem would be to build an index of > the offset of the start of each line, and then randomly select from this > list. > that makes each line equally probable, and you can set up your class so > that the index is only built on the first call to the function. >

Re: [Tutor] reading random line from a file

2007-07-19 Thread Luke Paireepinart
bhaaluu wrote: > Greetings, > Thanks for including the complete source code! > It really helps to have something that works to look at. > I modified an earlier version of this to run on my > computer (GNU/Linux; Python 2.4.3). > I think the best strategy for this problem would be to build an ind

Re: [Tutor] reading random line from a file

2007-07-19 Thread bhaaluu
Greetings, Thanks for including the complete source code! It really helps to have something that works to look at. I modified an earlier version of this to run on my computer (GNU/Linux; Python 2.4.3). The os.module() stuff is new for me, so I played around with it... here's my modified file just i

Re: [Tutor] reading random line from a file

2007-07-19 Thread Aditya Lal
Sorry, I did not see the other thread in which this approach has already been covered. The point Kent has raised about going into infinite loop with file having single line is very true. Following is the corrected version (for completeness sake) - import os,random def getrandfromMem(filename) :

Re: [Tutor] reading random line from a file

2007-07-19 Thread Aditya Lal
An alternative approach (I found the Yorick's code to be too slow for large # of calls) : We can use file size to pick a random point in the file. We can read and ignore text till next new line. This will avoid outputting partial lines. Return the next line (which I guess is still random :)). In

Re: [Tutor] reading random line from a file

2007-07-18 Thread Nathan Coulter
> ---Original Message--- > From: Tiger12506 <[EMAIL PROTECTED]> > Yuck. Talk about a one shot function! Of course it only reads through the > file once! You only call the function once. Put a second print randline(f) > at the bottom of your script and see what happens :-) > > JS >

Re: [Tutor] reading random line from a file

2007-07-18 Thread Tiger12506
> import os > import random > > text = 'shaks12.txt' > if not os.path.exists(text): > os.system('wget http://www.gutenberg.org/dirs/etext94/shaks12.txt') > > def randline(f): >for i,j in enumerate(file(f, 'rb')): Alright. But put randline in a loop and you open a lot of file handles. Thank g

Re: [Tutor] reading random line from a file

2007-07-18 Thread Tiger12506
Yuck. Talk about a one shot function! Of course it only reads through the file once! You only call the function once. Put a second print randline(f) at the bottom of your script and see what happens :-) JS > This method only keeps one line in memory, only reads through the file > once, and doe

Re: [Tutor] reading random line from a file

2007-07-18 Thread Poor Yorick
> ---Original Message--- > From: Kent Johnson <[EMAIL PROTECTED]> > Subject: Re: [Tutor] reading random line from a file > Sent: 2007-07-18 10:19 > [SNIP] > > It probably doesn't matter, but this will pick longer lines more often > than short on

Re: [Tutor] reading random line from a file

2007-07-18 Thread Kent Johnson
Tiger12506 wrote: > If you truly wish to kill yourself trying to make it as efficient memory as > possible, then you can follow this example. (This is more like what I would > write in C). > The getrandomline function picks a random byte between the beginning and the > end of the file, then backs u

Re: [Tutor] reading random line from a file

2007-07-17 Thread Tiger12506
> wow thats pretty cool :) it's a bit above my level but it's interesting > :) thanks I'm deeply flattered! Thank you. >> # >> from os import stat os.stat returns a tuple whose 7th member is the size of the file. (python docs) >> from random impor

Re: [Tutor] reading random line from a file

2007-07-16 Thread Tiger12506
If you truly wish to kill yourself trying to make it as efficient memory as possible, then you can follow this example. (This is more like what I would write in C). The getrandomline function picks a random byte between the beginning and the end of the file, then backs up until the beginning of the

Re: [Tutor] reading random line from a file

2007-07-16 Thread Tiger12506
Perhaps ~this~ is what you are worried about performance-wise? Image NameMem Usage - python.exe11,096 K That's not too bad considering ~this~ explorer.exe 14,356 K svchost.exe24,000 K And I worry about the mp3 player I wrote in C using 2,520 K

Re: [Tutor] reading random line from a file

2007-07-15 Thread Luke Paireepinart
max baseman wrote: > cool thanks > > oh for performance eventualy i would like the file to contain many quotes Using readlines isn't exactly going to cause a performance bottleneck. I used the following code #make the file.py f = file("temp.txt","w") x = 10 while x > 0: f.write("aaa

Re: [Tutor] reading random line from a file

2007-07-14 Thread Andreas Kostyrka
Well he could implement the indexing into his program and check mtimes to decide if he needs to reindex. But yes, as long the file fits into memory, readlines (or list(file("quotes,txt")) makes more sense. Andreas -- Ursprüngl. Mitteil. -- Betreff: Re: [Tutor] reading random

Re: [Tutor] reading random line from a file

2007-07-14 Thread Alan Gauld
"max baseman" <[EMAIL PROTECTED]> wrote > im writing a quick quote reader that spits out a random quote from a > show but cant get it to pick randomly You can either get theclines to be the same length and use a random index to seek() to the start of the line you want. Or you can build a separa

Re: [Tutor] reading random line from a file

2007-07-14 Thread John Fouhy
On 15/07/07, max baseman <[EMAIL PROTECTED]> wrote: > im writing a quick quote reader that spits out a random quote from a > show but cant get it to pick randomly > i tried > a=randrange(820)+1 > text.readline(a) > > and i would prefer not having to bring evryline into the program then > picking li

Re: [Tutor] reading random line from a file

2007-07-14 Thread Luke Paireepinart
max baseman wrote: > im writing a quick quote reader that spits out a random quote from a > show but cant get it to pick randomly > i tried > a=randrange(820)+1 > text.readline(a) > > and i would prefer not having to bring evryline into the program then > picking like > > for line in text.readl