[tor-commits] [sbws/master] new: state: Add optional default arg to get

juga at torproject.org juga at torproject.org
Thu Mar 21 18:30:42 UTC 2019


commit 0ad7e04fcbef48b49acab0845ca83509f9b481b2
Author: juga0 <juga at riseup.net>
Date:   Thu Mar 7 15:24:17 2019 +0000

    new: state: Add optional default arg to get
    
    Part of #28567.
---
 sbws/util/state.py | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/sbws/util/state.py b/sbws/util/state.py
index 58da699..1d33c3d 100644
--- a/sbws/util/state.py
+++ b/sbws/util/state.py
@@ -63,12 +63,16 @@ class State:
         self._state = self._read()
         return self._state.__len__()
 
-    def get(self, key):
+    def get(self, key, d=None):
+        """
+        Implements a dictionary ``get`` method reading and locking
+        a json file.
+        """
         if not isinstance(key, str):
             raise TypeError(
                 'Keys must be strings. %s is a %s' % (key, type(key)))
         self._state = self._read()
-        return self._state.get(key)
+        return self._state.get(key, d)
 
     def __getitem__(self, key):
         if not isinstance(key, str):
@@ -93,7 +97,6 @@ class State:
             raise TypeError(
                 'May only store value with type in %s, not %s' %
                 (State._ALLOWED_TYPES, type(value)))
-        self._state = self._read()
         self._state.__setitem__(key, value)
         self._write()
 





More information about the tor-commits mailing list