示例#1
0
def dump(data, abspath, pk_protocol=py23.pk_protocol, 
         overwrite=False, enable_verbose=True):
    """Dump picklable object to file.
    Provides multiple choice to customize the behavior.

    :param data: picklable python object.
    :type data: dict or list

    :param abspath: ``save as`` path, file extension has to be ``.pickle`` or ``.gz``
        (for compressed Pickle)
    :type abspath: string

    :param pk_protocol: default = your python version, use 2, to make a
        py2.x/3.x compatible pickle file. But 3 is faster.
    :type pk_protocol: int

    :param overwrite: default ``False``, If ``True``, when you dump to existing
        file, it silently overwrite it. If ``False``, an alert message is shown.
        Default setting ``False`` is to prevent overwrite file by mistake.
    :type overwrite: boolean

    :param enable_verbose: default True, help-message-display trigger.
    :type enable_verbose: boolean

    Usage::

        >>> from dataIO import pk
        >>> data = {"a": 1, "b": 2}
        >>> dump(data, "test.pickle", overwrite=True)
        Dump to `test.pickle` ...
            Complete! Elapse 0.002432 sec

    **中文文档**

    将Python中可被序列化的"字典", "列表"以及他们的组合, 按照Json的编码方式写入文件
    文件

    参数列表

    :param data: 可Pickle化的Python对象
    :type data: ``字典`` 或 ``列表``

    :param abspath: Pickle文件绝对路径, 扩展名需为 ``.pickle`` 或 ``.gz``, 其中 ``.gz``
      是被压缩后的Pickle文件
    :type abspath: ``字符串``

    :param pk_protocol: 默认值为你的Python大版本号, 使用2可以使得Python2/3都能
      兼容你的Pickle文件。不过Python3的速度更快。
    :type pk_protocol: int

    :param overwrite: 默认 ``False``, 当为``True``时, 如果写入路径已经存在, 则会
      自动覆盖原文件。而为``False``时, 则会打印警告文件, 防止误操作覆盖源文件。
    :type overwrite: "布尔值"

    :param enable_verbose: 默认 ``True``, 信息提示的开关, 批处理时建议关闭
    :type enable_verbose: ``布尔值``
    """
    prt("\nDump to '%s' ..." % abspath, enable_verbose)
    
    abspath = lower_ext(str(abspath))
    is_pickle = is_pickle_file(abspath)
    
    if os.path.exists(abspath):
        if not overwrite: # 存在, 并且overwrite=False
            prt("    Stop! File exists and overwrite is not allowed",
                 enable_verbose)
            return

    st = time.clock()
    content = pickle.dumps(data, pk_protocol)
    if is_pickle:
        textfile.writebytes(content, abspath)
    else:
        compress.write_gzip(content, abspath)
    
    prt("    Complete! Elapse %.6f sec." % (time.clock() - st), enable_verbose)
