baker.py 文件源码

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

项目:rank_biased_precision_implementation 作者: vatsal-sodha 项目源码 文件源码
def write(fobj, content, convert=True):
        """
        Utility function used to write content to a file. Allow compatibility
        between Python versions.
        """

        # This function automatically converts strings to bytes
        # if running under Python 3. Otherwise we cannot write
        # to a file.

        # First detect whether fobj requires binary stream
        if hasattr(fobj, 'mode'):
            # A file-like object
            binary = 'b' in fobj.mode
        else:
            # A subclass of io.BufferedIOBase?
            binary = isinstance(fobj, io.BufferedIOBase)
        # If we are running under Python 3 and binary is required
        if sys.version_info[:2] >= (3, 0) and convert and binary:  # pragma: no cover
            content = bytes(content, 'utf-8')

        fobj.write(content)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号