Exemplo n.º 1
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
Exemplo n.º 2
0
def load(abspath, default=dict(), 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: ``布尔值``
    """
    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.clock()
    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.clock() - st), enable_verbose)
    return data
Exemplo n.º 3
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import jinja2
from dataIO import textfile
from docfly.packages.member import Module, Package
from docfly.doctree import Article

pkg = Package("docfly")
print(jinja2.Template(textfile.read("package.tpl")).render(package=pkg))
print(jinja2.Template(textfile.read("module.tpl")).render(module=pkg["zzz_manual_install"]))

article=Article(title="Hello World", path="hello-world/index.rst")
print(jinja2.Template(textfile.read("index.tpl")).render(article_list=[article,], has_toc=True))
Exemplo n.º 4
0
def test_read_zip():
    assert content == textfile.read("test_textfile.zip")
Exemplo n.º 5
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from pathlib_mate import Path
from dataIO.textfile import read, write, readlines

tags = list(readlines("tag.txt"))

path = r"D:\Workspace\py34\py34_projects\mm6doc-project\source\09-旅行时间表\content.rst"
text = read(path)

for tag in tags:
    text = text.replace(tag, " :ref:`%s` " % tag)

write(text, path)
Exemplo n.º 6
0
 def test_single_house():
     """Test with one address.
     """
     html = textfile.read(Path("testdata", "119083145_zpid.html").abspath)
     data = htmlparser.get_house_detail(html)
     ppt(data)
Exemplo n.º 7
0
 def test_get_house_detail():
     for p in Path("testdata").select_by_pattern_in_fname("zpid"):
         html = textfile.read(p.abspath)
         data = htmlparser.get_house_detail(html)
         ppt(data)
Exemplo n.º 8
0
 def test_get_items():
     for page, state, county, zipcode, street in testdata:
         filepath = Path("testdata", "%s.html" % page)
         data = htmlparser.get_items(textfile.read(filepath.abspath))
         ppt(data)