當前位置: 首頁>>代碼示例>>Python>>正文


Python Object.decorate方法代碼示例

本文整理匯總了Python中ming.base.Object.decorate方法的典型用法代碼示例。如果您正苦於以下問題:Python Object.decorate方法的具體用法?Python Object.decorate怎麽用?Python Object.decorate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在ming.base.Object的用法示例。


在下文中一共展示了Object.decorate方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: ODMCursor

# 需要導入模塊: from ming.base import Object [as 別名]
# 或者: from ming.base.Object import decorate [as 別名]
class ODMCursor(object):

    def __init__(self, session, cls, ming_cursor, refresh=False, decorate=None):
        self.session = session
        self.cls = cls
        self.mapper = mapper(cls)
        self.ming_cursor = ming_cursor
        self._options = Object(
            refresh=refresh,
            decorate=decorate,
            instrument=True)

    def __iter__(self):
        return self

    def __len__(self):
        return self.count()

    @property
    def extensions(self):
        return self.session.extensions

    def count(self):
        return self.ming_cursor.count()

    def _next_impl(self):
        doc = self.ming_cursor.next()
        obj = self.session.imap.get(self.cls, doc['_id'])
        if obj is None:
            obj = self.mapper.create(doc, self._options)
            state(obj).status = ObjectState.clean
            self.session.save(obj)
        elif self._options.refresh:
            # Refresh object
            state(obj).update(doc)
            state(obj).status = ObjectState.clean
        else:
            # Never refresh objects from the DB unless explicitly requested
            pass
        other_session = session(obj)
        if other_session is not None and other_session != self:
            other_session.expunge(obj)
            self.session.save(obj)
        if self._options.decorate is not None:
            return self._options.decorate(obj)
        else:
            return obj

    def next(self):
        call_hook(self, 'before_cursor_next', self)
        try:
            return self._next_impl()
        finally:
            call_hook(self, 'after_cursor_next', self)

    def options(self, **kwargs):
        odm_cursor = ODMCursor(self.session, self.cls,self.ming_cursor)
        odm_cursor._options = Object(self._options, **kwargs)
        call_hook(self, 'cursor_created', odm_cursor, 'options', self, **kwargs)
        return odm_cursor

    def limit(self, limit):
        odm_cursor = ODMCursor(self.session, self.cls,
                               self.ming_cursor.limit(limit))
        call_hook(self, 'cursor_created', odm_cursor, 'limit', self, limit)
        return odm_cursor

    def skip(self, skip):
        odm_cursor = ODMCursor(self.session, self.cls,
                               self.ming_cursor.skip(skip))
        call_hook(self, 'cursor_created', odm_cursor, 'skip', self, skip)
        return odm_cursor

    def hint(self, index_or_name):
        odm_cursor = ODMCursor(self.session, self.cls,
                               self.ming_cursor.hint(index_or_name))
        call_hook(self, 'cursor_created', odm_cursor, 'hint', self, index_or_name)
        return odm_cursor

    def sort(self, *args, **kwargs):
        odm_cursor = ODMCursor(self.session, self.cls,
                               self.ming_cursor.sort(*args, **kwargs))
        call_hook(self, 'cursor_created', odm_cursor, 'sort', self, *args, **kwargs)
        return odm_cursor

    def one(self):
        try:
            result = self.next()
        except StopIteration:
            raise ValueError, 'Less than one result from .one()'
        try:
            self.next()
        except StopIteration:
            return result
        raise ValueError, 'More than one result from .one()'

    def first(self):
        try:
            return self.next()
        except StopIteration:
#.........這裏部分代碼省略.........
開發者ID:apendleton,項目名稱:Ming,代碼行數:103,代碼來源:odmsession.py

示例2: ODMCursor

# 需要導入模塊: from ming.base import Object [as 別名]
# 或者: from ming.base.Object import decorate [as 別名]
class ODMCursor(object):
    """Represents the results of query.

    The cursors can be iterated over to retrieve the
    results one by one.
    """
    def __bool__(self):
        raise MingException('Cannot evaluate ODMCursor to a boolean')
    __nonzero__ = __bool__  # python 2

    def __init__(self, session, cls, ming_cursor, refresh=False, decorate=None, fields=None):
        self.session = session
        self.cls = cls
        self.mapper = mapper(cls)
        self.ming_cursor = ming_cursor
        self._options = Object(
            refresh=refresh,
            decorate=decorate,
            fields=fields,
            instrument=True)

    def __iter__(self):
        return self

    @property
    def extensions(self):
        return self.session.extensions

    def count(self):
        """Get the number of objects retrieved by the query"""
        return self.ming_cursor.count()

    def _next_impl(self):
        doc = next(self.ming_cursor)
        obj = self.session.imap.get(self.cls, doc['_id'])
        if obj is None:
            obj = self.mapper.create(doc, self._options, remake=False)
            state(obj).status = ObjectState.clean
            self.session.save(obj)
        elif self._options.refresh:
            # Refresh object
            state(obj).update(doc)
            state(obj).status = ObjectState.clean
        else:
            # Never refresh objects from the DB unless explicitly requested
            pass
        other_session = session(obj)
        if other_session is not None and other_session != self:
            other_session.expunge(obj)
            self.session.save(obj)
        if self._options.decorate is not None:
            return self._options.decorate(obj)
        else:
            return obj

    def next(self):
        _call_hook(self, 'before_cursor_next', self)
        try:
            return self._next_impl()
        finally:
            _call_hook(self, 'after_cursor_next', self)

    __next__ = next

    def options(self, **kwargs):
        odm_cursor = ODMCursor(self.session, self.cls,self.ming_cursor)
        odm_cursor._options = Object(self._options, **kwargs)
        _call_hook(self, 'cursor_created', odm_cursor, 'options', self, **kwargs)
        return odm_cursor

    def limit(self, limit):
        """Limit the number of entries retrieved by the query"""
        odm_cursor = ODMCursor(self.session, self.cls,
                               self.ming_cursor.limit(limit))
        _call_hook(self, 'cursor_created', odm_cursor, 'limit', self, limit)
        return odm_cursor

    def skip(self, skip):
        """Skip the first ``skip`` entries retrieved by the query"""
        odm_cursor = ODMCursor(self.session, self.cls,
                               self.ming_cursor.skip(skip))
        _call_hook(self, 'cursor_created', odm_cursor, 'skip', self, skip)
        return odm_cursor

    def hint(self, index_or_name):
        odm_cursor = ODMCursor(self.session, self.cls,
                               self.ming_cursor.hint(index_or_name))
        _call_hook(self, 'cursor_created', odm_cursor, 'hint', self, index_or_name)
        return odm_cursor

    def sort(self, *args, **kwargs):
        """Sort results of the query.

        See :meth:`pymongo.cursor.Cursor.sort` for details on the available
        arguments.
        """
        odm_cursor = ODMCursor(self.session, self.cls,
                               self.ming_cursor.sort(*args, **kwargs))
        _call_hook(self, 'cursor_created', odm_cursor, 'sort', self, *args, **kwargs)
        return odm_cursor
#.........這裏部分代碼省略.........
開發者ID:vzhz,項目名稱:function_generator,代碼行數:103,代碼來源:odmsession.py


注:本文中的ming.base.Object.decorate方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。