The “.”-Style Key Notation¶
To access nested item, e.g.,
config = ResConfig({"hosts": {"localhost": {"user": "John"}}})
you may use the “.”-style notation, so that
print(config["hosts.localhost.user"])
will print John
.
Similarly, the same style can be used when supplying a
dict
-like object to ResConfig
:
config = ResConfig({"hosts.localhost.user": "John"})
This will set exactly the same default configuration as the one above.
Note
Question: What if the key includes one of more “.” characters? It is quite common for us to see configuration with IP addresses like this:
config = ResConfig(
{"hosts": {"127.0.0.1": {"user": "John"}}}
)
Will this configuration be butchers into a mess like this?
{"hosts": {"127": {"0": {"0": {"1": {"user": "John"}}}}}}
Is this the end of the world?
Answer: Indeed, if you write the nested key like this,
config = ResConfig({"hosts.127.0.0.1.user": "John"})
the mess you mentioned will ensue. In order to avoid period to be interpreted as delimiter, you will need to escape them, e.g.,
config = ResConfig({"hosts.127\.0\.0\.1.user": "John"})
This way, they will not be interpret as nested keys:
{"hosts": {"127\.0\.0\.1": {"user": "John"}}}
Unfortunately, they need to be escaped all the time. For this reason, it is strongly recommended to use “.” only as the delimiter for nested keys, not as part of a key. While the end may not be near, this makes the world a bit messier place to be.