def set(self, target, value): """Set the value of this attribute for the passed object. """ if self.path is None: # There is no path defined on this resource. # We can do no magic to set the value. self.set = lambda *a: None return None if self._segments: # Attempt to resolve access to this attribute. self.get(target) if self._segments: # Attribute is not fully resolved; an interim segment is null. return # Resolve access to the parent object. # For a single-segment path this will effectively be a no-op. parent_getter = compose(*self._getters[:-1]) target = parent_getter(target) # Make the setter. func = self._make_setter(self.path.split('.')[-1], target.__class__) # Apply the setter now. func(target, value) # Replace this function with the constructed setter. def setter(target, value): func(parent_getter(target), value) self.set = setter
def get(self, target): """Retrieve the value of this attribute from the passed object. """ if self.path is None: # There is no path defined on this resource. # We can do no magic to get the value. self.get = lambda *a: None return None # Iterate and resolve each constructed getter. for func in self._getters: if target is None: # No more getters can be resolved. return None # Resolve this getter and continue iteration. target = func(target) # Are their segments remaining to make a getter for? while self._segments: if target is None: # No more getters can be made. return None # Fetch the next path segment. segment = self._segments.pop(0) # Make and append the corresponding getter. func = self._make_getter(segment, target.__class__) self._getters.append(func) # Resolve the getter. target = func(target) # Have we finished resolving the getters? if not self._segments: # Compose a replacement get function that just iterates over # the getters. self.get = compose(*self._getters) # Return our resolved value. return target
def _resolve_get(self, resolver, key, target): if self.path is None: # There is no path defined on this resource. # We can do no magic to get the value. resolver[key] = lambda *a: None return None # Iterate and resolve each constructed getter. for func in self._getters[key]: if target is None: # No more getters can be resolved. return None # Resolve this getter and continue iteration. target = func(target) # Are their segments remaining to make a getter for? while self._segments[key]: if target is None: # No more getters can be made. return None # Fetch the next path segment. segment = self._segments[key].pop(0) # Make and append the corresponding getter. func = self._make_getter(segment, key) self._getters[key].append(func) # Resolve the getter. target = func(target) # Have we finished resolving the getters? if not self._segments[key]: # Compose a replacement get function that just iterates over # the getters. resolver[key] = compose(*self._getters[key]) # Return our resolved value. return target
def set(self, target, value): """Set the value of this attribute for the passed object. """ if not self._set: return if self.path is None: # There is no path defined on this resource. # We can do no magic to set the value. self.set = lambda *a: None return None if self._segments[target.__class__]: # Attempt to resolve access to this attribute. self.get(target) if self._segments[target.__class__]: # Attribute is not fully resolved; an interim segment is null. return # Resolve access to the parent object. # For a single-segment path this will effectively be a no-op. parent_getter = compose(*self._getters[target.__class__][:-1]) target = parent_getter(target) # Make the setter. func = self._make_setter(self.path.split('.')[-1], target.__class__) # Apply the setter now. func(target, value) # Replace this function with the constructed setter. def setter(target, value): func(parent_getter(target), value) self.set = setter