> 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
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
> 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.
>
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
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
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) :
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
> ---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
>
> 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
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
> ---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
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
> 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
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
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
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
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
"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
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
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
20 matches
Mail list logo