Skip to content

Instantly share code, notes, and snippets.

def json2table(json_file, **kwds):
import pandas
df = pandas.read_json(json_file, **kwds)
df = df.set_index(df.columns[0])
with pandas.option_context("display.max_rows", None,
"display.max_columns", None):
print(df)
def make_parser(doc=__doc__):
@tkf
tkf / capturing_locals.py
Created April 22, 2017 15:15
Capture the local variables of a function upon exception.
from contextlib import contextmanager
import sys
@contextmanager
def capturing_locals(depth=1):
"""
Capture the local variables of a function upon exception.
>>> def fun():
@tkf
tkf / omp.py
Created April 20, 2017 04:48
Interface to OpenMP Runtime Library Routines.
import ctypes
class OpenMPRuntime(object):
"""
Interface to OpenMP Runtime Library Routines.
>>> gnu = OpenMPRuntime('libgomp.so')
>>> gnu.omp_set_num_threads(3)
@tkf
tkf / coroutine_online_average.py
Created April 1, 2017 07:38
Online average using coroutine
import functools
import itertools
def coroutine_send(func):
@functools.wraps(func)
def start(*args, **kwds):
cr = func(*args, **kwds)
next(cr)
return cr.send
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@tkf
tkf / LyapunovExponentsWithForwardDiff.jl
Last active June 3, 2019 01:06
Calculating Lyapunov Exponents with ForwardDiff.jl and DifferentialEquations.jl
module LyapunovExponentsWithForwardDiff
using DifferentialEquations
using ForwardDiff
using ParameterizedFunctions
using ProgressMeter
using RecipesBase
type LyapunovExponentsResult
sol_p
@tkf
tkf / simplify_with_pos.py
Created February 24, 2017 08:50
Simplify `expr` assuming `x` is positive.
import sympy
def recfactor(expr, deep=False):
"""Recursively factorize `expr` including additive terms."""
if isinstance(expr, (sympy.Mul, sympy.Add)):
cls = type(expr)
return cls(*[
recfactor(a, deep=deep).factor(deep=deep)
for a in expr.args
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#!/bin/bash -e
bin="${BASH_SOURCE[0]%.*}"
go build -o "$bin" "$bin.go"
for file in "$@"
do
for rev in $(git log --format='format:%H' "$file" | head -n-1)
do
@tkf
tkf / wscript
Created April 16, 2016 16:31
Install REVTeX under build/ and compile apssamp.tex and aipsamp.tex
def configure(conf):
# https://waf.io/apidocs/tools/tex.html
conf.load('tex')
if not conf.env.LATEX:
conf.fatal('The program LaTeX is required')
if conf.options.synctex:
conf.env.PDFLATEXFLAGS = ['-synctex=1']
def build(bld):