Skip to content

Instantly share code, notes, and snippets.

View tamsanh's full-sized avatar

Tam Nguyen tamsanh

View GitHub Profile
@tamsanh
tamsanh / check_git.sh
Created January 20, 2016 11:58
Don't run if git is dirty
ITERATING=0
MASTER_ONLY=0
if [ $ITERATING -ne 1 ]
then
git diff-index --quiet HEAD
if [ $? -ne 0 ]
then
echo "!!! There are untracked changes. You must commit before running this script."
echo "Will not allow running until you resolve the following files:"
echo
# Argument Parser that prints its help message by default
import argparse
import sys
class HelpDefaultParser(argparse.ArgumentParser):
def error(self, message):
sys.stderr.write('error: %s\n' % message)
self.print_help()
sys.exit(2)
#!/bin/sh
for arg; do
realarg="$(realpath "$arg")"
case "$realarg" in
/|/usr|/var|/etc|/home|/bin|/lib|/lib64|/boot|/opt|/media|/root)
echo "refusing to remove $realarg" 1>&2
exit 100
;;
@tamsanh
tamsanh / hlog.sh
Last active December 17, 2019 00:29
A leanly formatted git graph for understanding repos.
# Place this in your bashrc
function hlog {
git log --date-order --all --graph --format="%C(green)%h %Creset%C(yellow)%an%Creset %C(blue bold)%ar%Creset %C(red bold)%d%Creset %s" $@
}
alias hl="hlog"
@tamsanh
tamsanh / slack-interactive-message-request.js
Last active August 13, 2017 02:47
Sending an Interactive Message on Slack with NodeJS and request with a Developer Preview Token
const request = require('request');
const SLACK_TOKEN = "INSERT-TOKEN";
const TARGET_CHANNEL = "INSERT-CHANNEL"; // Ex @user, general
const data = {
"token": SLACK_TOKEN,
"channel": TARGET_CHANNEL,
"attachments": JSON.stringify([
{
@tamsanh
tamsanh / spark-notes.md
Last active February 15, 2018 23:55
Notes from Installing Spark on AWS Linux

Using PySpark Spark 2.2.1

Transform PySparkSQL Column from String to Date

from pyspark.sql.types import TimestampType
data = data.withColumn('status_updated', data['status_updated'].cast(TimestampType()))

Installing Spark on AWS Linux

Errors

@tamsanh
tamsanh / japan-spots.md
Last active December 30, 2018 22:20
Tamu's List of Japan Spots

Pro Tips

  1. Avoid eating at convenience stores as much as possible. The food is good, but you can get similarly priced food that is much better at any restaurant you might walk into.
  2. Bring cash. Credit cards only became a thing about 6 years ago.
  3. Police officers have very little to do, so if you're lost, use them for directions.

Tokyo

Food

@tamsanh
tamsanh / human_file_size.py
Last active March 20, 2018 03:28
Get a human readable filesize of the output
from __future__ import print
# Adopted from
# https://web.archive.org/web/20111010015624/http://blogmag.net/blog/read/38/Print_human_readable_file_size
# https://stackoverflow.com/questions/1094841/reusable-library-to-get-human-readable-version-of-file-size
def sizeof_fmt(num, suffix='B'):
for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']:
if abs(num) < 1024.0:
return "%3.1f%s%s" % (num, unit, suffix)
@tamsanh
tamsanh / directory_word_counter.py
Created March 25, 2018 00:21
Count words in all files in a given directory, either recursively or non-recursively.
import re
import io
import os
import csv
from glob import glob
DEFAULT_OUTPUT_NAME = 'word-counts.csv'
@tamsanh
tamsanh / jupyter_cell_notify.py
Created April 6, 2018 03:18
Generate and play a sound in Jupyter Notebook when then cell is run. Useful as notification when a long-running cell is done.
from IPython.lib.display import Audio
import numpy as np
framerate = 4410
play_time_seconds = 3
t = np.linspace(0, play_time_seconds, framerate*play_time_seconds)
audio_data = np.sin(2*np.pi*300*t) + np.sin(2*np.pi*240*t)
Audio(audio_data, rate=framerate, autoplay=True)