DGMS89
DGMS89

Reputation: 1667

Nested dictionaries in Python

Scenario: From my previous question (Organizing pythonic dictionaries for a JSON schema validation), I am now trying to create my dictionaries in a more efficient manner. Instead of feeding all the information at once to the dictionary, I am trying to pass it step by step.

What I have so far:

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"}
                                            }
                                        },                                        
                                    } 
                   }

What I am trying to do:

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"}
                                        }
                                    }

But that yields the following error:

TypeError: 'set' object does not support item assignment

Question1: Any ideas on what I am doing wrong?

Question2: Would it be possible to create a shared class for the inner parts of this dictionaries? Since they would be inherently the same, do I need to define each of them separately or is there a way to do it more efficiently?

Upvotes: 0

Views: 68

Answers (1)

Onel Harrison
Onel Harrison

Reputation: 1334

What's wrong about this?

You're mistakenly creating a set at the properties key.

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"}
                                        }
                                    }

Here's how you correct that

Give the properties key a value that's an empty dictionary. When you do cldr["holidays"]["properties"]["default"] = {the inner dictionary}, you'll be setting the default key and its value..

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"}
                                        }
                                    }

What it sounds like you want

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()

Upvotes: 1

Related Questions