示例#2
0
def dump(data,
         abspath,
         pk_protocol=py23.pk_protocol,
         overwrite=False,
         enable_verbose=True):
    """Dump picklable object to file.
    Provides multiple choice to customize the behavior.

    :param data: picklable python object.
    :type data: dict or list

    :param abspath: ``save as`` path, file extension has to be ``.pickle`` or ``.gz``
        (for compressed Pickle)
    :type abspath: string

    :param pk_protocol: default = your python version, use 2, to make a
        py2.x/3.x compatible pickle file. But 3 is faster.
    :type pk_protocol: int

    :param overwrite: default ``False``, If ``True``, when you dump to existing
        file, it silently overwrite it. If ``False``, an alert message is shown.
        Default setting ``False`` is to prevent overwrite file by mistake.
    :type overwrite: boolean

    :param enable_verbose: default True, help-message-display trigger.
    :type enable_verbose: boolean

    Usage::

        >>> from dataIO import pk
        >>> data = {"a": 1, "b": 2}
        >>> dump(data, "test.pickle", overwrite=True)
        Dump to `test.pickle` ...
            Complete! Elapse 0.002432 sec

    **中文文档**

    将Python中可被序列化的"字典", "列表"以及他们的组合, 按照Json的编码方式写入文件
    文件

    参数列表

    :param data: 可Pickle化的Python对象
    :type data: ``字典`` 或 ``列表``

    :param abspath: Pickle文件绝对路径, 扩展名需为 ``.pickle`` 或 ``.gz``, 其中 ``.gz``
      是被压缩后的Pickle文件
    :type abspath: ``字符串``

    :param pk_protocol: 默认值为你的Python大版本号, 使用2可以使得Python2/3都能
      兼容你的Pickle文件。不过Python3的速度更快。
    :type pk_protocol: int

    :param overwrite: 默认 ``False``, 当为``True``时, 如果写入路径已经存在, 则会
      自动覆盖原文件。而为``False``时, 则会打印警告文件, 防止误操作覆盖源文件。
    :type overwrite: "布尔值"

    :param enable_verbose: 默认 ``True``, 信息提示的开关, 批处理时建议关闭
    :type enable_verbose: ``布尔值``
    """
    prt("\nDump to '%s' ..." % abspath, enable_verbose)

    abspath = lower_ext(str(abspath))
    is_pickle = is_pickle_file(abspath)

    if os.path.exists(abspath):
        if not overwrite:  # 存在, 并且overwrite=False
            prt("    Stop! File exists and overwrite is not allowed",
                enable_verbose)
            return

    st = time.clock()
    content = pickle.dumps(data, pk_protocol)
    if is_pickle:
        textfile.writebytes(content, abspath)
    else:
        compress.write_gzip(content, abspath)

    prt("    Complete! Elapse %.6f sec." % (time.clock() - st), enable_verbose)
示例#3
0
def dump(data, abspath, 
         indent_format=False, 
         float_precision=None, 
         overwrite=False, 
         enable_verbose=True):
    """Dump Json serializable object to file.
    Provides multiple choice to customize the behavior.

    :param data: Serializable python object.
    :type data: dict or list

    :param abspath: ``save as`` path, file extension has to be ``.json`` or ``.gz``
        (for compressed Json)
    :type abspath: string

    :param indent_format: default ``False``, If ``True``, then dump to human
      readable format, but it's slower, the file is larger
    :type indent_format: boolean

    :param float_precision: default ``None``, limit flotas to N-decimal points. 
    :type float_precision: integer

    :param overwrite: default ``False``, If ``True``, when you dump to existing
        file, it silently overwrite it. If ``False``, an alert message is shown.
        Default setting ``False`` is to prevent overwrite file by mistake.
    :type overwrite: boolean

    :param enable_verbose: default True, help-message-display trigger.
    :type enable_verbose: boolean

    Usage::

        >>> from dataIO import js
        >>> data = {"a": 1, "b": 2}
        >>> dump(data, "test.json", overwrite=True)
        Dumping to 'test.json'...
            Complete! Elapse 0.002432 sec

    **中文文档**

    将Python中可被序列化的"字典", "列表"以及他们的组合, 按照Json的编码方式写入文件
    文件

    参数列表

    :param js: 可Json化的Python对象
    :type js: ``字典`` 或 ``列表``

    :param abspath: Json文件绝对路径, 扩展名需为 ``.json`` 或 ``.gz``, 其中 ``.gz``
      是被压缩后的Json文件
    :type abspath: ``字符串``

    :param indent_format: 默认 ``False``, 当为 ``True`` 时, Json编码时会对Key进行
      排序, 并进行缩进排版。但是这样写入速度较慢, 文件体积也更大。
    :type indent_format: "布尔值"

    :param overwrite: 默认 ``False``, 当为``True``时, 如果写入路径已经存在, 则会
      自动覆盖原文件。而为``False``时, 则会打印警告文件, 防止误操作覆盖源文件。
    :type overwrite: "布尔值"

    :param float_precision: 默认 ``None``, 当为任意整数时, 则会保留小数点后N位
    :type float_precision: "整数"

    :param enable_verbose: 默认 ``True``, 信息提示的开关, 批处理时建议关闭
    :type enable_verbose: ``布尔值``
    """
    prt("\nDump to '%s' ..." % abspath, enable_verbose)
    
    abspath = lower_ext(str(abspath))
    is_json = is_json_file(abspath)
    
    if os.path.exists(abspath):
        if not overwrite: # 存在, 并且overwrite=False
            prt("    Stop! File exists and overwrite is not allowed",
                 enable_verbose)
            return

    if float_precision is not None:
        encoder.FLOAT_REPR = lambda x: format(x, ".%sf" % float_precision)
        indent_format = True
    else:
        encoder.FLOAT_REPR = repr
       
    if indent_format:
        sort_keys = True
        indent = 4
    else:
        sort_keys = False
        indent = None

    st = time.clock()
    js = json.dumps(data, sort_keys=sort_keys, indent=indent)
    content = js.encode("utf-8")
    if is_json:
        textfile.writebytes(content, abspath)
    else:
        compress.write_gzip(content, abspath)
    
    prt("    Complete! Elapse %.6f sec." % (time.clock() - st), enable_verbose)
