Browse Source

permit usage of liste as key

Bastien Sevajol 7 years ago
parent
commit
d3dc1716bb
2 changed files with 28 additions and 7 deletions
  1. 9 3
      synergine2/share.py
  2. 19 4
      tests/test_share.py

+ 9 - 3
synergine2/share.py View File

@@ -2,6 +2,7 @@
2 2
 import pickle
3 3
 import typing
4 4
 
5
+import collections
5 6
 import redis
6 7
 
7 8
 from synergine2.exceptions import SynergineException
@@ -42,7 +43,9 @@ class SharedDataManager(object):
42 43
         self._data[key] = value
43 44
         self._modified_keys.add(key)
44 45
 
45
-    def get(self, key) -> typing.Any:
46
+    def get(self, *key_args: typing.Union[str, float, int]) -> typing.Any:
47
+        key = '_'.join([str(v) for v in key_args])
48
+
46 49
         if key not in self._data:
47 50
             b_value = self._r.get(key)
48 51
             if b_value is None:
@@ -71,10 +74,13 @@ class SharedDataManager(object):
71 74
 
72 75
     def create(
73 76
         self,
74
-        key: str,
75
-        value,
77
+        key_args: typing.Union[str, typing.List[typing.Union[str, int, float]]],
78
+        value: typing.Any,
76 79
         indexes: typing.List[SharedDataIndex]=None,
77 80
     ):
81
+        key = key_args
82
+        if not isinstance(key, str):
83
+            key = '_'.join(key_args)
78 84
         indexes = indexes or []
79 85
 
80 86
         def get_key(obj):

+ 19 - 4
tests/test_share.py View File

@@ -12,6 +12,21 @@ class TestShare(BaseTest):
12 12
         shared = SharedDataManager()
13 13
 
14 14
         class Foo(object):
15
+            counter = shared.create('counter', value=0)
16
+
17
+        foo = Foo()
18
+        foo.counter = 42
19
+
20
+        assert shared.get('counter') == 42
21
+
22
+        foo.counter = 48
23
+
24
+        assert shared.get('counter') == 48
25
+
26
+    def test_default_value(self):
27
+        shared = SharedDataManager()
28
+
29
+        class Foo(object):
15 30
             counter = shared.create('counter', 0)
16 31
 
17 32
         foo = Foo()
@@ -28,8 +43,8 @@ class TestShare(BaseTest):
28 43
 
29 44
         class Foo(object):
30 45
             counter = shared.create(
31
-                '{id}_counter',
32
-                0,
46
+                ['{id}', 'counter'],
47
+                value=0,
33 48
                 indexes=[],
34 49
             )
35 50
 
@@ -40,11 +55,11 @@ class TestShare(BaseTest):
40 55
         foo = Foo()
41 56
         foo.counter = 42
42 57
 
43
-        assert shared.get('{}_counter'.format(foo.id)) == 42
58
+        assert shared.get(foo.id, 'counter') == 42
44 59
 
45 60
         foo.counter = 48
46 61
 
47
-        assert shared.get('{}_counter'.format(foo.id)) == 48
62
+        assert shared.get(foo.id, 'counter') == 48
48 63
 
49 64
     def test_multiple_uses(self):
50 65
         shared = SharedDataManager()