@@ -91,7 +91,7 @@ def info(self):
9191 endpoint = '/' .join (['db' , self .__id_safe ])
9292 return self .__client ._call ('get' , endpoint )
9393
94- def get (self , item , cache = None ):
94+ def get (self , item , cache = None , unpack = False ):
9595 if cache is None : cache = self .__use_cache
9696 item = str (item )
9797 if cache and item in self .__cache :
@@ -102,8 +102,9 @@ def get(self, item, cache=None):
102102 if cache : self .__cache [item ] = result
103103 if isinstance (result , Hashable ): return deepcopy (result )
104104 if isinstance (result , Iterable ): return deepcopy (result )
105- #if isinstance(result, Iterable): return deepcopy(next(result, {}))
106- #if isinstance(result, list): return deepcopy(next(iter(result), {}))
105+ if unpack :
106+ if isinstance (result , Iterable ): return deepcopy (next (result , {}))
107+ if isinstance (result , list ): return deepcopy (next (iter (result ), {}))
107108 return result
108109
109110 def get_raw (self , item ):
@@ -113,13 +114,13 @@ def get_raw(self, item):
113114 def put (self , item , cache = None ):
114115 if self .__enforce_caps and not self .putable :
115116 raise CapabilityError ('Db {} does not have put capability' .format (self .__dbname ))
117+ if self .indexed and (not hasattr (item , self .__index_by )) and self .__enforce_indexby :
118+ raise MissingIndexError ("The provided document doesn't contain field '{}'" .format (self .__index_by ))
119+
116120 if cache is None : cache = self .__use_cache
117121 if cache :
118- if self .indexed :
119- if hasattr (item , self .__index_by ):
122+ if self .indexed and hasattr (item , self .__index_by ):
120123 index_val = getattr (item , self .__index_by )
121- elif self .__enforce_indexby :
122- raise MissingIndexError ("The provided document doesn't contain field '{}'" .format (self .__index_by ))
123124 else :
124125 index_val = item .get ('key' )
125126 if index_val :
@@ -140,13 +141,13 @@ def add(self, item, cache=None):
140141
141142 def iterator_raw (self , ** kwargs ):
142143 if self .__enforce_caps and not self .iterable :
143- raise CapabilityError ('Db {} does not have remove capability' .format (self .__dbname ))
144+ raise CapabilityError ('Db {} does not have iterator capability' .format (self .__dbname ))
144145 endpoint = '/' .join (['db' , self .__id_safe , 'rawiterator' ])
145146 return self .__client ._call ('get' , endpoint , kwargs )
146147
147148 def iterator (self , ** kwargs ):
148149 if self .__enforce_caps and not self .iterable :
149- raise CapabilityError ('Db {} does not have remove capability' .format (self .__dbname ))
150+ raise CapabilityError ('Db {} does not have iterator capability' .format (self .__dbname ))
150151 endpoint = '/' .join (['db' , self .__id_safe , 'iterator' ])
151152 return self .__client ._call ('get' , endpoint , kwargs )
152153
0 commit comments