Skip to content

Instantly share code, notes, and snippets.

@shimajima-eiji
Last active October 15, 2018 08:52
Show Gist options
  • Save shimajima-eiji/cd5056b37c538132cd532bfdadb9a186 to your computer and use it in GitHub Desktop.
Save shimajima-eiji/cd5056b37c538132cd532bfdadb9a186 to your computer and use it in GitHub Desktop.
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))

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

# 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"
[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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment