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
 import pickle
2
 import pickle
3
 import typing
3
 import typing
4
 
4
 
5
+import collections
5
 import redis
6
 import redis
6
 
7
 
7
 from synergine2.exceptions import SynergineException
8
 from synergine2.exceptions import SynergineException
42
         self._data[key] = value
43
         self._data[key] = value
43
         self._modified_keys.add(key)
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
         if key not in self._data:
49
         if key not in self._data:
47
             b_value = self._r.get(key)
50
             b_value = self._r.get(key)
48
             if b_value is None:
51
             if b_value is None:
71
 
74
 
72
     def create(
75
     def create(
73
         self,
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
         indexes: typing.List[SharedDataIndex]=None,
79
         indexes: typing.List[SharedDataIndex]=None,
77
     ):
80
     ):
81
+        key = key_args
82
+        if not isinstance(key, str):
83
+            key = '_'.join(key_args)
78
         indexes = indexes or []
84
         indexes = indexes or []
79
 
85
 
80
         def get_key(obj):
86
         def get_key(obj):

+ 19 - 4
tests/test_share.py View File

12
         shared = SharedDataManager()
12
         shared = SharedDataManager()
13
 
13
 
14
         class Foo(object):
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
             counter = shared.create('counter', 0)
30
             counter = shared.create('counter', 0)
16
 
31
 
17
         foo = Foo()
32
         foo = Foo()
28
 
43
 
29
         class Foo(object):
44
         class Foo(object):
30
             counter = shared.create(
45
             counter = shared.create(
31
-                '{id}_counter',
32
-                0,
46
+                ['{id}', 'counter'],
47
+                value=0,
33
                 indexes=[],
48
                 indexes=[],
34
             )
49
             )
35
 
50
 
40
         foo = Foo()
55
         foo = Foo()
41
         foo.counter = 42
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
         foo.counter = 48
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
     def test_multiple_uses(self):
64
     def test_multiple_uses(self):
50
         shared = SharedDataManager()
65
         shared = SharedDataManager()