Skip to content

Instantly share code, notes, and snippets.

View tawateer's full-sized avatar
Focusing

wateer tawateer

Focusing
  • Tencent
  • Beijing
View GitHub Profile
#!/usr/bin/env/python
#
# More of a reference of using jinaj2 without actual template files.
# This is great for a simple output transformation to standard out.
#
# Of course you will need to "sudo pip install jinja2" first!
#
# I like to refer to the following to remember how to use jinja2 :)
# http://jinja.pocoo.org/docs/templates/
#
@tawateer
tawateer / server_info.py
Last active August 29, 2015 14:21
机器信息获取
#/usr/bin/python
#-*- coding: utf-8 -*-
import re
import os
import subprocess
def shell(cmd):
@tawateer
tawateer / tornado_exception.py
Last active August 29, 2015 14:23
tornado中捕获异常并传给client
#!/bin/env python
import tornado.ioloop
import tornado.web
import tornado.httpserver
def _Exception(func):
def _decorator(*args, **kwargs):
@tawateer
tawateer / nginx_deploy.py
Last active August 29, 2015 14:23
Nginx 配置文件发布脚本
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Nginx 配置文件发布.
支持使用 lvs http check 发布和 reload 发布两种方式.
"""
@tawateer
tawateer / README.txt
Last active August 29, 2015 14:23
python pkgutil.extend_path 的作用
test 是一个目录:
test
├── __init__.py
└── echo.py
extend_path 会扫描 sys.path 下面的每一个目录, 如果目录下存在 __name__ 这个子目录,
而且子目录下存在 __init__.py 文件, 那么就把子目录加入 __path__ ;
而且 它会扫描 sys.path 的目录下是否有 __name__.pkg 文件, 如果有会把里面的路径都加到 __path__(不做检查).
@tawateer
tawateer / function_partial_example.py
Last active August 29, 2015 14:23
python functools 中 partial 和 wraps 的用处
#!/bin/env python
# -*- coding: utf-8 -*-
""" partial 实例.
看上面的源代码, 可以看出 partial 接受一个函数和若干参数, 返回一个新的函数,新的函数已经包含了之前的函数和参数,
而新函数执行时引入的新参数会和之前的参数相加, 真正执行的函数还是之前的函数.
所以, partial 的作用是动态绑定参数, 第一次用 partial 生成新函数, 第二次继续绑定参数并执行.
@tawateer
tawateer / decorator_example.py
Last active August 29, 2015 14:23
decorator 例子
#!/usr/bin/python
#-*- coding: utf-8 -*-
"""
output:
execute now1():
2013-12-25
call now2():
2013-12-25
@tawateer
tawateer / WSGIServer.py
Last active August 29, 2015 14:23
WSGIServer 例子, 理解什么是 WSGI
#!/bin/env python
# Tested with Python 2.7.9, Linux & Mac OS X
import socket
import StringIO
import sys
@tawateer
tawateer / lock_app.py
Last active August 29, 2015 14:23
Python 中锁的概念,互斥锁、可重入锁、Condition 条件、使用 Event 实现线程间通信.
#!/bin/env python
import threading
import functools
global mutex
mutex = threading.Lock()
@tawateer
tawateer / closing_example.py
Last active August 29, 2015 14:23
理解 with 和 contextlib 的用法.
#/bin/env python
import urllib
import contextlib
with contextlib.closing(urllib.urlopen("http://www.baidu.com")) as x:
print x.code