Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am geekkeen on github.
  • I am mouse_house (https://keybase.io/mouse_house) on keybase.
  • I have a public key ASA8KSnrLs_9Y8m3Iou9U5okQWog1wXOhwaseMGPPOL_3Ao

To claim this, I am signing this object:

@geekKeen
geekKeen / decorate.go
Created November 6, 2018 02:31
Golang
type APIHandler func(http.ResponseWriter, req *http.Request, ps httprouter.Params)
type Decorator func(APIHandler) APIHandler
func Log(logf lg.AppLogFunc) Decorator{
return func(f APIHandler) APIHandler{
return func(w http.ResponseWriter, req *http.Request, ps httprouter.Parms){
start = time.Now()
response, err = f(w, req, ps)
elapsed = time.Since(Start)
status := 200
@geekKeen
geekKeen / awk.sh
Created October 30, 2018 02:17
shell
# awk 使用方法
cat ad.platform.site | awk '{ split($27,url,"?"); if($33>5000 && $34 == "msecs" && $22 == "Oct" && $23 == 29 ) {print $22,$23,$24,url[1],$33,$34}}'
# Find an insteresting code about the relationship `Schedule` and `Job` in apschedule
Class Job(obejct):
def __init__(self, scheduler, id, **kwargs):
self._scheduler = scheduler
self.id = id
def modify(self, job, **changes):
self._scheduler.modify_job(self.id, **changes)
@geekKeen
geekKeen / get_callable_name.py
Last active January 9, 2018 02:43
inspection func
def get_callable_name(func):
if hasattr(func, '__qualname__'):
return func.__qualname__
# get func's class
f_self = getattr(func, '__self__', None) or getattr(func, 'im_self', None)
if f_self and hasattr(func, '__name__'):
# bound method class
f_class = f_self if isinstance(f_self, type) else f_self.__calss__
else:
@geekKeen
geekKeen / memoized_slots.py
Created December 26, 2017 03:23
Object attr
class MemoizedSlots(object):
def _fallback_getattr(self, key):
raise AttributeError(key)
def __getattr__(self, key):
if key.startswith('_memoized'):
raise AttributeError(key)
elif hasattr(self, '_memoized_attr_%s' % key):
value = getattr(self, '_memoized_attr_%s' % key)()
setattr(self, key, value)
@geekKeen
geekKeen / walk.py
Created November 2, 2017 03:51
Recursion 的常用写法
def get_labels(model, *filter_options):
unnest_lables = db.session.query(func.unnest(getattr(model, 'labels'))).filter(*filter_options)
labels = set()
def _walk(iterable):
for item in iterable:
if isinstance(item, tuple):
_walk(item)
else:
labels.add(item)
@geekKeen
geekKeen / proxy.py
Created October 12, 2017 01:59
代理模式 属性访问控制
class FileStorage(object):
def __init__(self, stream, **kwargs):
self.stream = stream
def __getattr__(self, name)
return getattr(self.stream, name)
@geekKeen
geekKeen / file_iterator.py
Last active February 1, 2018 08:38
生成器
class FileWrapper(object):
def __init__(self, file, buf_size=8192):
self.file = file
self.buf_size = buf_size
def close(self):
if hasattr(self.file, 'close'):
self.file.close()
def __iter__(self):
@geekKeen
geekKeen / exception_msg.py
Last active January 31, 2018 03:35
异常处理 / 自定义异常
class JobLookupError(KeyError):
def __init__(self, job_id):
super(KeyError, self).__init__(u'No job by the id of %s was found' % job_id)
raise JobLookupError(job_id)