Skip to content

Instantly share code, notes, and snippets.

@benkrikler
benkrikler / edit_path.bash
Last active February 26, 2020 10:29
Bash function to open a given "PATH" variable (PATH, LD_LIBRARY_PATH, PYTHONPATH, etc) in an editor with each field on one line, then reassemble the path with colon separators
edit_path ()
{
local tmpfile=$(mktemp);
local variable="${1:-PATH}";
tr ':' '\n' <<< "${!variable}" > "$tmpfile";
"$EDITOR" "$tmpfile";
export ${variable}="$(tr '\n' : < "$tmpfile"| sed -e 's/:$//' )"
}
@benkrikler
benkrikler / explode_wide.py
Created October 29, 2019 12:47
pandas dataframe with lists in cells to a wide-form one-item-per-column
def flatten_to_wideform(df, mask_column=None, max_objects=None):
# get the list columns
lst_cols = [col for col, dtype in df.dtypes.items()
if pd.api.types.is_object_dtype(dtype)
and df[col].head(10).apply(lambda x: isinstance(x, (list, np.ndarray, tuple))).all()
]
if not lst_cols:
return df
# all columns except `lst_cols`
import pandas as pd
_interval_regex = r"^(?P<open>[[(])"
_interval_regex += r"(?P<low>-inf|[0-9][.0-9]*)"
_interval_regex += r"\s*,\s*"
_interval_regex += r"(?P<high>\+?inf|[0-9][.0-9]*)"
_interval_regex += r"(?P<close>[)\]])$"
import numpy as np
class MaskedUprootTree():
def __init__(self, tree, mask=None):
if isinstance(tree, MaskedUprootTree):
self.tree = tree.tree
self._mask = tree._mask
else:
self.tree = tree
from math import pi, cos, sin, sinh, sqrt, asinh, atan2, fabs
class LorentzVector(object):
def __init__(self, x, y, z, T, r2=None):
self.x = x
self.y = y
self.z = z
self.T = T
if r2:
#! /usr/bin/env python
import tarfile
import re
import os
import subprocess
def find_tar_files(top_dir, exclude):
tar_files = []
for root, dirs, files in os.walk(top_dir):