本文整理汇总了Python中bson.py3compat.StringIO.close方法的典型用法代码示例。如果您正苦于以下问题:Python StringIO.close方法的具体用法?Python StringIO.close怎么用?Python StringIO.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bson.py3compat.StringIO
的用法示例。
在下文中一共展示了StringIO.close方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: write_me
# 需要导入模块: from bson.py3compat import StringIO [as 别名]
# 或者: from bson.py3compat.StringIO import close [as 别名]
def write_me(s, chunk_size):
buf = StringIO(s)
infile = GridIn(self.db.fs)
while True:
to_write = buf.read(chunk_size)
if to_write == b(''):
break
infile.write(to_write)
infile.close()
buf.close()
outfile = GridOut(self.db.fs, infile._id)
data = outfile.read()
self.assertEqual(s, data)
示例2: GridIn
# 需要导入模块: from bson.py3compat import StringIO [as 别名]
# 或者: from bson.py3compat.StringIO import close [as 别名]
class GridIn(object):
def __init__(self, client, root_collection, **kwargs):
self.client = client
self.root_collection = root_collection
self._files = self.client.connection(files_coll(self.root_collection))
self._chunks = self.client.connection(chunks_coll(self.root_collection))
# Handle alternative naming
if "content_type" in kwargs:
kwargs["contentType"] = kwargs.pop("content_type")
if "chunk_size" in kwargs:
kwargs["chunkSize"] = kwargs.pop("chunk_size")
# Defaults
kwargs["_id"] = kwargs.get("_id", ObjectId())
kwargs["chunkSize"] = kwargs.get("chunkSize", DEFAULT_CHUNK_SIZE)
self._file = kwargs
self._chunk_number = 0
self._position = 0
self._buffer = StringIO()
def __flush_data(self, data):
def no_check(*arg, **kwargs):
pass
chunk = {"files_id": self._file['_id'],
"n": self._chunk_number,
"data": Binary(data)}
self._chunks.insert(chunk, callback = no_check)
self._chunk_number += 1
self._position += len(data)
def __flush_buffer(self):
self.__flush_data(self._buffer.getvalue())
self._buffer.close()
self._buffer = StringIO()
def write(self, data, callback = None, **kwargs):
self._file['length'] = len(data)
self._file.update(kwargs)
try:
read = data.read
except AttributeError:
read = StringIO(data).read
if self._buffer.tell() > 0:
space = self.chunk_size = self._buffer.tell()
if space:
to_write = read(space)
self._buffer.write(to_write)
if len(to_write) < space:
return # EOF
self.__flush_buffer()
to_write = read(self._file['chunkSize'])
while to_write and len(to_write) == self._file['chunkSize']:
self.__flush_data(to_write)
to_write = read(self._file['chunkSize'])
self._buffer.write(to_write)
def __flush(self):
self.__flush_buffer()
def no_check(*arg, **kwargs):
pass
def cb_md5(*arg, **kwargs):
try:
self._file['md5'] = arg[0]['md5']
except:
pass
self._file["uploadDate"] = datetime.datetime.utcnow()
self._files.insert(self._file, callback = no_check)
self.client.command('filemd5', self._file['_id'], root=self.root_collection, callback=cb_md5)
def close(self):
self.__flush()