Skip to content

Instantly share code, notes, and snippets.

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 September 10, 2015 09:53
Create directory if necessary
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 September 10, 2015 10:10
Tic-toc timer in Python
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 August 29, 2015 14:16
Hack to check the code is executed in an IPython Notebook
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 August 29, 2015 14:12
Multiplex stdout to a file
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 October 5, 2015 08:32
Convert a human readable DataFrame representation to an actual DataFrame
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