Skip to content

Instantly share code, notes, and snippets.

View rlabbe's full-sized avatar

Roger Labbe rlabbe

  • SF Bay Area, California
View GitHub Profile
import os
import winreg
''' find all duplicate and invalid paths in your user account on windows'''
def expand_env_vars(paths: list[str]) -> list[str]:
return [os.path.expandvars(path) for path in paths]
def find_invalid_paths():
user_paths, system_paths = get_user_and_system_paths()
combined_paths = user_paths + system_paths
Git diff on Jupyter notebook to ignore most of the irrelevant output cell changes
$ git diff 5579009 -I image/png -I execution_count -I \"version\":
minimal log to just see commit, author, and one line comment
$ git log --pretty=format:"%h%x09%an%x09%ad%x09%s"
git config --global alias.jdiff "diff -I image/png -I execution_count -I \"version\"::
@rlabbe
rlabbe / putf.cpp
Created December 4, 2018 22:48
putf implementation
#include <stdarg.h>
// simple implementation of the proposed std::putf
// int i = 3;
// auto d = 3.1415926;
// cout << putf("%03di -> %.3f", i, d) << endl;
std::string putf(const char* format, ...)
{
va_list args;
@rlabbe
rlabbe / attr_dict.py
Created September 12, 2018 17:07
AttrDict
class AttrDict(dict):
"""
dict that lets you access any key via attribute:
x['hi'] or x.hi
"""
def __init__(self, *args, **kwargs):
super(AttrDict, self).__init__(*args, **kwargs)
self.__dict__ = self
def __repr__(self):
@rlabbe
rlabbe / as_dict.py
Created March 15, 2018 13:33
numpy recarray to dict
def as_dict(rec):
""" turn a numpy recarray record into a dict. this is mostly useful
just to have a human readable output of a record on the console.
as_dict(my_data[234])
"""
return {name:rec[name] for name in rec.dtype.names}
@rlabbe
rlabbe / camel_to_snake.py
Created February 27, 2018 20:51
camel case to snake case
def camel_to_snake(name):
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
# convert comma separated csv header
keys = header.split(',')
keys = [camel_to_snake(k) for k in keys]
header = ','.join(keys)
@rlabbe
rlabbe / spyder_startup.py
Last active June 10, 2019 22:46
Make spyder act like I want
rom subprocess import call, Popen
import sys
import matplotlib as mpl
import numpy
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
def npp(precision=4):
''' create object using numpy to read CSV. creates one variable per column
so you can use f.col_name instead of f.data['col_name']
f = NamedCSV(filename)
f.data # all data in the file in an np recarray
f.mass
f.data['mass'] # same as previous line
f.extract(f.mass > 180)
'''
@rlabbe
rlabbe / psource.py
Created November 3, 2016 16:15
Print source without docstring
from inspect import getsource
import re
def psource(fun):
print(re.sub('""".*"""', '', getsource(fun), flags=re.DOTALL))
#example
from somewhere import foo
psource(foo)
@rlabbe
rlabbe / gist:f9360578ba48606c2c148392bc802304
Last active October 31, 2016 23:54
3D surface plotting
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
xs = np.linspace(-4, 4, n)
ys = np.linspace(-4, 4, n)
X, Y = np.meshgrid(xs, ys)
# mixture of Gaussians - based on Matlab peaks() function