def dtype(self, new_type): # check if this is a valid type if not types.valid_type(new_type): raise AttributeError("'%s' is not a valid type." % new_type) # we convert the value if possible old_type = self._dtype old_value = types.set(self._value, self._dtype, self._encoder) try: new_value = types.get(old_value, new_type, self._encoder) except: # cannot convert, try the other way around try: old_value = types.set(self._value, new_type, self._encoder) new_value = types.get(old_value, new_type, self._encoder) except: #doesn't work either, therefore refuse raise ValueError("cannot convert '%s' from '%s' to '%s'" % (self.value, old_type, new_type)) self._value = new_value self._dtype = new_type
def value(self): """ used to access typed data of the value as a string. Use data to access the raw type, i.e.: >>> v = Value(1, type="float") >>> v.data 1.0 >>> v.data = 1.5 >>> v.value "1.5" >>> v.value = 2 >>> v.data 2.0 """ return types.set(self._value, self._dtype, self._encoder)
def date(self): """ The date the document was created. """ return types.set(self._date, "date")