utils.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:trip 作者: littlecodersh 项目源码 文件源码
def iter_slices_future(r, slice_length, decode_unicode=False):
    """Iterate over slices of a string."""
    pos, string = 0, r._content
    if r.encoding is None:
        decode_unicode = False
    if decode_unicode:
        decoder = codecs.getincrementaldecoder(r.encoding)(errors='replace')

    if slice_length is None or slice_length <= 0:
        slice_length = len(string)

    while pos < len(string):
        future = Future()
        chunk = string[pos:pos + slice_length]
        if decode_unicode:
            chunk = decoder.decode(chunk)
        future.set_result(chunk)
        yield future
        pos += slice_length

    chunk = decoder.decode(b'', final=True)
    if chunk:
        future = Future()
        future.set_result(chunk)
        yield future
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号