Skip to content

Instantly share code, notes, and snippets.

View gubatron's full-sized avatar

Angel Leon gubatron

View GitHub Profile
@gubatron
gubatron / gist:1889173
Created February 23, 2012 02:00
Testing disk I/O speed on a single EBS volume, vs a RAID0 volume made of 2 EBS volumes.
import os
import time
def writeBytes(size,fileName):
start = time.time()
cmd = "dd if=/dev/zero of=" + fileName + " bs=" + size + " count=1 status=noxfer > /dev/null"
print cmd
os.system(cmd)
return time.time() - start
@gubatron
gubatron / gist:1897094
Created February 24, 2012 03:22
How to shuffle a list in javascript in O(n) time using the Fisher-Yates algorithm
/**
* Returns number starting from offset up to n-1
*/
function getRandomWithOffset(n,offset) {
return Math.floor(Math.random()*n+offset);
}
/**
* Returns random integer from 0 to n-1
*/
@gubatron
gubatron / NewsCrawler.java
Created April 19, 2012 02:58
Blast from the past
import java.util.*;
import java.io.*;
import java.util.regex.*;
import java.net.*;
final public class NewsCrawler implements Runnable {
private static int NEWS_PORT = 119;
private static String _newsServer = new String("news.telcel.net.ve");
private static String COMMAND_HEAD = new String("head");
@gubatron
gubatron / post-fixer.php
Created December 13, 2012 02:46
Console PHP Script to process thousands of Wordpress posts. Good examples of how to process posts using the wordpress api without exhausting the interpreter's memory. (turns off the default caching of the api) Examples of updating and adding post metadata (custom fields), setting the featured image of the post (working with galleries), and simpl…
<?php
/**
* Console PHP Script to process thousands of Wordpress posts.
*
* You can use this script as an example on how to:
* > extract urls and put them in the posts metadata.
* > download images referenced by the HTML out on flickr into the post
* > massaging the post HTML
*
* The script makes use of the wordpress api, in order to do this it turns off the
@gubatron
gubatron / gist:5499514
Created May 2, 2013 01:03
Remove trailing slashes on a list or several variables using list comprehension. #python #listComprehension
#let's say you receive several variables and you want none of them to have trailing slashes
backup_name='mysql/'
backup_source_path='/var/lib/mysql/'
backup_destination_path='/mnt/nas/mysql_backups/'
#you can put them in a tuple and use list comprehension to operate on all of them
#and reassign them
(backup_name, backup_source_path, backup_destination_path) = [ x.rstrip('/') for x in (backup_name, backup_source_path, backup_destination_path)]
@gubatron
gubatron / gist:5543946
Created May 8, 2013 21:50
Fixing all the license headers in code recursively, changing "FrostWire(TM)" to "FrostWire(R)" in bash
#first find the files that need to be changed, and output their paths to a text file
#I use 'cut', to trim the ":" left by the grep output.
grep "FrostWire(TM)" * -R | awk {'print $1'} | cut -d ":" -f 1 > tm_replace
#then go through each file path in the text file and perform a search and replace using perl regex
for FILE in `cat tm_replace`; do perl -p -i -e 's/FrostWire\(TM\)/FrostWire\(R\)/g' $FILE; done
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static List<List<Integer>> getDirtyPositions(String[] board) {
List<List<Integer>> dirtyPositions = new ArrayList<List<Integer>>();
function selectText(element) {
var doc = document
, text = doc.getElementById(element)
, range, selection
;
if (doc.body.createTextRange) { //ms
range = doc.body.createTextRange();
range.moveToElementText(text);
range.select();
} else if (window.getSelection) { //all others
@gubatron
gubatron / HistoHashMap.java
Created September 24, 2013 18:34
Whenever you need to implement a histogram this will save time. Just invoke update(K key) to add one more ocurrence to the underlying map. When you want the histogram in ascending order, invoke histogram() and you will get all the entries and number of ocurrences in a Entry<K,Integer> array.
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class HistoHashMap<K> {
private final Map<K,Integer> map = new HashMap<K, Integer>();
@gubatron
gubatron / gist:6943884
Created October 11, 2013 23:59
how to check the changelog format for a .deb package
dpkg-parsechangelog -lchangelog