99
1010
1111class 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."""
1215
1316 def __init__ (self , store , key = '.zattrs' , read_only = False , cache = True ,
1417 synchronizer = None ):
@@ -19,7 +22,7 @@ def __init__(self, store, key='.zattrs', read_only=False, cache=True,
1922 self ._cached_asdict = None
2023 self .synchronizer = synchronizer
2124
22- def _get (self ):
25+ def _get_nosync (self ):
2326 try :
2427 data = self .store [self .key ]
2528 except KeyError :
@@ -28,16 +31,11 @@ def _get(self):
2831 d = json .loads (text_type (data , 'ascii' ))
2932 return d
3033
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-
3734 def asdict (self ):
35+ """Retrieve all attributes as a dictionary."""
3836 if self .cache and self ._cached_asdict is not None :
3937 return self ._cached_asdict
40- d = self ._get ()
38+ d = self ._get_nosync ()
4139 if self .cache :
4240 self ._cached_asdict = d
4341 return d
@@ -67,43 +65,57 @@ def __setitem__(self, item, value):
6765 def _setitem_nosync (self , item , value ):
6866
6967 # load existing data
70- d = self ._get ()
68+ d = self ._get_nosync ()
7169
7270 # set key value
7371 d [item ] = value
7472
7573 # _put modified data
76- self ._put (d )
74+ self ._put_nosync (d )
7775
7876 def __delitem__ (self , item ):
7977 self ._write_op (self ._delitem_nosync , item )
8078
8179 def _delitem_nosync (self , key ):
8280
8381 # load existing data
84- d = self ._get ()
82+ d = self ._get_nosync ()
8583
8684 # delete key value
8785 del d [key ]
8886
8987 # _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
91100
92101 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."""
94103 self ._write_op (self ._update_nosync , * args , ** kwargs )
95104
96105 def _update_nosync (self , * args , ** kwargs ):
97106
98107 # load existing data
99- d = self ._get ()
108+ d = self ._get_nosync ()
100109
101110 # update
102111 d .update (* args , ** kwargs )
103112
104113 # _put modified data
105- self ._put (d )
114+ self ._put_nosync (d )
106115
116+ def keys (self ):
117+ return self .asdict ().keys ()
118+
107119 def __iter__ (self ):
108120 return iter (self .asdict ())
109121
0 commit comments