__init__.py 文件源码

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

项目:blender 作者: gastrodia 项目源码 文件源码
def format(tokens, formatter, outfile=None):  # pylint: disable=redefined-builtin
    """
    Format a tokenlist ``tokens`` with the formatter ``formatter``.

    If ``outfile`` is given and a valid file object (an object
    with a ``write`` method), the result will be written to it, otherwise
    it is returned as a string.
    """
    try:
        if not outfile:
            realoutfile = getattr(formatter, 'encoding', None) and BytesIO() or StringIO()
            formatter.format(tokens, realoutfile)
            return realoutfile.getvalue()
        else:
            formatter.format(tokens, outfile)
    except TypeError as err:
        if isinstance(err.args[0], str) and \
           ('unbound method format' in err.args[0] or
            'missing 1 required positional argument' in err.args[0]):
            raise TypeError('format() argument must be a formatter instance, '
                            'not a class')
        raise
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号