Skip to content

Instantly share code, notes, and snippets.

@andyhd
andyhd / autoswitch-virtualenv.sh
Created February 28, 2023 14:02
Autoswitch virtualenv on change directory
# autoswitch virtualenv
function cd() {
venv="venv"
if [[ -f .venv ]]; then
if [[ -d "$HOME/.virtualenvs/$(cat .venv)" ]]; then
venv="$HOME/.virtualenvs/$(cat .venv)"
fi
fi
@andyhd
andyhd / prune_ebs_snapshots.go
Created June 6, 2018 14:30
Prune EBS Snapshots refactor
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/sts"
"log"
"os"
@andyhd
andyhd / System Design.md
Created September 20, 2017 15:06 — forked from vasanthk/System Design.md
System Design Cheatsheet

System Design Cheatsheet

Picking the right architecture = Picking the right battles + Managing trade-offs

Basic Steps

  1. Clarify and agree on the scope of the system
  • User cases (description of sequences of events that, taken together, lead to a system doing something useful)
    • Who is going to use it?
    • How are they going to use it?
@andyhd
andyhd / markov.py
Last active April 25, 2016 18:22
Code golf with Markov string generation
import random
def build_lookup(corpus, key_size=2):
lookup = {}
for i, _ in enumerate(corpus):
if i + key_size < len(corpus):
words = lookup.setdefault(tuple(corpus[i:i + key_size]), [])
words.append(corpus[i + key_size])
return lookup

Keybase proof

I hereby claim:

  • I am andyhd on github.
  • I am andyhd (https://keybase.io/andyhd) on keybase.
  • I have a public key whose fingerprint is C289 A2BE B069 56A2 0222 BCA1 C142 9457 4A0A F3C6

To claim this, I am signing this object:

@andyhd
andyhd / with-statement-generator.py
Created July 13, 2012 08:33
Python with statement using generator
@contextlib.contextmanager
def google_search_results(terms, page=1, custom_search_id=''):
params = {
'q': terms,
'cx': custom_search_id or app.config.get('GOOGLE_CUSTOM_SEARCH_ID', ''),
'start': ((page - 1) * 10) + 1,
'num': 10
}
yield custom_search_service.list(**params).execute()
@andyhd
andyhd / env.py
Created July 12, 2012 09:25
Get Alembic working with Flask
from __future__ import with_statement
from alembic import context
from sqlalchemy import engine_from_config, pool
from logging.config import fileConfig
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
@andyhd
andyhd / maybe.php
Created July 2, 2012 14:30
Pseudo Maybe monad in PHP, for the lulz
<?php
/**
* A Maybe Monad class, encapsulates null checking
*/
class Maybe {
private $value;
public function __construct($value=null) {
$this->value = $value;
@andyhd
andyhd / truncate_html.js
Created May 23, 2012 16:01
Tag aware truncation of HTML by words
function truncate_html(html, words_left, depth) {
if (typeof html == 'string') {
html = html.replace(/</g, ' <').replace(/>/g, '> ').trim().split(/\s+/);
}
if (!depth) { depth = 0; }
var opening_tag = /^<\s*\w+[^>]*>/;
var closing_tag = /^<\/\s*\w+[^>]*>/;
var void_tag = /^<\s*(area|base|br|col|command|embed|hr|img|input|keygen|link|meta|param|source|track|wbr)[^>]*>/;
var word = html[0];
if (!word) { return []; }
@andyhd
andyhd / world.js
Created April 18, 2012 09:12 — forked from nefarioustim/world.js
render with one loop
function drawMap(map) {
var x, y,
width = map[0].length,
current_node = map.length * width;
while (current_node--) {
x = current_node % width;
y = ~~(current_node / width);
drawTile({
x: x,
y: y