Skip to content

Instantly share code, notes, and snippets.

@georgepsarakis
georgepsarakis / mysql-triggers.rb
Last active December 13, 2015 20:58
MySQL Trigger Command Line Editor in Ruby
#!/usr/bin/ruby
require 'rubygems'
require 'optparse'
require 'ostruct'
# >> Helper functions
# deep copy
def dcopy(o)
return Marshal.load(Marshal.dump(o))
@georgepsarakis
georgepsarakis / split.sql
Created March 3, 2013 21:02
Split (explode) function for MySQL. Simple version requires 3 arguments, S (the delimited string), DELIM (delimiter) and S_INDEX the index of the instance of substring between delimiters. The LTSPLIT, RTSPLIT & TSPLIT wrappers perform trimming on the returned substring, on leading, trailing & both characters respectively.
DROP FUNCTION IF EXISTS SPLIT;
DROP FUNCTION IF EXISTS _SPLIT;
DROP FUNCTION IF EXISTS RTSPLIT;
DROP FUNCTION IF EXISTS LTSPLIT;
DELIMITER //
-- Simple Split (no trim)
CREATE FUNCTION SPLIT(S CHAR(255), DELIM VARCHAR(30), S_INDEX TINYINT UNSIGNED) RETURNS VARCHAR(255)
BEGIN
RETURN _SPLIT(S, DELIM, S_INDEX, '', 0);
@georgepsarakis
georgepsarakis / math_and_random.sql
Created July 8, 2013 18:45
Mathematical & Random Numbers MySQL Functions
-- Random integer between 2 signed integers
DROP FUNCTION IF EXISTS RANDINT;
DELIMITER //
CREATE FUNCTION RANDINT(a INT, b INT) RETURNS INT
BEGIN
RETURN FLOOR( ABS(a-b) * RAND() ) + IF(a > b, b, a);
END//
@georgepsarakis
georgepsarakis / random_string.py
Last active December 20, 2015 21:09
Python random helper functions
from sys import maxint
from random import choice, sample, shuffle
from string import ascii_uppercase as U, ascii_lowercase as L, digits as D, punctuation as P
def populate(punctuation, extra):
chars = U + L + D
if isinstance(extra, basestring):
chars += extra
if punctuation:
chars += P
@georgepsarakis
georgepsarakis / memory_monitor.py
Created August 13, 2013 10:33
Function to return memory consumption for the script itself.
from resource import getrusage, RUSAGE_SELF
def monitor(unit="M", alert=None):
unit = unit.upper()
unit_transformation = {
"B" : 1./1024.,
"K" : 1.,
"M" : 1024.,
"G" : 1024.*1024.,
}
@georgepsarakis
georgepsarakis / string.js
Last active December 25, 2015 06:29
JS String methods
String.prototype.sub = function(regex, replacement){
var s = this.toString();
var matches = s.match(regex);
if ( matches !== null ){
for(i in matches){
var item = matches[i];
if ( !item.escaped() ){
s = s.replace(item, replacement);
}
}
@georgepsarakis
georgepsarakis / lighter.js
Last active December 25, 2015 20:39
Extra-Lightweight HTML/Markdown Editor (jQuery Plugin)
/*
<div id="editor"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<style>
.clearfix:after {
clear: both;
}
.clearfix:before, .clearfix:after {
content: " ";
display: table;
@georgepsarakis
georgepsarakis / probability-counter.py
Created October 28, 2013 10:59
Probability counter (binary)
from random import getrandbits
def probability_counter(counter=None):
if counter is None:
counter = 1
counter += int((getrandbits(counter) + 1) >> counter == 1)
return counter
@georgepsarakis
georgepsarakis / redis-transaction.py
Last active December 27, 2015 01:29
Redis Emulated Transaction (Python & redis-py)
import redis
from operator import methodcaller
class RedisTransaction(object):
def __init__(self, **kwargs):
''' Transaction timeout sets TTL for copied keys '''
if 'timeout' in kwargs:
self.TRANSACTION_TIMEOUT = kwargs['timeout']
else:
self.TRANSACTION_TIMEOUT = 10
@georgepsarakis
georgepsarakis / templater.php
Last active December 28, 2015 11:59
PHP Templating
<?php
class T {
protected $Context = array();
protected $Compiled = NULL;
public $Regex_Variable = '~\{\{\s+[a-z_0-9\|:\.]+\s+\}\}~imsu';
public $Separator_Filter = '|';
public $Separator_Directive = ':';
protected $HTML_Encoder = NULL;
protected $HTML_ENCODE = TRUE;
protected $VALUE_CACHE = array();