Skip to content

Instantly share code, notes, and snippets.

View thelahunginjeet's full-sized avatar

Kevin Brown thelahunginjeet

View GitHub Profile
@thelahunginjeet
thelahunginjeet / recdel.sh
Created July 11, 2019 15:06
recursive file delete
find . -type f -name 'myfiles.*' -delete
@thelahunginjeet
thelahunginjeet / profile.py
Created July 30, 2015 15:16
example usage of cProfile module
import pstats,cProfile
import my_function
cProfile.runctx("my_function(a,b)",globals(),locals(),"Profile.prof")
s = pstats.Stats("Profile.prof")
s.strip_dirs().sort_stats("time").print_stats()
@thelahunginjeet
thelahunginjeet / cleanwhite.py
Created July 21, 2015 18:25
python idiom for cleaning up lines with inconsistent whitespace
def clean_whitespace(s):
'''
Returns a version of s with no leading or trailing whitespace/\t/\n and one whitespace
between each whitespace-separated token in the string.
Ex: ' a b c d \n' -> 'a b c d'
'''
return ' '.join(l.strip().split())
@thelahunginjeet
thelahunginjeet / timeparse.py
Created June 26, 2015 15:52
Simple translator between 'mm/dd/yyyy hh:mm:ss' object and python datetime object.
import datetime as dt
def date_parser(datestring):
"""
Accepts a datestring in the format:
mm/dd/yyyy hh:mm:ss
and returns a python datetime object with all relevant fields properly set.
"""
mdy = [int(x) for x in datestring.split(' ')[0].split('/')]
hms = [int(x) for x in datestring.split(' ')[1].split(':')]
@thelahunginjeet
thelahunginjeet / timer.py
Created July 25, 2014 18:32
simple python timer
import time
class Timer:
def __enter__(self):
self.start = time.clock()
return self
def __exit__(self,*args):
self.end = time.clock()
self.interval = self.end - self.start
@thelahunginjeet
thelahunginjeet / scripting-fish
Last active August 29, 2015 14:02
example fish shell script that chews through multiple .cfg files for a utility that requires them
for f in *.cfg
echo 'Working on' $f
set out (basename $f .cfg).out
set err (basename $f .cfg).err
<myprog> --config=$f >$out ^$err
python process_output.py -input=$out -output=results.dat
end
@thelahunginjeet
thelahunginjeet / redirect-fish
Last active August 29, 2015 14:01
redirect of stdout/stderr in fish shell
# write
mycommand >stdout ^stderr
# append
mycommand >>stdout ^^stderr
@thelahunginjeet
thelahunginjeet / python_array_args_job.sh
Last active July 19, 2018 10:51
example LSF array job showing how to use node-specific inputs and outputs
#!/bin/bash
#BSUB -L /bin/bash
#BSUB -J paj[1-4]
#BSUB -e paj.%I.err
#BSUB -o paj.%I.out
#BSUB -n 1
source ~/venv/bin/activate
python parse_command_line.py --config=conf_${LSB_JOBINDEX}.cfg --output=results_${LSB_JOBINDEX}.out
@thelahunginjeet
thelahunginjeet / parse_command_line.py
Last active August 29, 2015 13:58
basic example of python parsing of command-line arguments
#!/usr/bin/python
import sys, getopt
def config_file_parser(configfile):
"""
Very basic code for reading a simple config file and returning
a dictionary of options.
"""
config = {}
@thelahunginjeet
thelahunginjeet / pip_alternate.sh
Created March 28, 2014 20:14
python package installation using pip, to the distutils "user" location
pip install --install-option="--user" package_name