

def _db_set_value(self, v, p, value):
    if isinstance(value, str):
        v.set_stringvalue(value)
    elif isinstance(value, unicode):
        v.set_stringvalue(value.encode('utf8'))
        if (not self._indexed):
            p.set_meaning(entity_pb.Property.TEXT)
    elif isinstance(value, bool):
        v.set_booleanvalue(value)
    elif isinstance(value, (int, long)):
        if (not ((- _MAX_LONG) <= value < _MAX_LONG)):
            raise TypeError(('Property %s can only accept 64-bit integers; received %s' % (self._name, value)))
        v.set_int64value(value)
    elif isinstance(value, float):
        v.set_doublevalue(value)
    elif isinstance(value, Key):
        ref = value.reference()
        rv = v.mutable_referencevalue()
        rv.set_app(ref.app())
        if ref.has_name_space():
            rv.set_name_space(ref.name_space())
        for elem in ref.path().element_list():
            rv.add_pathelement().CopyFrom(elem)
    elif isinstance(value, datetime.datetime):
        if (value.tzinfo is not None):
            raise NotImplementedError(('Property %s can only support the UTC. Please derive a new Property to support alternative timezones.' % self._name))
        dt = (value - _EPOCH)
        ival = (dt.microseconds + (1000000 * (dt.seconds + ((24 * 3600) * dt.days))))
        v.set_int64value(ival)
        p.set_meaning(entity_pb.Property.GD_WHEN)
    elif isinstance(value, GeoPt):
        p.set_meaning(entity_pb.Property.GEORSS_POINT)
        pv = v.mutable_pointvalue()
        pv.set_x(value.lat)
        pv.set_y(value.lon)
    elif isinstance(value, users.User):
        datastore_types.PackUser(p.name(), value, v)
    elif isinstance(value, BlobKey):
        v.set_stringvalue(str(value))
        p.set_meaning(entity_pb.Property.BLOBKEY)
    elif isinstance(value, Model):
        set_key = (value._key is not None)
        pb = value._to_pb(set_key=set_key)
        value = pb.SerializePartialToString()
        v.set_stringvalue(value)
        p.set_meaning(entity_pb.Property.ENTITY_PROTO)
    elif isinstance(value, _CompressedValue):
        value = value.z_val
        v.set_stringvalue(value)
        p.set_meaning_uri(_MEANING_URI_COMPRESSED)
        p.set_meaning(entity_pb.Property.BLOB)
    else:
        raise NotImplementedError(('Property %s does not support %s types.' % (self._name, type(value))))

dataset/ETHPy150Open GoogleCloudPlatform/datastore-ndb-python/ndb/model.py GenericProperty._db_set_value/original