一个zope3/grok文件表示包。
dolmen.file的Python项目详细描述
兼容性
为了确保我们的 文件 实现是完整的,并且 功能性的,我们用原始的 zope.app.file 测试来测试它:
>>> from dolmen.file import NamedFile, INamedFile, FileChunk
让我们测试构造函数:
>>> file = NamedFile() >>> file.contentType '' >>> file.data '' >>> file = NamedFile('Foobar') >>> file.contentType '' >>> file.data 'Foobar' >>> file = NamedFile('Foobar', 'text/plain') >>> file.contentType 'text/plain' >>> file.data 'Foobar' >>> file = NamedFile(data='Foobar', contentType='text/plain') >>> file.contentType 'text/plain' >>> file.data 'Foobar'
让我们来测试变异器:
>>> file = NamedFile() >>> file.contentType = 'text/plain' >>> file.contentType 'text/plain' >>> file.data = 'Foobar' >>> file.data 'Foobar' >>> file.data = None Traceback (most recent call last): ... TypeError: Cannot set None data on a file.
让我们测试大数据输入:
>>> file = NamedFile() Insert as string: >>> file.data = 'Foobar'*60000 >>> file.size 360000 >>> file.data == 'Foobar'*60000 True
将数据作为文件块插入:
>>> fc = FileChunk('Foobar'*4000) >>> file.data = fc >>> file.size 24000 >>> file.data == 'Foobar'*4000 True
从文件对象插入数据:
>>> import cStringIO >>> sio = cStringIO.StringIO() >>> sio.write('Foobar'*100000) >>> sio.seek(0) >>> file.data = sio >>> file.size 600000 >>> file.data == 'Foobar'*100000 True
最后,但并非最不重要的是,验证接口实现:
>>> from zope.interface.verify import verifyClass >>> INamedFile.implementedBy(NamedFile) True >>> verifyClass(INamedFile, NamedFile) True