本文整理汇总了Python中pydblite.Base.filter方法的典型用法代码示例。如果您正苦于以下问题:Python Base.filter方法的具体用法?Python Base.filter怎么用?Python Base.filter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pydblite.Base
的用法示例。
在下文中一共展示了Base.filter方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from pydblite import Base [as 别名]
# 或者: from pydblite.Base import filter [as 别名]
class Posts:
def __init__(self, filename, erase_db):
self.db = Base(filename)
self.db.create('author', 'content', 'date', mode="override" if erase_db else "open")
def addPost(self, post):
""" Persist a Post object in db and returns auto-generated id. """
post.id = self.db.insert(author = post.author, content = post.content, date = post.date)
self.db.commit()
return post.id
def getPost(self, id):
""" Get a post by its id. Returns a Post object or None if id is not found. """
db_entry = self.db[id] if id in self.db else None
return self.__createPost(db_entry) if db_entry is not None else None
def getPosts(self, from_date = None, to_date = None, author = None):
""" Get all posts matching optionally provided conditions. Returns a list (can be empty). """
iterator = self.db.filter()
if from_date is not None:
iterator = iterator & (self.db("date") > from_date)
if to_date is not None:
iterator = iterator & (self.db("date") < to_date)
if author is not None:
iterator = iterator & (self.db("author") == author)
return [self.__createPost(db_entry) for db_entry in iterator]
def getPostsCount(self):
""" Get total number of posts in db. """
return len(self.db)
def __createPost(self, db_entry):
return Post(db_entry['author'], db_entry['content'], db_entry['date'], db_entry['__id__'])