Skip to content

Instantly share code, notes, and snippets.

@wingyiu
wingyiu / borg
Created December 12, 2017 16:18
class Borg:
__shared_state = {}
def __new__(cls, *args, **kwargs):
instance = super().__new__(cls, *args, **kwargs)
instance.__dict__ = cls.__shared_state
return instance
@wingyiu
wingyiu / singleton.py
Created December 12, 2017 16:18 — forked from werediver/singleton.py
A thread safe implementation of singleton pattern in Python. Based on tornado.ioloop.IOLoop.instance() approach.
import threading
# Based on tornado.ioloop.IOLoop.instance() approach.
# See https://github.com/facebook/tornado
class SingletonMixin(object):
__singleton_lock = threading.Lock()
__singleton_instance = None
@classmethod
#!/bin/bash
SCRIPT_DIR=`readlink -f $(dirname $0)`
CONFNAME=git.conf
CFG_FILE=${SCRIPT_DIR}/${CONFNAME}
# 默认配置
DIR=/tmp/boss/publish
# 加载自定义配置
@wingyiu
wingyiu / caller_name.py
Created July 14, 2017 15:12 — forked from techtonik/caller_name.py
Python - inspect - Get full caller name (package.module.function)
# Public Domain, i.e. feel free to copy/paste
# Considered a hack in Python 2
import inspect
def caller_name(skip=2):
"""Get a name of a caller in the format module.class.method
`skip` specifies how many levels of stack to skip while getting caller
name. skip=1 means "who calls me", skip=2 "who calls my caller" etc.
@wingyiu
wingyiu / beautiful_idiomatic_python.md
Created August 28, 2016 13:12 — forked from JeffPaine/beautiful_idiomatic_python.md
Transforming Code into Beautiful, Idiomatic Python: notes from Raymond Hettinger's talk at pycon US 2013. The code examples and direct quotes are all from Raymond's talk. I've reproduced them here for my own edification and the hopes that others will find them as handy as I have!

Transforming Code into Beautiful, Idiomatic Python

Notes from Raymond Hettinger's talk at pycon US 2013 video, slides.

The code examples and direct quotes are all from Raymond's talk. I've reproduced them here for my own edification and the hopes that others will find them as handy as I have!

Looping over a range of numbers

for i in [0, 1, 2, 3, 4, 5]:
[alias]
tree = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
[color]
diff = auto
status = auto
branch = auto
interactive = auto
ui = true
pager = true
@wingyiu
wingyiu / sweep2.txt
Created February 25, 2016 05:33
voronoi sweep line algorithm implementation by author fortune
# To unbundle, sh this file
echo Makefile 1>&2
sed 's/.//' >Makefile <<'//GO.SYSIN DD Makefile'
-C=edgelist.c geometry.c heap.c main.c memory.c output.c voronoi.c
-O=edgelist.o geometry.o heap.o main.o memory.o output.o voronoi.o
-
-tt: voronoi t
- voronoi -t <t >tt
-voronoi: $O
- cc -o voronoi $O -lm
@wingyiu
wingyiu / wsgi和tornado.md
Created November 21, 2015 12:57 — forked from nature-python/wsgi和tornado.md
python wsgi 与 tornado

#WSGI:(web server gateway interface)WEB服务器网关接口 WSGI是为python语言定义的web服务器和web应用程序或框架之间的一种简单而实用的借口。wsgi是一个web组件的接口规范,它将web组件分为三类:server,middleware,application

##wsgi server wsgi server可以理解为一个符合wsgi规范的web server,接收request请求,封装一系列环境变量,按照wsgi规范调用注册的wsgi app,最后将response返回给客户端。文字很难解释清楚wsgi server到底是什么东西,以及做些什么事情,最直观的方式还是看wsgi server的实现代码。以python自带的wsgiref为例,wsgiref是按照wsgi规范实现的一个简单wsgi server。其工作流程如下:

  1. 服务器创建socket,监听端口,等待客户端连接。
  2. 当有请求来时,服务器解析客户端信息放到环境变量environ中,并调用绑定的handler来处理请求。
  3. handler解析这个http请求,将请求信息例如method,path等放到environ中。
  4. wsgi handler再将一些服务器端信息也放到environ中,最后服务器信息,客户端信息,本次请求信息全部都保存到了环境变量environ中。
@wingyiu
wingyiu / CustomCollectionFlowLayout.h
Last active September 12, 2015 13:19 — forked from toblerpwn/CustomCollectionFlowLayout.h
Sticky Headers at the top of a UICollectionView! -- // -- http://stackoverflow.com/questions/13511733/how-to-make-supplementary-view-float-in-uicollectionview-as-section-headers-do-i -- // -- still needs work around contentInsets.bottom and oddly-sized footers.
//
// CustomCollectionFlowLayout.h
// evilapples
//
// http://stackoverflow.com/questions/13511733/how-to-make-supplementary-view-float-in-uicollectionview-as-section-headers-do-i
//
//
#import <UIKit/UIKit.h>
@wingyiu
wingyiu / bcompare-git.md
Last active September 2, 2015 07:00 — forked from jfromaniello/bcompare-git.md
Integrate beyond compare 4 with git

Install command line tools:

then run this:

git config --global diff.tool bc3
git config --global difftool.bc3 trustExitCode true
git config --global merge.tool bc3