コード例 #1
0
def load(abspath, default=None, enable_verbose=True):
    """Load Pickle from file. If file are not exists, returns ``default``.

    :param abspath: file path. use absolute path as much as you can. 
      extension has to be ``.pickle`` or ``.gz`` (for compressed Pickle). 
    :type abspath: string

    :param default: default ``dict()``, if ``abspath`` not exists, return the
        default Python object instead.

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

    Usage::

        >>> from dataIO import pk
        >>> pk.load("test.pickle") # if you have a pickle file
        Load from `test.pickle` ...
            Complete! Elapse 0.000432 sec.
        {'a': 1, 'b': 2}

    **中文文档**

    从Pickle文件中读取数据

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

    :param default: 默认 ``dict()``, 如果文件路径不存在, 则会返回指定的默认值

    :param enable_verbose: 默认 ``True``, 信息提示的开关, 批处理时建议关闭
    :type enable_verbose: ``布尔值``
    """
    if default is None:
        default = dict()

    prt("\nLoad from '%s' ..." % abspath, enable_verbose)

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

    if not os.path.exists(abspath):
        prt("    File not found, use default value: %r" % default,
            enable_verbose)
        return default

    st = time.clock()
    if is_pickle:
        data = pickle.loads(textfile.readbytes(abspath))
    else:
        data = pickle.loads(compress.read_gzip(abspath))

    prt("    Complete! Elapse %.6f sec." % (time.clock() - st), enable_verbose)
    return data
コード例 #2
0
def load(abspath, default=None, enable_verbose=True):
    """Load Json from file. If file are not exists, returns ``default``.

    :param abspath: file path. use absolute path as much as you can. 
      extension has to be ``.json`` or ``.gz`` (for compressed Json). 
    :type abspath: string

    :param default: default ``dict()``, if ``abspath`` not exists, return the
        default Python object instead.

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

    Usage::

        >>> from dataIO import js
        >>> js.load("test.json") # if you have a json file
        Load from 'test.json' ...
            Complete! Elapse 0.000432 sec.
        {'a': 1, 'b': 2}

    **中文文档**

    从Json文件中读取数据

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

    :param default: 默认 ``dict()``, 如果文件路径不存在, 则会返回指定的默认值

    :param enable_verbose: 默认 ``True``, 信息提示的开关, 批处理时建议关闭
    :type enable_verbose: ``布尔值``
    """
    if default is None:
        default = dict()

    #prt("\nLoad from '%s' ..." % abspath, enable_verbose)

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

    if not os.path.exists(abspath):
        #prt("    File not found, use default value: %r" %
        #    default, enable_verbose)
        return default

    st = time.process_time()
    if is_json:
        data = json.loads(textfile.read(abspath, encoding="utf-8"))
    else:
        data = json.loads(compress.read_gzip(abspath).decode("utf-8"))

    #prt("    Complete! Elapse %.6f sec." % (time.process_time() - st), enable_verbose)
    return data
コード例 #3
0
ファイル: pk.py プロジェクト: MacHu-GWU/docfly-project
def load(abspath, default=dict(), enable_verbose=True):
    """Load Pickle from file. If file are not exists, returns ``default``.

    :param abspath: file path. use absolute path as much as you can. 
      extension has to be ``.pickle`` or ``.gz`` (for compressed Pickle). 
    :type abspath: string

    :param default: default ``dict()``, if ``abspath`` not exists, return the
        default Python object instead.

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

    Usage::

        >>> from dataIO import pk
        >>> pk.load("test.pickle") # if you have a pickle file
        Load from `test.pickle` ...
            Complete! Elapse 0.000432 sec.
        {'a': 1, 'b': 2}

    **中文文档**

    从Pickle文件中读取数据

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

    :param default: 默认 ``dict()``, 如果文件路径不存在, 则会返回指定的默认值

    :param enable_verbose: 默认 ``True``, 信息提示的开关, 批处理时建议关闭
    :type enable_verbose: ``布尔值``
    """
    prt("\nLoad from '%s' ..." % abspath, enable_verbose)
    
    abspath = lower_ext(str(abspath))
    is_pickle = is_pickle_file(abspath)
        
    if not os.path.exists(abspath):
        prt("    File not found, use default value: %r" % default, enable_verbose)
        return default

    st = time.clock()
    if is_pickle:
        data = pickle.loads(textfile.readbytes(abspath))
    else:
        data = pickle.loads(compress.read_gzip(abspath))
        
    prt("    Complete! Elapse %.6f sec." % (time.clock() - st), enable_verbose)
    return data
コード例 #4
0
def test_gzip():
    compress.write_gzip(b, path)
    b1 = compress.read_gzip(path)

    assert b == b1
    os.remove(path)