Научиться пользоваться отложенной библиотекой - PullRequest
2 голосов
/ 16 ноября 2011

Я пытаюсь код, почти идентичный примеру из руководства, чтобы включить счетчик загрузок, но я получаю исключение:

Файл "/media/Lexar/montao/wwwblob/handler.py", строка 117, в FileInfo download_count = db.IntegerProperty (обязательно = True, count = 0) TypeError: init () получил неожиданный аргумент ключевого слова 'count'

Вот код, который я пытаюсь запустить:

from google.appengine.ext import deferred
from google.appengine.runtime import DeadlineExceededError


    class Mapper(object):

    # Subclasses should replace this with a model class (eg, model.Person).

    KIND = None

    # Subclasses can replace this with a list of (property, value) tuples to filter by.

    FILTERS = []

    def __init__(self):
        self.to_put = []
        self.to_delete = []

    def map(self, entity):
        """Updates a single entity.

        Implementers should return a tuple containing two iterables (to_update, to_delete).
        """

        return ([], [])

    def finish(self):
        """Called when the mapper has finished, to allow for any final work to be done."""

        pass

    def get_query(self):
        """Returns a query over the specified kind, with any appropriate filters applied."""

        q = self.KIND.all()
        for (prop, value) in self.FILTERS:
            q.filter('%s =' % prop, value)
        q.order('__key__')
        return q

    def run(self, batch_size=100):
        """Starts the mapper running."""

        self._continue(None, batch_size)

    def _batch_write(self):
        """Writes updates and deletes entities in a batch."""

        if self.to_put:
            db.put(self.to_put)
            self.to_put = []
        if self.to_delete:
            db.delete(self.to_delete)
            self.to_delete = []

    def _continue(self, start_key, batch_size):
        q = self.get_query()

        # If we're resuming, pick up where we left off last time.

        if start_key:
            q.filter('__key__ >', start_key)

        # Keep updating records until we run out of time.

        try:

            # Steps over the results, returning each entity and its index.

            for (i, entity) in enumerate(q):
                (map_updates, map_deletes) = self.map(entity)
                self.to_put.extend(map_updates)
                self.to_delete.extend(map_deletes)

                # Do updates and deletes in batches.

                if (i + 1) % batch_size == 0:
                    self._batch_write()

                # Record the last entity we processed.

                start_key = entity.key()
            self._batch_write()
        except DeadlineExceededError:

            # Write any unfinished updates to the datastore.

            self._batch_write()

            # Queue a new task to pick up where we left off.

            deferred.defer(self._continue, start_key, batch_size)
            return
        self.finish()

class FileInfo(db.Model):

    blob = blobstore.BlobReferenceProperty(required=True)
    download_count = db.IntegerProperty(required=True, count=0)
    uploaded_by = db.UserProperty(required=True)
    uploaded_at = db.DateTimeProperty(required=True, auto_now_add=True)


    class DailyTotal(db.Model):

        date = db.DateProperty(required=True, auto_now_add=True)
        file_count = db.IntegerProperty(required=True)
        download_count = db.IntegerProperty(required=True)


    class DownloadCountMapper(Mapper):

        KIND = FileInfo

        def __init__(self):
            self.file_count = 0
            self.download_count = 0

        def map(self, file):
            self.file_count += 1
            self.download_count += file.download_count

        def finish(self):
            total = DailyTotal(file_count=self.file_count,
                               download_count=self.download_count)
            total.put()

Можете ли вы сказать мне, что я должен делать?

Спасибо

1 Ответ

3 голосов
/ 16 ноября 2011

Виновна эта строка:

download_count = db.IntegerProperty(required=True, count=0)

Конструктор IntegerProperty не знает, что делать с count.Может быть, вы имели в виду это:

download_count = db.IntegerProperty(required=True, default=0)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...