Skip to content

Instantly share code, notes, and snippets.

@ninehills
ninehills / b64.py
Created March 19, 2014 13:52
64进制
# http://snipperize.todayclose.com/snippet/py/64-%E5%92%8C-10%E8%BF%9B%E5%88%B6--93237/
def encode_b64(n):
table = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'
result = []
temp = n
if 0 == temp:
¦ result.append('0')
else:
@mywaiting
mywaiting / tornado.file.upload.py
Created August 23, 2012 16:45
Tornado上传文件的例子!
"""
http://saepy.sinaapp.com/topic/24/Tornado%E4%B8%8A%E4%BC%A0%E6%96%87%E4%BB%B6%E7%A4%BA%E4%BE%8B
在bcore的开发过程中,涉及到上传文件有两个地方,一个是相册,一个是文章图文混排。这里作为一个备忘。罗列一些关键点:
文件上传的内容体在tornado.web.RequestHandler.request.files属性中,并且是以数组形式存放的。
使用临时文件存储时,在write完成后要记着把seek重置到文件头。要不然文件无法被读取。
再使用Image模块的thumbnail方法进行缩放时,resample=1作为重载渲染参数能够有效的使图片平滑,消除锯齿。
@mywaiting
mywaiting / gist:2792038
Created May 26, 2012 03:47 — forked from jparise/gist:2789026
Protocol Buffer Mixin for Tornado
class ProtocolBufferMixin(object):
"""Protocol Buffer support for RequestHandler objects."""
MIMETYPE = 'application/x-protobuf'
def read_protobuf(self, message_type, data):
"""Attempts to parse a protocol buffer message from the given data."""
# Create the message object and attempt to parse the data into it.
try:
message = message_type()
@hrldcpr
hrldcpr / tree.md
Last active June 19, 2025 08:17
one-line tree in python

One-line Tree in Python

Using Python's built-in defaultdict we can easily define a tree data structure:

def tree(): return defaultdict(tree)

That's it!