Skip to main content

support json template for python developers

Project description

pyco-template

  • Fast
  • Simple
  • Easy To Use
  • Lightweight (no more requirements)
  • 尘归尘,土归土,配置归配置,逻辑归逻辑,业务归业务

install

  • pip install pyco-template

usage sample

from pyco_template.ext_json import CoTextTemplate
from pyco_template.ext_json import CoJsonTemplate


def test_text():
    ## NOTE: generally string_format should use `CoTextTemplate`    
    ## NOTE: 注意这种是不适合 CoJsonTemplate, 因为有"{}"
    text = """
    {
      "username": "{name}",
      "password": "{password}",
      "email": "{name}@address.com"
    }
    """
    tpl = CoTextTemplate(text)
    print(tpl)
    txt = tpl.render(password="***")
    print(100, txt)


def test_text2():
    ## NOTE: suggest to use string_template with ${}
    ## NOTE: 这种使用 ${} ,可以使用 CoJsonTemplate 
    text = """
    {
      "username": "${name}",
      "password": "${password}",
      "email": "${name}@address.com"
    }
    """

    tpl = CoJsonTemplate(text)
    txt = tpl.render(password="***")
    print(101, txt)


def test_list1():
    data2 = ["username=${name}", "password=${pwd}"]
    tpl2 = CoJsonTemplate.create(data2)
    txt2 = tpl2.render(pwd="***")
    print(201, txt2)


def test_list2():
    data1 = ["username={name}", "password={pwd}"]
    tpl1 = CoJsonTemplate.create(data1, _used_delimit=False)
    txt1 = tpl1.render(name="developer", ignore_info="this will be ignored!")
    print(202, txt1)


def test_dict1():
    data = dict(
        username="${name}",
        password="${password}",
        email="${name}@address.com",
    )
    tpl = CoJsonTemplate.create(data)
    txt = tpl.render(name="developer")
    print(301, txt)


def test_dict2():
    data = dict(
        username="{name}",
        password="{password}",
        email="{name}@address.com",
    )
    tpl = CoJsonTemplate.create(data, False)
    txt = tpl.render(name="developer")
    tpl.save_file(fout="test_dict.tmp.json")
    print(302, txt)


def test_dict3():
    data = {
        "name": "{topic_name}",
        "sensor": "{sensor_key}",
        "input": "{dataset_dir}",
        "device": "{sdir}",
        "calib_params": "",
        "protocol": {
            "name": "data_proto",
            "version": "{version}"
        },
        "type": "{topic_name}.{type}",
        "elem_count": 0,
        "elem_size": 0,
        "order": "up",
        "data": [
            "{sdir}/{sensor_key}.bin"
        ],
        "index": [
            "{sdir}/{sensor_key}.index.json"
        ]
    }

    tpl_json = CoJsonTemplate.create(data, _used_delimit=False)
    # print(tpl_json)
    kws1 = dict(topic_name="camera", version=2, extra_info="hello")
    txt1 = tpl_json.render(**kws1)
    print(303, txt1)
    tpl_json.save_file(fout="test_dict.tmp.json", **kws1)
    
    tpl2 = CoJsonTemplate.load_file(fout="test_dict.tmp.json")
    print(304, tpl2.render())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyco_template-0.0.1.tar.gz (6.2 kB view details)

Uploaded Source

File details

Details for the file pyco_template-0.0.1.tar.gz.

File metadata

  • Download URL: pyco_template-0.0.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.2

File hashes

Hashes for pyco_template-0.0.1.tar.gz
Algorithm Hash digest
SHA256 6ff537aeb9fd056eb200ccd3203c42ccc35fd2de78a9f21e60892a9fac792eac
MD5 885b9bde68495ae9e6b664dd35a458a3
BLAKE2b-256 84bba93eab9b0884e731f189d682ae6686ee5a362b12832622c1de97ebac62ba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page