Skip to content

Instantly share code, notes, and snippets.

View huangsam's full-sized avatar
🌱
Continuous learning

Samuel Huang huangsam

🌱
Continuous learning
View GitHub Profile
@huangsam
huangsam / sql_cascade.py
Last active March 27, 2021 22:05
Play around with SQLAlchemy ORM and Core
from datetime import datetime
from enum import Enum as RegularEnum
from enum import auto
from sqlalchemy import Column, DateTime
from sqlalchemy import Enum as DatabaseEnum
from sqlalchemy import ForeignKey, Integer, String, event
from sqlalchemy.engine import create_engine
from sqlalchemy.orm import declarative_base, relationship, sessionmaker
@huangsam
huangsam / Dictionary.swift
Last active August 15, 2020 23:19
Play around with Swift language
var multFour: [Int: Int] = [:]
for i in 1...3 {
multFour[i] = i * 4
}
typealias AnswerItem = Dictionary<Int, Int>.Element
typealias AnswerTuple = (Int, Int)
let sortedMultFour: [AnswerItem] = multFour.sorted { x, y in x.key < y.key }
@huangsam
huangsam / sarcasm.py
Created June 14, 2020 00:08
Use Tensorflow to detect sarcasm in sentences
# Colab -> https://goo.gle/tfw-sarcembed
# GitHub -> https://goo.gle/2PH90ea
import json
with open("sarcasm.json", "r") as f:
datastore = json.load(f)
sentences = []
labels = []
for item in datastore:
@huangsam
huangsam / main.rs
Last active April 26, 2020 18:51
Show how ownership in Rust works
/// See link more details:
/// https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html
fn main() {
// String is made up of the following:
// - pointer to string contents
// - length
// - capacity
let s1 = String::from("hello");
// immutable transfer from s1 to s2
@huangsam
huangsam / classic.rs
Last active April 27, 2020 15:51
Classic algorithms in Rust
//! A module with classic algorithms from CS 101 classes.
//! Consider using this as a way to learn how Rust works
//! in general while solving safe + simple problems.
/// Returns a `Result` from computing factorial
///
/// # Arguments
///
/// * `n` - An integer
pub fn fact(n: i32) -> Result<i32, i32> {
@huangsam
huangsam / gpull.sh
Created January 31, 2020 04:56
Recursively update Git repositories with a remote origin
# Run `git pull` for every repo under a path
function gpull() {
# shellcheck disable=SC2156
find "${1:-.}" -type d -name ".git" -exec bash -c '
cd {}/../
dir="$(pwd)"
if grep -qs "remote" .git/config; then
echo "= pull $dir"
git pull -q
else
@huangsam
huangsam / approve.py
Last active January 17, 2020 17:38
Testing decorator with parameter(s)
def approve(whitelist):
"""Bind whitelist arg to wrapper"""
def wrapper(func):
"""Bind old func to guard"""
def guard(*args, **kwargs):
"""Check if 'name' keyword matches whitelist"""
name = kwargs.get("name")
if name in whitelist:
print(f"=== approved {name}")
func(*args, **kwargs)
@huangsam
huangsam / app.py
Created October 11, 2019 06:53
Long polling demo with Flask + axios
from datetime import datetime
from random import randint
from time import sleep
from flask import Flask, jsonify, render_template
app = Flask(__name__)
@huangsam
huangsam / ssh_tunnel.sh
Created September 20, 2019 16:19
Bash function for creating a SSH tunnel
function ssh_tunnel() {
target_host="$1"
target_port="$2"
tunnel_host="$3"
source_port="$4"
[[ `ps axu | grep $target_port | grep ssh` ]] && return
ssh -Nf -L "$source_port:$target_host:$target_port" "$tunnel_host"
}
@huangsam
huangsam / kafka-demo.sh
Created April 25, 2019 17:17
Basic operations with Apache Kafka
#!/bin/bash
# kafka-demo.sh: A playground for running Kafka on a Docker container
# https://www.tutorialspoint.com/apache_kafka/apache_kafka_basic_operations.htm
# Initialization step
# openjdk:8 image uses the following software:
# Java 1.8.0_212 and Debian 9 (stretch)
docker run --rm -it --name kafka openjdk:8 bash
# All the commands below are run within the Docker container