io.py 文件源码

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

项目:blender_addons_collection 作者: manorius 项目源码 文件源码
def dump(filepath, data, options=None):
    """Dump the output to disk (JSON, msgpack, etc)

    :param filepath: output file path
    :param data: serializable data to write to disk
    :param options: (Default value = None)
    :type options: dict

    """
    options = options or {}
    logger.debug("io.dump(%s, data, options=%s)", filepath, options)

    compress = options.get(constants.COMPRESSION, constants.NONE)
    if compress == constants.MSGPACK:
        try:
            import msgpack
        except ImportError:
            logger.error("msgpack module not found")
            raise

        logger.info("Dumping to msgpack")
        func = lambda x, y: msgpack.dump(x, y)
        mode = 'wb'
    else:
        round_off = options.get(constants.ENABLE_PRECISION)
        if round_off:
            _json.ROUND = options[constants.PRECISION]
        else:
            _json.ROUND = None

        indent = options.get(constants.INDENT, True)
        indent = 4 if indent else None
        compact_separators = (',', ':')
        logger.info("Dumping to JSON")
        func = lambda x, y: _json.json.dump(x, y, indent=indent, separators=compact_separators)
        mode = 'w'

    logger.info("Writing to %s", filepath)
    with open(filepath, mode=mode) as stream:
        func(data, stream)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号