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
#!/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 / 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 / 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 / get_calling_file_name.py
Created April 12, 2018 00:11
Gets the name of the calling file.
def get_calling_file_name():
# If this function is embedded in another function, offset the stack appropriately
import inspect
stack_offset = 1
stack = inspect.stack()
calling_file = stack[stack_offset][1]
return calling_file
@tamsanh
tamsanh / virtualenv_venv_quick_setup_macosx.sh
Last active September 24, 2018 21:48
virtualenv venv Quick Setup MacOSX - Python 3
# Instructions for setting up a virtualenv venv
#########
# SETUP #
#########
## Make sure you have python3
python3 --version
@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 / .ssh_config
Created June 4, 2018 02:10
Settings file to specify a specific host for a different ssh key.
Host target-repo-name.github.com
HostName github.com
User git
IdentityFile /home/ec2-user/.ssh/target-repo-name
IdentitiesOnly yes