Skip to content

Instantly share code, notes, and snippets.

View sochoa's full-sized avatar

Sean Ochoa sochoa

  • Seattle, WA USA
View GitHub Profile
@sochoa
sochoa / ping.sh
Created July 6, 2012 04:55
Pinging a Google DNS server to confirm internet connectivity
#!/bin/bash
host=8.8.8.8
while :
do
echo "Pinging $host"
ping_result=$(ping $host -c 1 -n -q | egrep "[0-9]+% packet loss" -o | sed 's/% packet loss//g')
if [[ ! $ping_result ]]; then
clear
date
echo -en "\n\n$host could not be reached"
@sochoa
sochoa / gist:3846128
Created October 6, 2012 21:06
Backbone.js > Invalid URL Redirect
if (/^\/.+/.test(window.location.pathname)) {
if (!/^(\/#)/.test(window.location.pathname)) {
console.error("Invalid URL: " + window.location);
window.location = '/#' + window.location.pathname;
}
}
#!/usr/bin/env ruby
for i in 0..100
if i % 15 == 0
print " fizzbuzz "
elsif i % 5 == 0
print " buzz "
elsif i % 3 == 0
print " fizz "
else
@sochoa
sochoa / reverse.rb
Created January 21, 2013 08:04
Could I have done this better, in ruby?
#!/usr/bin/env ruby
require 'trollop'
opts = Trollop::options do
banner <<-EOS
Reverses a string.
Usage:
ruby reverse.rb [options]
Options:
@sochoa
sochoa / yum-cli.utils.py
Created July 4, 2013 00:49
Pure awesomeness
def exception2msg(e):
""" DIE python DIE! Which one works:
to_unicode(e.value); unicode(e); str(e);
Call this so you don't have to care. """
# ... some stuff
@sochoa
sochoa / to_markdown.sh
Last active December 30, 2015 16:18
I added a function to my bashrc to render to markdown that I could reuse in Vim. :)
#!/bin/bash
# I renamed Markdown.pl to markdown for simpler typing.
function to_markdown() {
local src="${1}"
if [ -z "${src}" ]; then
echo "no source"
return
fi
@sochoa
sochoa / matches.py
Created December 28, 2013 05:03
Returning a basic type for each attempt at a regex match. Also memoizing the calls to the re module for speed.
import re
def memoize(f):
class memodict(dict):
def __init__(self, f):
self.f = f
def __call__(self, *args):
return self[args]
def __missing__(self, key):
ret = self[key] = self.f(*key)
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class
public class HelloWorld extends HttpServlet {
private String message;
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
#include <iostream>
#include <fstream>
#include <exception>
#include <vector>
int main(const int argc, const char** argv) {
const int UNSET = -1;
const int BEGINNING = 0;
const std::string DELIMITER = " ";