T
Module can be used for flexibility https://pypi.org/project/dpath/ :from copy import deepcopy
import dpath.util as DU # pip install dpath
template = {
"action": {
"type": "text",
"label": "???"
},
"color": "positive"
}
def gen_entry(val, template=template, path="/action/label"):
d = deepcopy(template)
DU.set(d, path, val)
return d
keys = list(CLASSES.keys())
bucket_size = 4
buttons = [[gen_entry(key, template=template, path="/action/label")
for key in keys[i:i+bucket_size]]
for i in range(0, len(keys), bucket_size)]
keyboard_classes = dict(
one_time=False,
buttons=buttons
)
result:In [51]: print(json.dumps(keyboard_classes, indent=2))
{
"one_time": false,
"buttons": [
[
{
"action": {
"type": "text",
"label": "1"
},
"color": "positive"
},
{
"action": {
"type": "text",
"label": "2"
},
"color": "positive"
},
{
"action": {
"type": "text",
"label": "3"
},
"color": "positive"
},
{
"action": {
"type": "text",
"label": "4"
},
"color": "positive"
}
],
[
{
"action": {
"type": "text",
"label": "5"
},
"color": "positive"
},
{
"action": {
"type": "text",
"label": "6"
},
"color": "positive"
},
{
"action": {
"type": "text",
"label": "7"
},
"color": "positive"
},
{
"action": {
"type": "text",
"label": "8"
},
"color": "positive"
}
],
[
{
"action": {
"type": "text",
"label": "9"
},
"color": "positive"
},
{
"action": {
"type": "text",
"label": "10"
},
"color": "positive"
},
{
"action": {
"type": "text",
"label": "11"
},
"color": "positive"
}
]
]
}