Skip to content

Instantly share code, notes, and snippets.

@TheEmpty
TheEmpty / PHP MySQL random row.php
Created October 23, 2010 23:55
This script uses MySQL table standards and will effectively pull multipliable random rows in one query.
function getRandomMySQLRows($table, $rows, $fields = "*"){
// $table = MySQL table
// $rows = number of rows to randomly select
// $fields = fields to select separated by a comma
// returns a string with the SQL that needs to be preformed
if($rows == 0){ return ""; };
$result = mysql_query("SELECT MAX(id) FROM $table WHERE 1");
$count = mysql_result($result,0,0);
@TheEmpty
TheEmpty / qualification_mask.rb
Created March 8, 2012 20:51
Bit Mask Helper for ActiveRecord
module QualificationMask
def bitmask_field_for(field, options)
array = options.delete(:with).to_s
field = field.to_s
methods = field.gsub /_mask/, ''
class_eval %(
def #{methods}=(#{methods})
#{methods} = [#{methods}] if not #{methods}.is_a?(Array)
self.#{field} = (#{methods} & #{array}).map { |r| 2**#{array}.index(r) }.sum
@TheEmpty
TheEmpty / heroku-deploy.sh
Created March 10, 2012 00:22
Heroku Deploy
#!/bin/bash
ENV='staging'
BRANCH='master'
GOT=`git status`
EXPECTED="# On branch master
nothing to commit (working directory clean)"
if [ "$GOT" != "$EXPECTED" ]
@TheEmpty
TheEmpty / ControllerFormAttributes.rb
Created March 11, 2012 02:23
Trying some different ways to secure a Rails application with user input
module ControllerFormAttributes
@@types = {}
# form_params_accessors(:user, :email, :password, :password_confirmation)
def form_params_accessors(type, attributes)
@@types[type] = attributes
end
# user.update_attributes(form_params_for(:user))
def form_params_for(type)
@TheEmpty
TheEmpty / Substrings.py
Last active December 16, 2015 07:08
Untitled 3
# iPad
import console
console.clear()
# Program
def maxSub(a, b):
table = {}
# Base cases, Empty strings
for i in range(0, len(a) + 1):
@TheEmpty
TheEmpty / main.py
Last active December 16, 2015 11:28
Python PGP (attempt)
import pgp
print("Generating keys...")
p = pgp.generateLargePrime(1024)
q = pgp.generateLargePrime(1024)
mod = p * q
print("Generating public key...")
public = pgp.generatePublic(p, q)
print("Generating private key...")
private = pgp.generatePrivate(p, q, public)
@TheEmpty
TheEmpty / crypt.py
Last active December 16, 2015 11:28
PGP Example (using static values)
def crypt(mod, key, data, hashes = 50):
encrypted = []
for v in data:
for i in range(0, hashes + 1):
v = (v ** key) % mod
encrypted.append(v)
return list(map(int, encrypted))
# My keys (p and q are intermediates)
p = 11
@TheEmpty
TheEmpty / Output
Last active December 26, 2015 17:08
Quick Sort in Erlang
$ erl
1> c(misc_lib). % compile module
{ok,misc_lib}
2> misc_lib:quickSort([1, 9, 6, 3, 0, 5, 2, 5082, 5, 2]).
[0,1,2,2,3,5,5,6,9,5082]
@TheEmpty
TheEmpty / gist:7258361
Last active December 27, 2015 03:19
Color fading for Java UI
int[] rgb = {0,0,0};
int i = 0; // current working index
int step = 25;
while(true) {
// so raise R, raise G, drop R, raise B, drop G, etc?
int p = (i + 2) % 3; // previous index
if(rgb[i] >= 255 && rgb[p] > 0) {
rgb[p] -= step;
if(rgb[p] < 0) rgb[p] = 0;
public class AnExample {
public static void main(String[] args) {
System.out.println(
ColoredString.s("Such ANSI, Easy Color. Many wow.").bold().cyan().blink();
);
}
}