strings.py 文件源码

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

项目:node-gn 作者: Shouqun 项目源码 文件源码
def collect_string_fields(format_string):
    """ Given a format string, return an iterator
    of all the valid format fields. It handles nested fields
    as well.
    """

    formatter = string.Formatter()
    try:
        parseiterator = formatter.parse(format_string)
        for result in parseiterator:
            if all(item is None for item in result[1:]):
                # not a replacement format
                continue
            name = result[1]
            nested = result[2]
            yield name
            if nested:
                for field in collect_string_fields(nested):
                    yield field
    except ValueError:
        # probably the format string is invalid
        # should we check the argument of the ValueError?
        raise utils.IncompleteFormatString(format_string)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号