本文整理汇总了Python中persistent.mapping.PersistentMapping.pop方法的典型用法代码示例。如果您正苦于以下问题:Python PersistentMapping.pop方法的具体用法?Python PersistentMapping.pop怎么用?Python PersistentMapping.pop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类persistent.mapping.PersistentMapping
的用法示例。
在下文中一共展示了PersistentMapping.pop方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Box
# 需要导入模块: from persistent.mapping import PersistentMapping [as 别名]
# 或者: from persistent.mapping.PersistentMapping import pop [as 别名]
class Box(Persistent):
implements(IDepositBox)
def __init__(self, max_age=config.MAX_AGE, purge_days=config.PURGE_DAYS):
self.data = PersistentMapping()
self._last_purge = int(time.time())
self.max_age = max_age
self.purge_days = purge_days
def _generate_new_id(self):
"""Generate new id.
"""
new_id = id_generator()
while new_id in self.data.keys():
new_id = id_generator()
return new_id
def put(self, value, token=None):
"""Put value in box, with optional token, and return generated id.
Calling this method also does a purge once a day (well, when
the last purge was at least 24 hours ago). The frequency can
be controlled with the purge_days attribute.
"""
cutoff = int(time.time()) - (self.purge_days * 86400)
if self._last_purge < cutoff:
self.purge()
if value is None:
raise ValueError
id = self._generate_new_id()
self.data[id] = BoxItem(token, value, confirmed=False)
return id
def edit(self, secret, value, token=None):
"""Edit value in the box, when secret and optional token match.
"""
if value is None:
raise ValueError
stored = self.get(secret, token=token)
if value == stored:
# No change
return
self.data[secret] = BoxItem(token, value, confirmed=True)
def get(self, secret, token=None):
stored = self.data.get(secret)
if stored is None:
return None
if stored.token != token:
# raise Exception
return None
if not stored.confirmed:
# Purge this item when it is expired:
cutoff = int(time.time()) - self.max_age * 86400
if stored.timestamp < cutoff:
del self.data[secret]
return None
if token:
# When there is a token, the item must be confirmed
# before we return the value. Main use case: email
# confirmation.
return None
return stored.value
def confirm(self, secret, token=None):
"""Confirm the item/token and return whether this succeeded or not.
"""
stored = self.data.get(secret)
if stored is None:
return None
if stored.token != token:
# raise Exception?
return None
if not stored.confirmed:
# First check if the confirmation comes too late.
cutoff = int(time.time()) - self.max_age * 86400
if stored.timestamp < cutoff:
del self.data[secret]
# Report back that we have failed, in case anyone
# wants to know.
return False
stored.confirmed = True
return True
def pop(self, secret, token=None):
stored = self.get(secret, token=token)
if stored is None:
return None
self.data.pop(secret)
return stored
def get_all_confirmed(self):
for key, stored in self.data.items():
if stored.confirmed:
yield stored.value
def purge(self):
"""Purge items that have expired.
#.........这里部分代码省略.........