GitLab is used only for code review, issue tracking and project management. Canonical locations for source code are still https://gitweb.torproject.org/ https://git.torproject.org/ and git-rw.torproject.org.

Commit 9acad379 authored by juga  's avatar juga 💬

chg: state: Add method to count list values

parent 26409a3d
......@@ -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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment