本文整理汇总了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:
#.........这里部分代码省略.........
示例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
#.........这里部分代码省略.........