Skip to content

Instantly share code, notes, and snippets.

@ls0f
ls0f / analysis.py
Last active November 30, 2015 07:20
词频统计&DFA关键词匹配
# http://vdisk.weibo.com/s/azYuqTtsWXaEc
def analysis(fn):
with open(fn, 'rb') as f:
content = f.read()
word_dict = {}
cur_word = ''
total_word = 0
for c in content:
@ls0f
ls0f / python_daemon.py
Last active November 26, 2015 09:20
sime python daemon
#!/usr/bin/env python
# http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/
import sys, os, time, atexit
from signal import SIGTERM
class Daemon:
"""
A generic daemon class.
@ls0f
ls0f / notify.py
Last active November 23, 2015 09:01
daemon notify
import functools
import sys
import pyinotify
import datetime
def restart():
pass
class MyEventHandler(pyinotify.ProcessEvent):
def my_init(self, file_object=sys.stdout):
@ls0f
ls0f / pysheme.py
Last active November 14, 2015 14:32
python scheme interpreters http://norvig.com/lispy.html
import math,operator as op
Number = (int, float)
Symbol = str
class Env(dict):
"An environment: a dict of {'var':val} pairs, with an outer Env."
@ls0f
ls0f / png2jpg.py
Created November 13, 2015 03:27
convert png to jpg
#!/usr/bin/env python
# http://stackoverflow.com/questions/9166400/convert-rgba-png-to-rgb-with-pil
# http://stackoverflow.com/questions/1962795/how-to-get-alpha-value-of-a-png-image-with-pil
import sys
from PIL import Image
def main():
png = Image.open(sys.argv[1])
png.load()
background = Image.new("RGB", png.size, (255, 255, 255))
#!/bin/bash
welcome=(
'佛祖保佑 永无BUG'
'你好啊单身狗'
'壮士,干了这碗鸡汤!'
'上线了吗?'
'法海,你的头发怎么那么长!'
'少壮不努力,长大做挨踢.'
'佛祖,我好烦.'
@ls0f
ls0f / nginx_auth.py
Last active November 10, 2015 08:49
gen http auth passwd file
import os
import sys
import random
# more info
# http://trac.edgewall.org/export/10770/trunk/contrib/htpasswd.py
# We need a crypt module, but Windows doesn't have one by default. Try to find
# one, and tell the user if we can't.
try:
@ls0f
ls0f / proxy_google.conf
Last active November 7, 2015 04:36
反代google.co.jp
proxy_cache_path /data/nginx/cache/ levels=1:2 keys_zone=one:10m max_size=10g;
proxy_cache_key "$host$request_uri";
upstream google_jp {
server 173.194.38.216:80;
server 173.194.38.215:80;
server 173.194.38.217:80;
server 173.194.38.218:80;
}
@ls0f
ls0f / backup_mongo.sh
Last active November 5, 2015 02:35
mongodb backup
#!/bin/bash
BACKUP_DB="xx"
DUMP_CMD="/usr/bin/mongodump"
HOST="127.0.0.1"
PORT="27017"
TIMESTAMP=`date +%F-%H%M`
BACKUP_DIR="/opt/data/backup"
cd $BACKUP_DIR
BACKUP_NAME_PREFIX="app-db"
@ls0f
ls0f / one_process.py
Last active October 18, 2015 03:30
Prevent the script repeatedly run
import os
import inspect
import atexit
from functools import wraps
class OneProcess(object):
def __init__(self):
self.caller_file_name = inspect.getouterframes(