Skip to content

Instantly share code, notes, and snippets.

@eladkehat
eladkehat / py3_script_template.py
Created September 21, 2019 18:22
Template (outline) for a Python3 script.
#!/usr/bin/env python3
"""Python3 script template."""
import argparse
def run(args):
pass
def parse_arguments():
@eladkehat
eladkehat / mummer_all_vs_all.py
Last active June 15, 2017 11:10
Run MUMmer on every pair of files from a list and plot the result. Fixes a bug in their gnuplot script.
import argparse
import itertools
import os
import subprocess
"""
Run MUMmer on every pair of files from the list of input files.
"""
# Default path to the MUMmer binary
@eladkehat
eladkehat / add-my-ip-to-aws-ssh-sg.sh
Created July 20, 2016 06:58
This script adds your current IP address to the security group named SSH on AWS. Useful for the security conscious among us who want to limit SSH access to their EC2 instances by IP address but don't want to get into the AWS console any time their home IP changes.
#!/usr/bin/env bash
# Requirements:
# * The AWS CLI should be installed: https://aws.amazon.com/cli/
# * A credentials file has the correct account credentials configured as default (see the CLI reference)
# * There exists a security group in the default region whose name is SSH
MYIP=$(curl -s http://checkip.amazonaws.com/)
#echo "Current IP is: $MYIP"
@eladkehat
eladkehat / alexatopsites.rb
Created October 4, 2012 09:16
Get the list of top sites for a given country from Alexa
# Get the list of top sites for a given country from Alexa, found here:
# http://www.alexa.com/topsites/countries/<country code>
# and transform it into a nice text file, one domain per line.
# Supply the country code as the first argument. Defaults to US.
require 'rubygems'
require 'open-uri'
require 'nokogiri'
BASE_URL = "http://www.alexa.com/topsites/countries"
country = ARGV[0] || "US"
@eladkehat
eladkehat / aws.rb
Created December 24, 2011 07:56
Use S3 (via the aws gem) in Rails to store user files such as images
# Place this file in config/initializers
# load the libraries
require 'aws'
# log requests using the default rails logger
AWS.config(:logger => Rails.logger)
# here's how to load credentials from a file
#config_path = File.expand_path(File.dirname(__FILE__)+"/../aws.yml")
#AWS.config(YAML.load(File.read(config_path)))
@eladkehat
eladkehat / sinatra_google_contacts_oauth.rb
Created December 20, 2011 21:29
Sinatra code that gets your Google contacts through OAuth. Wrote this because OmniAuth was giving me trouble with the contacts data api scope for some reason, so I decided to go 'raw'.
require 'sinatra'
require 'net/http'
require 'json'
ENDPOINT = 'https://accounts.google.com/o/oauth2/auth'
TOKEN_ENDPOINT = 'https://accounts.google.com/o/oauth2/token'
CLIENT_ID = '<your app client id>'
CLIENT_SECRET = '<your app client secret>'
REDIRECT_URI = 'http://localhost:4567/auth/google_oauth2/callback'
SCOPE = 'https://www.google.com/m8/feeds/'
@eladkehat
eladkehat / retryable.rb
Created November 28, 2011 18:07
Utility method that retries a block until it receives some expected result, with some other nifty options
# Retry the block several times, with a timeout between retries
# Options:
# times - maximum number of retries
# timeout - timeout betwen retries, in seconds
# break_on - will break the rerty loop given that result from the block
# if break_on is a lambda, it will call it with the block result and break if it returns true
# @return the block's result
def retryable(options = {}, &block)
opts = {times:3, timeout:60, break_on:false}.merge(options)
(1...opts[:times]).each do |i|
@eladkehat
eladkehat / gist:791014
Created January 22, 2011 09:41
Arduino sketch that uses a pushbutton to fade a LED in and out. On every button release the fader direction (in or out) is switched. See it working here: http://www.youtube.com/watch?v=osDDfbNOaM0
#define BUTTON 7
#define LED 9
int dir = 1;
int lum = 0;
boolean toggle = false;
void setup() {
pinMode(BUTTON, INPUT);
pinMode(LED, OUTPUT);
#!/usr/bin/env ruby
# Explodes a minified css file, e.g. using YUI Compressor (http://developer.yahoo.com/yui/compressor/)
# Minified css (unlike obfuscated javascript) is still easily readable if only the spaces, newlines and
# indentations are put back in. This little ruby script does this with some simple regular expressions.
fn = ARGV[0]
css = IO.read fn
replacements = [ [/\s*\{/, " {\n\t"], [/;/, ";\n\t"], [/\t?\}/, "}\n\n"], ]
# An implementation of the Damerau–Levenshtein algorithm for string distance
# See: http://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance
# Also based on: http://en.wikibooks.org/wiki/Algorithm_implementation/Strings/Levenshtein_distance#Ruby
module DamerauLevenshtein
def self.distance(a, b)
return b.length if a.empty?
return a.length if b.empty?