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
# post_commit and post_rollback transaction signals for Django with monkey patching
# Author Grégoire Cachet <gregoire.cachet@gmail.com>
# http://gist.github.com/247844
#
# Usage:
# You have to make sure to load this before you use signals.
# For example, create utils/__init__.py and then utils/transaction.py contening
# this gist in you project. Then add "import utils.transaction" in your project
# __init__.py file
#
@wingyiu
wingyiu / nginx
Created August 17, 2013 06:53
Nginx Init Script for centos
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
@wingyiu
wingyiu / memcached
Created August 17, 2013 06:54
memcached init script for centos
#! /bin/bash
#
# memcached: MemCached Daemon
#
# chkconfig: - 90 25
# description: MemCached Daemon
#
### BEGIN INIT INFO
# Provides: memcached
# Required-Start: $syslog