From c7a873ba71d1103b66c1cdf93451cb03ddf19832 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 08:58:26 -0500 Subject: Add information on indexing into ConfigFile --- README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/README.md b/README.md index 96a27af..c1d4e9a 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,14 @@ config.get('section') >>> {'num_key': '5', 'str_key': 'blah', 'bool_key': 'true', 'list_key': '[1, 2]'} ``` +Furthermore, you can also index into the `ConfigFile` object +to retrieve keys if that is preferred. + +```python +config['section']['num_key'] +>>> '5' +``` + #### Coercing the return types However, some of these keys are obviously not strings natively. @@ -181,6 +189,20 @@ dependent on the file format we're using, since every format is a little different in it's structure and in what it supports. See the [full documentation](#Documentation) for more information there. +Lastly, we can set values using an array notation as well. The underlying +content is all manipulated as a dictionary for every file type. If we wanted +to create a new section, we'd simply set it to be an empty dictionary. + +```python +config['new_section'] = {} +``` + +Which would result to be an empty section: + +```ini +[new_section] +``` + ### Using `delete()` `delete()` allows us to delete entire sections or specific keys. @@ -196,6 +218,12 @@ However, we can also just delete a single key. config.delete('section.num_key') ``` +We can also use the array notation here as well. + +```python +del config['section']['num_key'] +``` + ### Using `has()` `has()` allows us to check whether a given key exists in our file. There -- cgit v1.2.3