shimajima-eiji
10/15/2018 - 3:27 AM

toml: [https://nomuraya.work/techzine/0202, https://shimajima-eiji.github.io/gist/python_toml/]

test = "TOML"

[section]
key = "value_section"  # セクションのバリュー
list = ["value1", "value2"]  # セクションのバリュー
#dict.key = ["value", inner_dict = "inner_dict"]
dict.key = "value"
dict.inner_dict = "inner_dict"

### yamlだとこう
# dict:
#   key: "value"
#   inner: "value"

[sec]
key = "value_sec"  # セクションのバリュー
list = ["value1", "value2"]  # セクションのバリュー
#dict.key = ["value", inner_dict = "inner_dict"]
dict.key = "value"
dict.inner_dict = "inner_dict"
# need python
pip install toml
pip install pathlib  # option
piip install python-box  # option

python exec_toml.py
# {'test': 'TOML', 'section': {'key': 'value_section', 'list': ['value1', 'value2'], 'dict': {'key': 'value', 'inner_dict': 'inner_dict'}}, 'sec': {'key': 'value_sec', 'list': ['value1', 'value2'], 'dict': {'key': 'value', 'inner_dict': 'inner_dict'}}}

# ver2
python exec_toml.py | jq
: <<PRINT
{
  "key": "value_sec",
  "list": [
    "value1",
    "value2"
  ],
  "dict": {
    "key": "value",
    "inner_dict": "inner_dict"
  }
}
PRINT

python_toml

exec_toml.py

view latest

import toml
from pathlib import Path
from box import Box

gettoml = Box(toml.loads(Path("test.toml").open().read())))
print(gettoml)

# jqコマンドに渡したい場合、json.dumpsで' -> "に変換する
# ver2
import json
print(json.dumps(gettoml.section))

setup.sh

view latest

# need python
pip install toml
pip install pathlib  # option
piip install python-box  # option

python exec_toml.py
# {'test': 'TOML', 'section': {'key': 'value_section', 'list': ['value1', 'value2'], 'dict': {'key': 'value', 'inner_dict': 'inner_dict'}}, 'sec': {'key': 'value_sec', 'list': ['value1', 'value2'], 'dict': {'key': 'value', 'inner_dict': 'inner_dict'}}}

# ver2
python exec_toml.py | jq
: <<PRINT
{
  "key": "value_sec",
  "list": [
    "value1",
    "value2"
  ],
  "dict": {
    "key": "value",
    "inner_dict": "inner_dict"
  }
}
PRINT

test.toml

view latest

test = "TOML"

[section]
key = "value_section"  # セクションのバリュー
list = ["value1", "value2"]  # セクションのバリュー
#dict.key = ["value", inner_dict = "inner_dict"]
dict.key = "value"
dict.inner_dict = "inner_dict"

### yamlだとこう
# dict:
#   key: "value"
#   inner: "value"

[sec]
key = "value_sec"  # セクションのバリュー
list = ["value1", "value2"]  # セクションのバリュー
#dict.key = ["value", inner_dict = "inner_dict"]
dict.key = "value"
dict.inner_dict = "inner_dict"

back

import toml
from pathlib import Path
from box import Box

gettoml = Box(toml.loads(Path("test.toml").open().read())))
print(gettoml)

# jqコマンドに渡したい場合、json.dumpsで' -> "に変換する
# ver2
import json
print(json.dumps(gettoml.section))