Skip to content

Instantly share code, notes, and snippets.

View jian-en's full-sized avatar

jian-en jian-en

View GitHub Profile
@jian-en
jian-en / singledispatch_demo.py
Created July 27, 2017 03:01
A method to replace if else if
from functools import singledispatch
from collections import abc
import numbers
import html
@singledispatch
def htmlize(obj):
content = html.escape(repr(obj))
return '<pre>{}</pre>'.format(content)
@jian-en
jian-en / clockdemo.py
Last active July 27, 2017 03:13
Usage of lru_cache
import time
import functools
def clock(func):
@functools.wraps(func) # relace with orig func's __name__ and __doc__
def clocked(*args, **kwargs):
t0 = time.time()
result = func(*args, **kwargs)
elapsed = time.time() - t0
name = func.__name__
@jian-en
jian-en / gist:a16be3ec8d8a42db6cbb
Created May 29, 2015 10:04
python timestamp to datetime object
datetime.datetime.fromtimestamp(
int("1284101485")
).strftime('%Y-%m-%d %H:%M:%S')
@jian-en
jian-en / gist:7d9bf37cc8d0b8eb1450
Created May 28, 2015 03:07
useful python snippets
import json
json.dumps(response, ensure_ascii=False).encoding('utf-8')