本文整理匯總了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__'])