代码思考_textwrapper 原理

代码实现<省去一些非关键代码>


def _wrap_chunks(self, chunks):

    lines = []
    # Arrange in reverse order so items can be efficiently popped
    # from a stack of chucks.
    chunks.reverse()

    while chunks:

        # Start the list of chunks that will make up the current line.
        # cur_len is just the length of all the chunks in cur_line.
        cur_line = []
        cur_len = 0

        # Maximum width for this line.
        width = self.width - len(indent)

        # First chunk on line is whitespace -- drop it, unless this
        # is the very beginning of the text (ie. no lines started yet).
        # eg " like"-->[" ","like"]
        if self.drop_whitespace and chunks[-1].strip() == '' and lines:
            del chunks[-1]
           
		# begin to truncate the line
        while chunks:
            l = len(chunks[-1])

            # Can at least squeeze this chunk onto the current line.
            if cur_len + l <= width:
                cur_line.append(chunks.pop())
                cur_len += l

            # Nope, this line is full.
            else:
                break
		# NOTACK
        # The current line is full, and the next chunk is too big to
        # fit on *any* line (not just this one).
        if chunks and len(chunks[-1]) > width:
            self._handle_long_word(chunks, cur_line, cur_len, width)
            cur_len = sum(map(len, cur_line))

        # If the last chunk on this line is all whitespace, drop it.
        if self.drop_whitespace and cur_line and cur_line[-1].strip() == '':
            cur_len -= len(cur_line[-1])
            del cur_line[-1]
            
		# merge cur_line to lines
        if cur_line:
            if (self.max_lines is None or
                    len(lines) + 1 < self.max_lines or
                    (not chunks or
                     self.drop_whitespace and
                     len(chunks) == 1 and
                     not chunks[0].strip()) and cur_len <= width):
                # Convert current line back to a string and store it in
                # list of all lines (return value).
                lines.append(indent + ''.join(cur_line))
            else:
                while cur_line:
                    if (cur_line[-1].strip() and
                            cur_len + len(self.placeholder) <= width):
                        cur_line.append(self.placeholder)
                        lines.append(indent + ''.join(cur_line))
                        break
                    cur_len -= len(cur_line[-1])
                    del cur_line[-1]
                else:
                    if lines:
                        prev_line = lines[-1].rstrip()
                        if (len(prev_line) + len(self.placeholder) <=
                                self.width):
                            lines[-1] = prev_line + self.placeholder
                            break
                    lines.append(indent + self.placeholder.lstrip())
                break

    return lines

Q&A

I do not know what it means? Chunks correspond roughly to words and the whitespace between them
answer: 文本块大致和单词以及单词间的空格一致,因为有时候会去掉开头和结尾的空格

docstring: 

    """_wrap_chunks(chunks : [string]) -> [string]

    Wrap a sequence of text chunks and return a list of lines of
    length 'self.width' or less.  (If 'break_long_words' is false,
    some lines may be longer than this.-->   explained by pic 1  )  Chunks correspond roughly
    to words and the whitespace between them: each chunk is
    indivisible (modulo 'break_long_words'), but a line break can
    come between any two chunks.  Chunks should not have internal
    whitespace; ie. a chunk is either all whitespace or a "word".
    Whitespace chunks will be removed from the beginning and end of
    lines, but apart from that whitespace is preserved.
    """

pic1:
在这里插入图片描述

为啥会采用双层循环来控制chunk 的读取呢
答:第一层循环用来控制整个chunk的读取,第二个循环用来控制一个width 块的读取, 之后的代码用来判断特殊情况以及块的合并

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值