質問

I'm writing a Google Appengine app in Python. The app does receive and send data through google cloud endpoint messages. Below you'll find an example of the database model definition. Object contains all the objects, ObjectBoolean just the boolean ones and ObjectInteger just the integer ones. All the classes do have got two methods defined: message_to_model() and model_to_message().

How can I re-use the methods in the sub-classes? I do want to get rid of repeated code like:

        self.name = request.name
        self.description = request.description
        self.created = request.created
        self.modified = request.modified

Those fields are part of the Object-class. But in my code the fields are also listed in the sub-classes, which is what I do want to avoid. How can I achieve this? I did already check the super() function, but I think this is not suitable in my case because I do not want to execute the method I do want to extend it.

Cheers

-Luca.

from google.appengine.ext.ndb import polymodel
from google.appengine.ext import ndb
from endpoint_messages.object import *


class Object(polymodel.PolyModel):
    name = ndb.StringProperty(required=True, verbose_name="Name")
    description = ndb.TextProperty(verbose_name="Description")
    created = ndb.DateTimeProperty(auto_now_add=True, verbose_name="Created")
    modified = ndb.DateTimeProperty(auto_now=True, verbose_name="Modified")

    def model_to_message(self):
        return ObjectResponseMessage(class_name=self._class_name(),
                                     id=self.key.integer_id(),
                                     name=self.name,
                                     description=self.description,
                                     created=self.created,
                                     modified=self.modified)

    def message_to_model(self, request):
        self.name = request.name
        self.description = request.description
        self.created = request.created
        self.modified = request.modified


class ObjectBoolean(Object):
    current = ndb.BooleanProperty(verbose_name="Current")
    target = ndb.BooleanProperty(verbose_name="Target")

    def model_to_message(self):
        return ObjectResponseMessage(class_name=self._class_name(),
                                     id=self.key.integer_id(),
                                     name=self.name,
                                     description=self.description,
                                     created=self.created,
                                     modified=self.modified,
                                     boolean=ObjectBooleanMessage(current=self.current,
                                                                  target=self.target))

    def message_to_model(self, request):
        self.name = request.name
        self.description = request.description
        self.created = request.created
        self.modified = request.modified
        if request.boolean:
            self.current = request.boolean.success
            self.target = request.boolean.target


class ObjectInteger(Object):
    current = ndb.IntegerProperty(verbose_name="Current")
    target = ndb.IntegerProperty(verbose_name="Target")

    def model_to_message(self):
        return ObjectResponseMessage(class_name=self._class_name(),
                                     id=self.key.integer_id(),
                                     name=self.name,
                                     description=self.description,
                                     created=self.created,
                                     modified=self.modified,
                                     float=ObjectIntegerMessage(current=self.current,
                                                                target=self.target))

    def message_to_model(self, request):
        self.name = request.name
        self.description = request.description
        self.created = request.created
        self.modified = request.modified
        if request.integer:
            self.current = request.integer.current
            self.target = request.integer.target
役に立ちましたか?

解決

super() can be used for this. You could use something like this in ObjectBoolean:

def message_to_model(self, request):
    super(ObjectBoolean, self).message_to_model(request)

    if request.boolean:
        self.current = request.boolean.success
        self.target = request.boolean.target

For this to work, Object must be a new style class, which it is if polymodel.PolyModel is a new style class.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top