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
@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 / 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):
"""
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 / bootstrap.sh
Created May 2, 2013 08:23
使用python来写linux的service脚本
#!/usr/local/bin/python2.7
#encoding:utf-8
#
# chkconfig: - 91 35
# description: Starts and stops the app server\
# used to provide app services.
#
import sys
import os
pid = "/var/run/app.pid"
@jamiesun
jamiesun / gist:3765636
Created September 22, 2012 09:20
asynchronous subprocess
# subprocess - Subprocesses with accessible I/O streams
#
# For more information about this module, see PEP 324.
#
# Copyright (c) 2003-2005 by Peter Astrand <astrand@lysator.liu.se>
#
# Licensed to PSF under a Contributor Agreement.
# See http://www.python.org/2.4/license for licensing details.
r"""subprocess - Subprocesses with accessible I/O streams
@jamiesun
jamiesun / gist:3760891
Created September 21, 2012 10:55
namedtuple
EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade')
import csv
for emp in map(EmployeeRecord._make, csv.reader(open("employees.csv", "rb"))):
print emp.name, emp.title
import sqlite3
conn = sqlite3.connect('/companydata')
cursor = conn.cursor()
cursor.execute('SELECT name, age, title, department, paygrade FROM employees')
#!/bin/bash
logs_path="/var/log/named/"
oldlogs=${logs_path}dns_logs
newlogs=${logs_path}dns_logs_$(date -d "yesterday" +"%Y%m%d")
rm -fr ${newlogs}
cp ${oldlogs} ${newlogs}
echo "" > ${oldlogs}
import datetime
def convtime(ctime):
if not ctime:
return ''
cdate = datetime.datetime.strptime(ctime,'%Y-%m-%d %H:%M:%S')
nowdate = datetime.datetime.now()
dt = nowdate - cdate
secs = dt.total_seconds()
@jamiesun
jamiesun / 5c286d3256ca44d892a7817d79e8d2c3.py
Created September 21, 2012 10:34
独立爬虫脚本
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: Rolando Espinoza La fuente
#
# Changelog:
# 24/07/2011 - updated to work with scrapy 13.0dev
# 25/08/2010 - initial version. works with scrapy 0.9
from scrapy.contrib.loader import XPathItemLoader
from scrapy.item import Item, Field
#!/bin/bash
USERNAME=root
PASSWORD=root
DBNAME=mydb
DATE=`date +%Y-%m-%d`
OLDDATE=`date +%Y-%m-%d -d '-20 days'`
FTPOLDDATE=`date +%Y-%m-%d -d '-60 days'`