我有一个嵌套的dict,它被解析成一个列表。
以下是工作代码:code in ideone 但缺少几条牙箍。
预期结果应为:
[['>=', 'qty', '3'], 'AND', ['in', 'category_ids', '240']]
我在哪里
[['>=', 'qty', '3', 'AND', ['in', 'category_ids', '240']]]
def aggregatorConvert(aggregator):
if aggregator == "any":
return "OR"
else:
return "AND"
def operatorSymbol(operator):
if operator == "{}":
return "in"
elif operator == "!{}":
return "not in"
else:
return operator
def expression_tuple(query):
if query.get("attribute"):
exp = []
exp.append(operatorSymbol(query.get("operator")))
exp.append(query.get("attribute"))
exp.append(query.get("value"))
return exp
else:
return []
def format_serialized(query, counter):
counter = counter or 0
ex = expression_tuple(query)
conditions = query.get("conditions")
if conditions:
for i in query.get("conditions"):
if ex:
ex.append(aggregatorConvert(query.get("aggregator")))
ex.append(format_serialized(query.get("conditions")[i], counter + 1))
print ex
return ex
query = {'type': 'salesrule/rule_condition_combine', 'aggregator': 'all', 'operator': None, 'attribute': None, 'conditions': {0: {'type': 'salesrule/rule_condition_product_subselect', 'aggregator': 'all', 'operator': '>=', 'attribute': 'qty', 'conditions': {0: {'operator': '{}', 'attribute': 'category_ids', 'type': 'salesrule/rule_condition_product', 'is_value_processed': False, 'value': '240'}}, 'is_value_processed': None, 'value': '3'}}, 'is_value_processed': None, 'value': '1'}
format_serialized(query, 0)
有人能告诉我哪里出错了吗?
答案 0 :(得分:0)
将查询作业替换为:
query = {'type': 'salesrule/rule_condition_combine', 'aggregator': 'all', 'operator': None, 'attribute': None,
'conditions': {0: {'type': 'salesrule/rule_condition_product_subselect', 'aggregator': 'all', 'operator': '>=', 'attribute': 'qty', 'is_value_processed': None, 'value': '3'},
1: {'operator': '{}', 'attribute': 'category_ids', 'type': 'salesrule/rule_condition_product', 'is_value_processed': False, 'value': '240'}
}, 'is_value_processed': None, 'value': '1'}
完整示例here
答案 1 :(得分:0)
您的查询是嵌套的,我认为您获得的结果将是正确的结果,从而坚持嵌套。但是,如果要将其展平并删除嵌套,可以使用尾递归来生成结果。
这是你的查询非常印刷:
query = {
'type': 'salesrule/rule_condition_combine',
'aggregator': 'all',
'operator': None,
'attribute': None,
'conditions': {
0: {
'type': 'salesrule/rule_condition_product_subselect',
'aggregator': 'all',
'operator': '>=',
'attribute': 'qty',
'conditions': {
0: {
'operator': '{}',
'attribute': 'category_ids',
'type': 'salesrule/rule_condition_product',
'is_value_processed': False,
'value': '240'
}
},
'is_value_processed': None,
'value': '3'
}
},
'is_value_processed': None,
'value': '1'
}
如果您构建结果如下:
import copy
def format_serialized(query, counter, lst):
counter = counter or 0
ex = expression_tuple(query)
if ex:
lst.append(copy.copy(ex)) # you need to copy the list since it is mutable
conditions = query.get("conditions")
if conditions:
for i in query.get("conditions"):
if ex:
lst.append(aggregatorConvert(query.get("aggregator")))
format_serialized(query.get("conditions")[i], counter + 1, lst)
return lst
并称之为:
print format_serialized(query, 0, [])
你会得到:
[['>=', 'qty', '3'], 'AND', ['in', 'category_ids', '240']]
预期结果如何。实现的不同之处在于,您将列表传递到递归函数中,然后在下载的过程中构建它。而不是“在上升的路上”。
答案 2 :(得分:0)
只需在ex=[ex]
之前添加ex.append(aggregatorConvert(query.get("aggregator")))
:
if ex:
ex=[ex] # add this line
ex.append(aggregatorConvert(query.get("aggregator")))
ex.append(format_serialized(query.get("conditions")[i], counter + 1)
这将解决问题。