Skip to content

Instantly share code, notes, and snippets.

View keybase.md

Keybase proof

I hereby claim:

  • I am ys-l on github.
  • I am liauys (https://keybase.io/liauys) on keybase.
  • I have a public key ASAzTx57Edln8xZW5qIk-WBCyrkKL_c87j5901kSgRIw1wo

To claim this, I am signing this object:

@YS-L
YS-L / mkdir.py
Last active Sep 10, 2015
Create directory if necessary
View mkdir.py
def mkdir_p(directory):
"""Create a directory if necessary"""
# Thread safe
import os
try:
os.makedirs(directory)
except OSError:
pass
@YS-L
YS-L / timer.py
Last active Sep 10, 2015
Tic-toc timer in Python
View timer.py
import time
class Timer(object):
""" A quick tic-toc timer
Credit: http://stackoverflow.com/questions/5849800/tic-toc-functions-analog-in-python
"""
def __init__(self, name=None, verbose=True):
@YS-L
YS-L / in_ipynb.py
Last active Aug 29, 2015
Hack to check the code is executed in an IPython Notebook
View in_ipynb.py
def in_ipynb():
try:
cfg = get_ipython().config
if cfg['IPKernelApp']['parent_appname'] == 'ipython-notebook':
return True
else:
return False
except NameError:
return False
@YS-L
YS-L / Tee.py
Last active Aug 29, 2015
Multiplex stdout to a file
View Tee.py
from traceback import print_exception
class Tee(object):
""" Multiplex stdout to a file
Possibly useful for inspecting the output of a long running python process started via IPython.
Adapted from answers found in SO questions [1] and [2].
@YS-L
YS-L / to_dataframe.py
Last active Oct 5, 2015
Convert a human readable DataFrame representation to an actual DataFrame
View to_dataframe.py
import pandas as pd
from functools import wraps
@wraps(pd.read_csv)
def to_dataframe(s, sep='[\s]+', **kwargs):
"""
Convert a human readable DataFrame representation to an actual DataFrame
s : string