English 中文(简体)
粉碎法的这种封锁是怎样的?
原标题:What does this block of Python code do?

我正在撰写一本假歌雕,我希望能够随机地从档案中产生一字,并发挥作用。 但是,我从一个网站上拿到这个法典的一行,它帮助我做我需要做的事情,但我确实知道how<>strong>。

增 编

   offset = random.randint(0, os.stat(filename)[6]) # ?????
   fd = file(filename,  rb )
   fd.seek(offset)
   fd.readline()
   return fd.readline()
最佳回答

stat(filename)[6]简单地将.filename上点名的文档的大小按字母顺序排列。 http://docs.python.org/library/os.htm。

random.randint(...) generates a random integer between zero and n, where n is the size of the file obtained via os.stat().

The code then seeks to that (random) position in the file. The chances are that this position is in the middle of a line. Therefore, the code reads the partial line and discards it. It then reads the next line and returns it.

最后,这部法典有一点点:如果档案最后一行的随机位置,那么第二个<编码>(<>readline()将没有任何内容可读。

<<>strong>edit: 而且,正如“Russell Borogove”在评论中指出的那样,这种方法确保了选择相同的可能性。

问题回答

为了扩大六分辨率,在文档“距”内有随机分类后,我们将用<代码>fd.seek(offset)进入该地点。 我们使用<条码>fd.readline(>)来缩小我们所走的界线,并移至下一条。 然后,我们使用<条码>fd.readline()来恢复我们目前整个行程。

Note that if you end up on the last line of the file, you will return an empty string. To demonstrate set your offset to os.stat(filename)[6] - 1 and use readline twice.

我试图补充这一意见,但可能没有列入一个法典的例子。

这里,你所包括的法典中最后一行/第一行bug:

size = os.stat(filename)[6]
offset = random.randint(0, size) # ?????
fd = file(filename,  rb )
fd.seek(offset)
fd.readline()
if fd.tell() == size:
    fd.seek(0)
return fd.readline()

它没有解决@russell-borogove所描述的统一问题。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签