Skip to content

Instantly share code, notes, and snippets.

View kbfreder's full-sized avatar

Kendra kbfreder

View GitHub Profile
@kbfreder
kbfreder / .bash_profile
Created June 21, 2022 21:46
misc .bash_profile
# to change the username displayed at the bash prompt
PS1="kendra@\W $ "
# functions
function path(){ # nicely list all dir in path
old=$IFS # rather than separated by ':', listed 1 per line
IFS=:
printf "%s\n" $PATH
IFS=$old
# this is intended to live as a function inside ~/.bash_profile
function git_del_branches {
pattern=$1
im=$2
usage="git_del_branches -p pattern [-i]
Match and delete git branches that match <pattern>.
Use the -i (--inverse) flag to delete branches that do
not match <pattern>"
@kbfreder
kbfreder / execute_shell_in_python.py
Last active April 20, 2021 18:15
executing shell commands in python
# ref: https://janakiev.com/blog/python-shell-commands/
# 1. using os
import os
output = os.system('ls -l')
print(output)
# 2. using subprocess
import subprocess
@kbfreder
kbfreder / activate_this.py
Created August 4, 2020 19:39
after you make a virtualenv --relocatable, you need this file
!#python
"""
After you make a virtualenv relocatable (https://pypi.org/project/virtualenv/1.3.3/#making-environments-relocatable), pip will execute a file called 'activate_this.py' in <ENV_NAME>/bin if you go to install another module. In Python3 (? not sure of the cause), this file doesn't get created, so you have to make it. Through trial & error (no copy-and-paste correct answer was out there), I got this code to work.
"""
import os
activator = <path to activate_this.py> # ex: '/home/kfrederick/repos/domain_twists/env/bin/activate_this.py'
virtual_env = <path to virutal env> # ex: '/home/kfrederick/repos/domain_twists/env'
@kbfreder
kbfreder / keybase.md
Created June 9, 2020 16:03
Keybase gist

Keybase proof

I hereby claim:

  • I am kbfreder on github.
  • I am kbfreder (https://keybase.io/kbfreder) on keybase.
  • I have a public key ASBmBZ75k43sulYdxxEkyvL5njbPuAE2l01Q_wSlTIwwRQo

To claim this, I am signing this object:

@kbfreder
kbfreder / bash_args.sh
Last active October 7, 2020 20:44
checks for arguments; assigns them to variables if present
#!/bin/bash
#--------------------------------------
# if no argument given, 'd' defaults to yesterday's date
# the `-z` argument checks for an empty string
if [ -z "$1" ]
then
echo "No argument supplied; using yesterday's date"
d=$(date --date '-1 day' +%Y-%m-%d)
else
@kbfreder
kbfreder / argparse.py
Last active March 10, 2020 20:21
because who doesn't love a good script
import argparse
import config_file as cfg # a config file
# i like to have 'main' as a stand-alone function, so I have the option
# of importing this file as a module, and calling main directly
def main(*args):
# in which we do the things
print('Running main function')
@kbfreder
kbfreder / bash_loop_dates.sh
Last active March 12, 2020 22:12
loop through dates in a bash script. Also uses a while loop, and if and not/then statements
#!/bin/bash
# loop through dates, using GNU dates
# GNU date reference: https://www.gnu.org/software/coreutils/manual/html_node/Examples-of-date.html
# define paths
open_path=../data/raw
save_path=../data/processed
# define dates
start_date=2020-02-25
@kbfreder
kbfreder / timing.py
Last active February 14, 2020 20:17
time how long your code takes to run
# BASIC
# ----------------------------------------------------------------------
# elapsed time
import time
start_time = time.time()
'''code to execute'''
end_time = time.time()
print('Elapsed time: {:.1f} min'.format((end_time - start_time) / 60))
@kbfreder
kbfreder / logging.py
Last active February 26, 2021 15:01
basic logging
import logging
# REFERENCES
# logging message attributes: https://docs.python.org/3/library/logging.html#logrecord-attributes
# datetime formatting: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
# ---------------------------------------------
# BASIC LOGGING
# ---------------------------------------------