def coerce(self, value, context=None): if isinstance(value, str): value = value.split(self._separator) if isinstance(value, dict): value = value.values() if not isinstance(value, Iterable): raise AttributeException("Unable to value convert to list") parent = context.parent if context is not None else None return tuple([ t.coerce(x, CoerceContext(parent=parent, key=i)) for i, (x, t) in enumerate(zip(value, self._types)) ])
def coerce(self, value, context=None): if not isinstance(value, Mapping): raise AttributeException("Unable to value convert to dict") container = self._container() for name, data in value.items(): ctx = CoerceContext( parent=context.parent if context is not None else None, key=name) container[name] = self._contains.coerce(data, ctx) if self.readonly: return ReadonlyMapping(container) else: return CoerceMapping(container, self, self._contains)
def coerce(self, value, context=None): if isinstance(value, str): value = [v.strip() for v in value.split(self._separator)] if isinstance(value, dict): value = value.values() if not isinstance(value, Iterable): raise AttributeException("Unable to convert value to list") parent = context.parent if context is not None else None data = [ self._contains.coerce(x, CoerceContext(parent=parent, key=i)) for i, x in enumerate(value) ] if self.readonly: return ReadonlySequence(data) else: return CoerceSequence(data, self, self._contains)
def __setitem__(self, key, value): ctx = CoerceContext(parent=self._parent, key=key) self._data[key] = self._type.coerce(value, ctx)
def __setitem__(self, index, value): ctx = CoerceContext(parent=self._parent, key=index) self._data[index] = self._type.coerce(value, ctx)
def insert(self, index, value): ctx = CoerceContext(parent=self._parent, key=index) self._data.insert(index, self._type.coerce(value, ctx))
def append(self, item): ctx = CoerceContext(parent=self._parent, key=len(self._data)) self._data.append(self._type.coerce(item, ctx))