[sbws/maint-1.1] chg: state: Add method to count list values

commit 9acad379acffa9c167529e7e12a2c445208cd60d Author: juga0 <juga@riseup.net> Date: Sat Mar 14 18:24:50 2020 +0000 chg: state: Add method to count list values --- sbws/util/state.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/sbws/util/state.py b/sbws/util/state.py index 86cc59b..50e6a66 100644 --- a/sbws/util/state.py +++ b/sbws/util/state.py @@ -90,3 +90,18 @@ class State: def __contains__(self, item): self._state = self._read() return self._state.__contains__(item) + + def count(self, k): + """ + Returns the length if the key value is a list + or the sum of number if the key value is a list of list + or the key value + or None if the state doesn't have the key. + """ + if self.get(k): + if isinstance(self._state[k], list): + if isinstance(self._state[k][0], list): + return sum(map(lambda x: x[1], self._state[k])) + return len(self._state[k]) + return self.get(k) + return None
participants (1)
-
juga@torproject.org