Skip to content

Instantly share code, notes, and snippets.

@shuxiang
shuxiang / slog2rethink.py
Last active August 29, 2015 14:04
pull mysql log to rethinkdb
#coding=utf8
import uvent
uvent.install()
import gevent
from gevent import monkey
monkey.patch_all()# must patch first, then from gevent import sth else
from gevent.threadpool import ThreadPool
import time
import rethinkdb as r
@shuxiang
shuxiang / xlsx-xls2csv.js
Created August 12, 2014 06:17
handle xlsx xls csv file in frontend js
/************************ need js lib ***********************************
// https://raw.githubusercontent.com/SheetJS/js-xlsx/master/dist/xlsx.core.min.js
// https://raw.githubusercontent.com/SheetJS/js-xls/master/dist/xls.core.min.js
// https://raw.githubusercontent.com/gkindel/CSV-JS/master/csv.js
************************************************************************/
//upload file
function handleKVFile(e) {
var files = e.target.files;
var i,f;
@shuxiang
shuxiang / dbquery.py
Last active August 29, 2015 14:05
easy operat mysqldb
import MySQLdb
class Row(dict):
"""A dict that allows for object-like property access syntax."""
def __getattr__(self, name):
try:
return self[name]
except KeyError:
raise AttributeError(name)
@shuxiang
shuxiang / config_helper.py
Last active August 29, 2015 14:05
easy ini config cache
import os
import os.path
import time
from ConfigParser import SafeConfigParser
from django.conf import settings
__all__ = ['get_config']
_CONFIG_MAP = {}
def get_config(path):
@shuxiang
shuxiang / sqla.py
Last active September 7, 2022 10:34
sqlalchemy get model by name and get model by tablename
from flask.ext.sqlalchemy import SQLAlchemy
def get_model(self, name):
return self.Model._decl_class_registry.get(name, None)
SQLAlchemy.get_model = get_model
def get_model_by_tablename(self, tablename):
for c in self.Model._decl_class_registry.values():
if hasattr(c, '__tablename__') and c.__tablename__ == tablename:
return c
@shuxiang
shuxiang / tesseract.py
Last active July 13, 2017 02:47
captcha code hack
# apt-get install libtesseract3 libtesseract-dev; pip install pytesseract;
# download google tesseract eng data then copy it to /usr/local/share/tessdata: https://github.com/tesseract-ocr/tesseract/wiki/Data-Files
import Image
import requests
import pytesseract
from StringIO import StringIO
jpg = Image.open(StringIO(requests.get('http://test.jpg').content))
code = pytesseract.image_to_string(jpg).replace(' ', '')
@shuxiang
shuxiang / flask_pdf.py
Created October 18, 2014 06:19
flask operate pdf
from reportlab.pdfgen import canvas
from io import BytesIO
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
# 采用微软雅黑字体
def some_view():
pdfmetrics.registerFont(TTFont('msyh', os.path.join(veteransApp.config['PROJECT_DIR'], '../msyh.ttf')))
# Create the PDF object, using the response object as its "file."
buffer = BytesIO()
@shuxiang
shuxiang / fabfile.py
Created October 18, 2014 06:23
fabric test
#coding=utf8
from fabric.api import *
env.user = 'root'
env.password = 'jindengtaia10'
env.hosts = ['121.199.4.240']
@task
def nginx_start():
@shuxiang
shuxiang / proxy_google
Last active August 29, 2015 14:08
proxy of google
# /etc/nginx/sites-enabled/google
server {
listen 443 default ssl;
listen [::]:443;
#listen 80;
server_name yourhostname;
##1,设置反向代理的域名
ssl on;
ssl_certificate /home/sx/googleca.crt;
ssl_certificate_key /home/sx/nopassgoogle.key;
@shuxiang
shuxiang / timetool.py
Last active August 29, 2015 14:08
time transform
#coding=utf8
from datetime import datetime, timedelta, date
import time
from time import struct_time
from time import localtime
"""
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)