This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\":: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' 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) | |
''' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from inspect import getsource | |
import re | |
def psource(fun): | |
print(re.sub('""".*"""', '', getsource(fun), flags=re.DOTALL)) | |
#example | |
from somewhere import foo | |
psource(foo) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
NewerOlder