我为博客/新闻网站编写代码.主页有10篇最新文章,还有一个存档部分,所有文章按修改时间降序排序.在存档部分中,我使用基于游标的分页,并且我从第二页开始缓存结果,因为只有在发布新文章或由于某种原因现有的文章发布时才更改页面.每页有10篇文章.因此,当用户点击具有某个数字(而不是第一个)的存档页面时,首先检查该页面编号的内存缓存.如果页面不存在,则检查memcache是否存在该页面的光标,然后使用该游标从数据存储区中获取结果:
class archivePage:
def GET(self, page):
if not page:
articles = memcache.get('archivePage')
if not articles:
articles = fetchArticles()
memcache.set('archivePage', articles)
else:
if int(page) == 0 or int(page) == 1:
raise web.seeother('/archive')
articles = memcache.get('archivePage'+page)
if not articles:
pageCursor = memcache.get('ArchivePageMapping'+page)
if not pageCursor:
pageMapping = ArchivePageMapping.query(ArchivePageMapping.page == int(page)).get()
pageCursor = pageMapping.cursor
memcache.set('ArchivePageMapping'+page, pageCursor)
articles = fetchArticles(cursor=Cursor(urlsafe=pageCursor))
memcache.set('archivePage'+page, articles)
Run Code Online (Sandbox Code Playgroud)
每次创建新文章或更改现有文章的状态(草稿/发布)时,我都会刷新存档页面结果和游标的缓存.我将文章保存到数据存储区后执行此操作:
class addArticlePage:
def POST(self):
formData = web.input()
if formData.title and formData.content:
article = Article(title=formData.title,
content=formData.content,
status=int(formData.status))
key = article.put()
if int(formData.status) …
Run Code Online (Sandbox Code Playgroud) 我是经验丰富的Python/Jupyter用户,但是Windows新手,在下载并安装Anaconda Python 3发行版并启动Jupyter笔记本后,我注意到Jupyter Notebook的内核Python[Root]
(而不是Python 3
基于Unix的系统).
笔记本电脑工作正常,但共享笔记本电脑似乎有问题,因为每当在我的机器上创建的笔记本电脑在非Windows机器上打开时,用户会遇到"无法找到Python[Root]
内核"消息并被提示选择Python 3(或Python 2)内核.这很烦人.
我似乎没有选择在笔记本中手动更改内核.也许这是我的Windows机器上如何安装Anaconda(或Jupyter)的问题?
我使用mitsuhiko的pbkdf2实现密码散列:
def pbkdf2_bin(data, salt, iterations=1000, keylen=24, hashfunc=None):
"""Returns a binary digest for the PBKDF2 hash algorithm of `data`
with the given `salt`. It iterates `iterations` time and produces a
key of `keylen` bytes. By default SHA-1 is used as hash function,
a different hashlib `hashfunc` can be provided.
"""
hashfunc = hashfunc or hashlib.sha1
mac = hmac.new(data, None, hashfunc)
def _pseudorandom(x, mac=mac):
h = mac.copy()
h.update(x)
return map(ord, h.digest())
buf = []
for block in xrange(1, -(-keylen // mac.digest_size) + …
Run Code Online (Sandbox Code Playgroud)