Skip to content

Instantly share code, notes, and snippets.

View monklof's full-sized avatar

Chao Liu monklof

View GitHub Profile
@monklof
monklof / gist:917f4e4cdb1cd304d537df6cc62c35c7
Created April 8, 2016 04:21 — forked from mfenniak/gist:2978805
An extension of Flask that adds file hashes to static file URLs built by url_for("static"...)
import os.path
import contextlib
import hashlib
from flask import Flask
from flask.helpers import safe_join
# Injects an "h" parameter on the URLs of static files that contains a hash of
# the file. This allows the use of aggressive cache settings on static files,
# while ensuring that content changes are reflected immediately due to the
# changed URLs. Hashes are cached in-memory and only checked for updates when
@monklof
monklof / very_common_utils.py
Last active March 2, 2016 16:15
some common utils gist in python
def str_to_datetime(string):
return datetime.datetime.strptime(string, "%Y-%m-%d %H:%M:%S")
class PrefixLogger(object):
def __init__(self, prefix="", logger= logger):
self.logger = logger
self.prefix = prefix
self.logging_methods = {}
for funcname in ["debug", "info", "warning", "warn", "error", "critical", "log", "exception"]:
self.logging_methods[funcname] = self.wrap_log_method(self.prefix)(getattr(self.logger, funcname))
@staticmethod
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
from sqlalchemy import Column, Integer, String
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
name = Column(String)
@monklof
monklof / OutputDependHandler.py
Created November 4, 2015 16:36
Output Depended Log Handler
# coding: utf-8
import sys,os
import logging
from logging.handlers import TimedRotatingFileHandler
def is_stdout_attached_to_terminal():
'''判断当前进程组是不是前台进程组(主要关注stdout是否被关联到/dev/tty)'''
try:
# 如果stdout被绑定到终端设备上,则是前端进程组
if os.getpgrp() == os.tcgetpgrp(sys.stdout.fileno()):
@monklof
monklof / scriptdir.sh
Created November 3, 2015 08:17
shell get script dir in linux
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
@monklof
monklof / test_skeleton.py
Last active September 8, 2015 06:47
frequently used test code with nose
import signal
from functools import wraps
from nose.tools import nottest, with_setup
class TestTimeoutException(Exception):
pass
@nottest
def raise_timeout_exception(*args, **kwargs):
@monklof
monklof / multiprocessing-parallel-example.py
Last active August 29, 2015 14:26
parallel programming with multiprocessing
#coding: utf-8
"""调度系统日志分析:日志统计"""
import multiprocessing
from multiprocessing import Process, Queue, Pool
import logging
import time
import pprint
import os
@monklof
monklof / postJson
Created May 6, 2015 11:04
Js function for posting json data to backend.
jQuery.postJSON = function(url, args, successCall, errorCall){
$.ajax({
type:"post",
url:url,
data:JSON.stringify(args),
contentType:"application/json; charset=UTF-8",
success:successCall,
error:errorCall
});
}
{
"province": [
{
"code": 110000,
"name": "北京市",
"py": "beijingshi"
},
{
"code": 120000,
"name": "天津市",