9
9
10
10
11
11
class Attributes (MutableMapping ):
12
+ """Class providing access to user attributes on an array or group. Should not be
13
+ instantiated directly, will be available via the `.attrs` property of an array or
14
+ group."""
12
15
13
16
def __init__ (self , store , key = '.zattrs' , read_only = False , cache = True ,
14
17
synchronizer = None ):
@@ -19,7 +22,7 @@ def __init__(self, store, key='.zattrs', read_only=False, cache=True,
19
22
self ._cached_asdict = None
20
23
self .synchronizer = synchronizer
21
24
22
- def _get (self ):
25
+ def _get_nosync (self ):
23
26
try :
24
27
data = self .store [self .key ]
25
28
except KeyError :
@@ -28,16 +31,11 @@ def _get(self):
28
31
d = json .loads (text_type (data , 'ascii' ))
29
32
return d
30
33
31
- def _put (self , d ):
32
- s = json .dumps (d , indent = 4 , sort_keys = True , ensure_ascii = True , separators = (',' , ': ' ))
33
- self .store [self .key ] = s .encode ('ascii' )
34
- if self .cache :
35
- self ._cached_asdict = d
36
-
37
34
def asdict (self ):
35
+ """Retrieve all attributes as a dictionary."""
38
36
if self .cache and self ._cached_asdict is not None :
39
37
return self ._cached_asdict
40
- d = self ._get ()
38
+ d = self ._get_nosync ()
41
39
if self .cache :
42
40
self ._cached_asdict = d
43
41
return d
@@ -67,43 +65,57 @@ def __setitem__(self, item, value):
67
65
def _setitem_nosync (self , item , value ):
68
66
69
67
# load existing data
70
- d = self ._get ()
68
+ d = self ._get_nosync ()
71
69
72
70
# set key value
73
71
d [item ] = value
74
72
75
73
# _put modified data
76
- self ._put (d )
74
+ self ._put_nosync (d )
77
75
78
76
def __delitem__ (self , item ):
79
77
self ._write_op (self ._delitem_nosync , item )
80
78
81
79
def _delitem_nosync (self , key ):
82
80
83
81
# load existing data
84
- d = self ._get ()
82
+ d = self ._get_nosync ()
85
83
86
84
# delete key value
87
85
del d [key ]
88
86
89
87
# _put modified data
90
- self ._put (d )
88
+ self ._put_nosync (d )
89
+
90
+ def put (self , d ):
91
+ """Overwrite all attributes with the key/value pairs in the provided dictionary
92
+ `d` in a single operation."""
93
+ self ._write_op (self ._put_nosync , d )
94
+
95
+ def _put_nosync (self , d ):
96
+ s = json .dumps (d , indent = 4 , sort_keys = True , ensure_ascii = True , separators = (',' , ': ' ))
97
+ self .store [self .key ] = s .encode ('ascii' )
98
+ if self .cache :
99
+ self ._cached_asdict = d
91
100
92
101
def update (self , * args , ** kwargs ):
93
- # override to provide update in a single write
102
+ """Update the values of several attributes in a single operation."""
94
103
self ._write_op (self ._update_nosync , * args , ** kwargs )
95
104
96
105
def _update_nosync (self , * args , ** kwargs ):
97
106
98
107
# load existing data
99
- d = self ._get ()
108
+ d = self ._get_nosync ()
100
109
101
110
# update
102
111
d .update (* args , ** kwargs )
103
112
104
113
# _put modified data
105
- self ._put (d )
114
+ self ._put_nosync (d )
106
115
116
+ def keys (self ):
117
+ return self .asdict ().keys ()
118
+
107
119
def __iter__ (self ):
108
120
return iter (self .asdict ())
109
121
0 commit comments