示例#4
0
def dump(data,
         abspath,
         indent_format=False,
         float_precision=None,
         ensure_ascii=True,
         overwrite=False,
         enable_verbose=True):
    """Dump Json serializable object to file.
    Provides multiple choice to customize the behavior.

    :param data: Serializable python object.
    :type data: dict or list

    :param abspath: ``save as`` path, file extension has to be ``.json`` or ``.gz``
        (for compressed Json)
    :type abspath: string

    :param indent_format: default ``False``, If ``True``, then dump to human
      readable format, but it's slower, the file is larger
    :type indent_format: boolean

    :param float_precision: default ``None``, limit flotas to N-decimal points. 
    :type float_precision: integer

    :param overwrite: default ``False``, If ``True``, when you dump to existing
        file, it silently overwrite it. If ``False``, an alert message is shown.
        Default setting ``False`` is to prevent overwrite file by mistake.
    :type overwrite: boolean

    :param enable_verbose: default True, help-message-display trigger.
    :type enable_verbose: boolean

    Usage::

        >>> from dataIO import js
        >>> data = {"a": 1, "b": 2}
        >>> dump(data, "test.json", overwrite=True)
        Dumping to 'test.json'...
            Complete! Elapse 0.002432 sec

    **中文文档**

    将Python中可被序列化的"字典", "列表"以及他们的组合, 按照Json的编码方式写入文件
    文件

    参数列表

    :param js: 可Json化的Python对象
    :type js: ``字典`` 或 ``列表``

    :param abspath: Json文件绝对路径, 扩展名需为 ``.json`` 或 ``.gz``, 其中 ``.gz``
      是被压缩后的Json文件
    :type abspath: ``字符串``

    :param indent_format: 默认 ``False``, 当为 ``True`` 时, Json编码时会对Key进行
      排序, 并进行缩进排版。但是这样写入速度较慢, 文件体积也更大。
    :type indent_format: "布尔值"

    :param overwrite: 默认 ``False``, 当为``True``时, 如果写入路径已经存在, 则会
      自动覆盖原文件。而为``False``时, 则会打印警告文件, 防止误操作覆盖源文件。
    :type overwrite: "布尔值"

    :param float_precision: 默认 ``None``, 当为任意整数时, 则会保留小数点后N位
    :type float_precision: "整数"

    :param enable_verbose: 默认 ``True``, 信息提示的开关, 批处理时建议关闭
    :type enable_verbose: ``布尔值``
    """
    #prt("\nDump to '%s' ..." % abspath, enable_verbose)

    abspath = lower_ext(str(abspath))
    is_json = is_json_file(abspath)

    if os.path.exists(abspath):
        if not overwrite:  # 存在, 并且overwrite=False
            prt("    Stop! File exists and overwrite is not allowed",
                enable_verbose)
            return

    if float_precision is not None:
        encoder.FLOAT_REPR = lambda x: format(x, ".%sf" % float_precision)
        indent_format = True
    else:
        encoder.FLOAT_REPR = repr

    if indent_format:
        sort_keys = True
        indent = 4
    else:
        sort_keys = False
        indent = None

    st = time.process_time()
    js = json.dumps(data,
                    sort_keys=sort_keys,
                    indent=indent,
                    ensure_ascii=ensure_ascii)
    content = js.encode("utf-8")
    if is_json:
        textfile.writebytes(content, abspath)
    else:
        compress.write_gzip(content, abspath)
示例#5
0
def test_gzip():
    compress.write_gzip(b, path)
    b1 = compress.read_gzip(path)

    assert b == b1
    os.remove(path)