added support for list, renamed 'sep' to 'delimiter', converted dict.iteritems() to dict.items()
@@ -5,10 +5,10 @@ Query a url and display the json response
Configuration parameters:
cache_timeout: how often we refresh this module in seconds
(default 30)
+ delimiter: the delimiter between parent and child objects
+ (default '_')
format: placeholders will be replaced by the returned json key names
(default '')
- sep: the separator between parent and child objects
- (default '_')
timeout: how long before deciding we're offline
(default 5)
url: specify a url to fetch json from
@@ -16,9 +16,14 @@ Configuration parameters:
Format placeholders:
Placeholders will be replaced by the json keys
- Placeholders for objects with sub-objects are flattened using 'sep' in between
+
+ Placeholders for objects with sub-objects are flattened using 'delimiter' in between
(eg. {'parent': {'child': 'value'}} will use placeholder {parent_child}
+ Placeholders for list elements have 'delimiter' followed by the index
+ (eg. {'parent': ['this', 'that']) will use placeholders {parent_0} for 'this' and
+ {parent_1} for 'that'
+
@author vicyap
"""
@@ -36,20 +41,27 @@ class Py3status:
"""
# available configuration parameters
cache_timeout = 30
+ delimiter = '_'
format = ''
- sep = '_'
timeout = 5
url = ''
def _flatten(self, d, parent_key=None):
- """
- modified from http://stackoverflow.com/a/6027615
+ """Flatten a dictionary.
+
+ Values that are dictionaries are flattened using self.delimiter in between
+ (eg. parent_child)
+ Values that are lists are flattened using self.delimiter followed by the index
+ (eg. parent_0)
"""
items = []
- for k, v in d.iteritems():
- new_key = parent_key + self.sep + k if parent_key else k
+ for k, v in d.items():
+ new_key = parent_key + self.delimiter + k if parent_key else k
if isinstance(v, collections.Mapping):
- items.extend(self._flatten(v, new_key).iteritems())
+ items.extend(self._flatten(v, new_key).items())
+ elif isinstance(v, list):
+ items.extend(
+ [(new_key + self.delimiter + str(i), str(x)) for i, x in enumerate(v)])
else:
items.append((new_key, v))
return dict(items)
