我找到了这段代码here,它允许我从一个在线zip文件下载一个文件。它奇迹般地工作,但我不明白它是如何工作的,尤其是class
是如何工作的(我只对class
有一些基本的了解)。我简化了一点原始代码得到下面的MWE。在
import zipfile
import urllib2
DEBUG = True
def HTTPGetFileSize(url):
request = urllib2.Request(url)
page = urllib2.urlopen(request)
size = page.headers['content-length']
page.close()
return int(size)
def HTTPGetPartialData(url, f, t):
request = urllib2.Request(url)
request.headers['range'] = 'bytes=%u-%u' % (f, t)
partial_page = urllib2.urlopen(request)
partial_data = partial_page.read()
partial_page.close()
return partial_data
class MyFileWrapper:
def __init__(self, url):
self.url = url
self.position = 0
self.total_size = HTTPGetFileSize(url)
def seek(self, offset, whence):
if whence == 0:
self.position = offset
elif whence == 1:
self.position += offset
elif whence == 2:
self.position = self.total_size + offset
if DEBUG==True:
print "seek: (%u) %u -> %u" % (whence, offset, self.position)
pass
def tell(self):
if DEBUG==True:
print "tell: -> %u" % self.position
return self.position
def read(self, amount=-1):
if amount == -1:
amount = self.total_size - self.position
d = HTTPGetPartialData(self.url, self.position, self.position + amount - 1)
self.position += len(d)
if DEBUG==True:
print "read: %u %u -> %u" % (self.position - len(d), amount, self.position)
return d
url = 'http://the.url.that/contains/the/zipfiles.zip'
filename = 'the_name_of_the_file_I_need.csv'
f = MyFileWrapper(url)
print "class like object f is constructed"
z = zipfile.ZipFile(f)
print "f is read by zipfile and passed to z"
content = z.open(filename)
print "open filename, pass to content"
print content.read()
我有很多问题,但我主要困惑于:
filename
是如何进入所有函数的?在tell
函数之后,代码又回到了seek
函数。在offset
和{感谢任何帮助。在
编辑:我包括代码的调试版本,下面是一个示例测试的输出:
^{pr2}$
这是Python的“duck typing”方法的一个主要示例。为了解释这一点,让我们首先从程序员的角度考虑一下文件实际上是什么:
示例中的代码提供了一个
class
,它实现了一个read
方法,最后返回字节,因此可以像Python中的普通文件对象一样对待它(如果它有一个read
方法,那么它就是一个文件!)在考虑这个最小的例子:
类
SimpleFileUser
可以将任何内容用作实现read
方法的文件。它可以是open
返回的file对象,也可以是SimpleFile
类的实例,因为这个类还提供了一个read
方法。在类
MyFileWrapper
实现了允许您访问可通过HTTP访问的文件的函数。因此,它提供函数tell
文件中的当前位置,seek
(跳转)到文件中的另一个位置,以及read
文件中的实际数据。本例中的文件是通过HTTP访问的。如何调用方法取决于ZipFile
。如果在原始代码中使用DEBUG
变量,您可以看到ZipFile
实际在做什么来读取数据。在offset
和whence
只是seek
函数的参数。{a2:}是仿照a2建模的。文件/HTTP可访问内容中的当前位置存储在成员变量self.position
定义了
seek
和tell
方法,通过将文件指针设置到文件末尾(seek(2, 0)
)并获得文件指针位置(tell
),使zipfile
类能够确定文件大小。在没有。它只传递到ZipFile中。此代码中的函数未使用文件名。在
没有具体的命令。我不知道你在问什么。在
在ZipFile中,通过ZipFile代码。在
这段代码的作用是包装一个在线文件,这样只下载它实际读取的部分。其余的魔法行为是标准的。有趣的是
read()
方法。在相关问题 更多 >
编程相关推荐