def __delete__(self, instance): """ Deletter of field (= """ if instance is not None: if self._field_name in instance: del instance[self._field_name] else: raise AttributeError("'%(type_name)s' object has no attribute '%(field_name)s'" % {'type_name': type_name(instance), 'field_name': self._field_name})
def __get__(self, instance, owner): """ Getter of field """ if instance is None: return self else: if self._field_name in instance: return instance[self._field_name] raise AttributeError("'%(type_name)s' object has no attribute '%(field_name)s'" % {'type_name': type_name(owner), 'field_name': self._field_name})
def __delete__(self, instance): """ Deletter of field (= """ if instance is not None: if self._field_name in instance: del instance[self._field_name] else: raise AttributeError( "'%(type_name)s' object has no attribute '%(field_name)s'" % { 'type_name': type_name(instance), 'field_name': self._field_name })
def __get__(self, instance, owner): """ Getter of field """ if instance is None: return self else: if self._field_name in instance: return instance[self._field_name] raise AttributeError( "'%(type_name)s' object has no attribute '%(field_name)s'" % { 'type_name': type_name(owner), 'field_name': self._field_name })
def __init__(self, field_type=None, field_name=None): """ Construct a new field :param field_type: type which field would store. If set to None it could store any type. :param field_name: name of field in mongo document. If set to None it would be the same as field name. Any two field in one model can't have the same `field_name`. """ if field_type is not None and not isinstance(field_type, basestring) and \ not check_mongo_type(field_type): raise TypeError("Invalid mongo type %(type)s" % {'type': type_name(field_type)}) self._field_type = field_type self._field_name = field_name
def __repr__(self): params = u" ".join((key + u"=" + repr(value) for key, value in self.viewitems())) return u"<%(class_name)s %(params)s>" % {"class_name": type_name(self), "params": params}
def test_type_name(self): self.assertEqual("unicode", utils.type_name(u"some_str")) self.assertEqual("unicode", utils.type_name(unicode)) self.assertEqual("object", utils.type_name(object())) self.assertEqual("object", utils.type_name(object))
def cast_to_class(cls, value): if not isinstance(value, (list, )): raise TypeError("Value should be list, not %(type)s" % {'type': type_name(value)}) item_generator = (cls.cast_inner_item(item) for item in value) return cls(item_generator)