场景:从上一个问题(Organizing pythonic dictionaries for a JSON schema validation)开始,我现在试图以更有效的方式创建字典。我没有一步一步地将所有信息提供给字典,而是试图将其逐步传递。
我到目前为止所拥有的:
cldr["holidays"] = {"type": "object",
"description": "Holiday specification",
"properties": {
"default":{
"type": "object",
"description": "Calendars used",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
},
"exante": {
"type": "object",
"description": "Calendars used",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
},
"expost": {
"type": "object",
"description": "Calendars used",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
},
}
}
我要做什么:
cldr["holidays"] = {"type": "object",
"description": "Holiday specification",
"properties": {"default", "exante", "expost"}
}
cldr["holidays"]["properties"]["default"] = {
"type": "object",
"description": "",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
cldr["holidays"]["properties"]["exante"] = {
"type": "object",
"description": "",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
cldr["holidays"]["properties"]["expost"] = {
"type": "object",
"description": "",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
但是会产生以下错误:
TypeError: 'set' object does not support item assignment
问题1:关于我在做什么错的任何想法?
问题2::是否可以为该词典的内部部分创建共享类?由于它们本质上是相同的,我是否需要分别定义它们,或者是否有一种方法可以更有效地做到这一点?
答案 0 :(得分:1)
这有什么问题?
您错误地在属性键上创建了一个集合。
cldr["holidays"] = {"type": "object",
"description": "Holiday specification",
"properties": {"default", "exante", "expost"} # creates a set
}
cldr["holidays"]["properties"]["default"] = {
"type": "object",
"description": "",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
cldr["holidays"]["properties"]["exante"] = {
"type": "object",
"description": "",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
cldr["holidays"]["properties"]["expost"] = {
"type": "object",
"description": "",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
这是您纠正的方式
给属性键一个值,它是一个空字典。当您执行cldr["holidays"]["properties"]["default"] = {the inner dictionary}
时,将设置default
键及其值。
cldr["holidays"] = {"type": "object",
"description": "Holiday specification",
"properties": {} # creates an empty dictionary
}
cldr["holidays"]["properties"]["default"] = {
"type": "object",
"description": "",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
cldr["holidays"]["properties"]["exante"] = {
"type": "object",
"description": "",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
cldr["holidays"]["properties"]["expost"] = {
"type": "object",
"description": "",
"properties":{
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
听起来像您想要的
def holiday_property():
return {
"type": "object",
"description": "",
"properties": {
"ref": {"type": "string"},
"type": {"type": "string"},
"value": {"type": "string"}
}
}
cldr["holidays"] = {
"type": "object",
"description": "Holiday specification",
"properties": {}
}
cldr["holidays"]["properties"]["default"] = holiday_property()
cldr["holidays"]["properties"]["exante"] = holiday_property()
cldr["holidays"]["properties"]["expost"] = holiday_property()