Skip to content

Instantly share code, notes, and snippets.

View jamiesun's full-sized avatar
🍇
On vacation

Jett Wang jamiesun

🍇
On vacation
View GitHub Profile
"""
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作为重载渲染参数能够有效的使图片平滑,消除锯齿。
@jamiesun
jamiesun / caches.py
Created July 1, 2013 03:18
python memcached
#coding=:utf-8
import hashlib
from settings import settings
import memcache
import cPickle as pickle
mcache = memcache.Client(settings['memcache_url'],debug=0)
def cache_data(category='all',timeout=3600):
def func_warp1(func):
@jamiesun
jamiesun / resize_image.py
Created January 11, 2014 05:56
PIL压缩图片
def resize_img(img_path, out_path, new_width):
import Image
#读取图像
im = Image.open(img_path)
#获得图像的宽度和高度
width,height = im.size
#计算高宽比
ratio = 1.0 * height / width
#计算新的高度
new_height = int(new_width * ratio)
@jamiesun
jamiesun / redis
Created January 12, 2014 05:52
redis service script
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
#
# redis This is the init script for starting up the Redis server
#
# chkconfig: 2345 85 15
# description: Starts and stops the redis daemon that handles \
# all redis session requests.
#!/usr/bin/env python
#coding:utf-8
import sys, os, re
import logging
from tornado.ioloop import IOLoop
from tornado.iostream import IOStream
from tornado.netutil import TCPServer
from concurrent.futures import ThreadPoolExecutor
from functools import partial, wraps
import time
import tornado.ioloop
import tornado.web
EXECUTOR = ThreadPoolExecutor(max_workers=4)
#!/usr/bin/env python2
#coding:utf-8
import logging
from tornado.ioloop import IOLoop
from tornado.iostream import IOStream
from tornado.tcpserver import TCPServer
logging.basicConfig(level=logging.INFO, format='%(levelname)s - - %(asctime)s %(message)s', datefmt='[%d/%b/%Y %H:%M:%S]')
import os,sqlite3
def create_sql(infile,outfile):
print infile,outfile
outfs = open(outfile,'wb')
with open(infile,'rb') as infs:
@jamiesun
jamiesun / python_comprehension.ipynb
Created May 4, 2014 12:42
python_comprehension
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jamiesun
jamiesun / nextid.py
Created May 18, 2014 06:43
生成有时间规则的id
import datetime
def __next_id():
_inum = [10000]
def _next():
if _inum[0] >= 99999:
_inum[0] = 10000
_inum[0] += 1
_prefix = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
return int("%s%s"%(_prefix,_inum[0]))