Skip to main content

tpljson

PyPI License: MIT PyPI - Python Version PyPI - Downloads
tests Coverage Status Docs

A Templating Json package that implements the official Python json interface.

tpljson works by referencing placeholder template strings into your json file... like {$.mykey}
These template values will be filled with other values that appear in the document.

Features

  • template json, referencing other fields in the current document via {$.key}
  • comments
  • colorized and context on errors
  • built-in json module compatible, all json errors raise json.JSONDecodeError and TypeError - identical to the built-in json module.

Installation

pip3 install tpljson

Usage Example 1

Define contact.json as

// this is another comment, multi-line comments are not supported.
{
  "first_name": "Peter", // first_name - comments can appear at the end of a line
  "last_name": "Henderson",
  "phone": "3338675309",
  "email": "{$.first_name}.{$.last_name}@example.org", // construct email address from 
  "contact": "{$.first_name} {$.last_name}\nCell: {$.phone}\nEmail: {$.email}"
}

Load contact.json using tpljson

import tpljson as json
d = json.load(open('contact.json'))
print(json.dumps(d, indent=4))

Output

{
    "first_name": "Peter",
    "last_name": "Henderson",
    "phone": "3338675309",
    "email": "Peter.Henderson@example.org",
    "contact": "Peter Henderson\nCell: 3338675309\nEmail: Peter.Henderson@example.org"
}

Usage Example 2

A more

Define permissions.json as:

{
  "user_id": "u381815",
  "cluster": "hive-cluster",
  "groups": ["finance", "accounting"],
  "description": "Permissions for user Nicholas",
  "create_role": [
    {"role": "{$.user_id}"}
  ],
  "grant_role": [
    {"group": "{$.user_id}", "role": "{$.user_id}"}
  ],
  "grant_privilege": [
    {"privilege": "select",  "object": "database",  "patterns": "{$.groups}", "role": "{$.user_id}"},
    {"privilege": "all",  "object": "database",  "name": "{$.user_id}", "role": "{$.user_id}"},
    {"privilege": "all",  "object": "uri",  "name": "'hdfs://{$.cluster}/user/{$.user_id}'", "role": "{$.user_id}"}
  ],
  "create_database": [
    {"name": "{$.user_id}", "owner": "{$.user_id}"}
  ],
  "sql": [
    "SHOW GRANT ROLE {$.user_id}",
    "SHOW ROLE GRANT GROUP {$.user_id}"
  ]
}

Load permissions.json using tpljson

import tpljson as json
d = json.load(open('permissions.json'))
print(json.dumps(d, indent=4))

Output:

{
    "user_id": "u381815",
    "cluster": "hive-cluster",
    "groups": [
        "finance",
        "accounting"
    ],
    "description": "Permissions for user Nicholas",
    "create_role": [
        {
            "role": "u381815"
        }
    ],
    "grant_role": [
        {
            "group": "u381815",
            "role": "u381815"
        }
    ],
    "grant_privilege": [
        {
            "privilege": "select",
            "object": "database",
            "patterns": [
                "finance",
                "accounting"
            ],
            "role": "u381815"
        },
        {
            "privilege": "all",
            "object": "database",
            "name": "u381815",
            "role": "u381815"
        },
        {
            "privilege": "all",
            "object": "uri",
            "name": "'hdfs://hive-cluster/user/u381815'",
            "role": "u381815"
        }
    ],
    "create_database": [
        {
            "name": "u381815",
            "owner": "u381815"
        }
    ],
    "sql": [
        "SHOW GRANT ROLE u381815",
        "SHOW ROLE GRANT GROUP u381815"
    ]
}

Implementation

  • Note that the order of values matters in the json, if one value depends on another.
  • commentjson is used internally to render comments, can be disabled via comments=false in load and loads.
  • errors are colorized by default via colorama

Metadata

Release files for tpljson 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tpljson 0.1.5
File Size Uploaded
tpljson-0.1.5.tar.gz 15.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tpljson 0.1.5
File Interpreter ABI Platform
tpljson-0.1.5-py3-none-any.whl Python 3 none any Details

Total release size: 26.1 kB

Release files / tpljson-0.1.5.tar.gz

Download URL tpljson-0.1.5.tar.gz
Size 15.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ad2353f79508c0aa5a2757a99d24b130fdfe4997cdff615dc4f91b4bb14b5a03
BLAKE2b-256 checksum
How to use checksums
72ab1b53284befc1fc6f3c27e268e12b18db0855fbf77b9d1a0d65f9fd78049d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

Release files / tpljson-0.1.5-py3-none-any.whl

Download URL tpljson-0.1.5-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
442b32d8131ddbb23256e08c75f14ed7bd95181b571a411f2f5698138df2445d
BLAKE2b-256 checksum
How to use checksums
54be43e725dc4f3d65221a9c55e536e68d4b67dc93241ef6bb821267ce2516cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

Release history Release notifications | RSS feed

This release

0.1.5 This release

2 release files

0.1.4

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page