Skip to content

Instantly share code, notes, and snippets.

George Psarakis georgepsarakis

Block or report user

Report or block georgepsarakis

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@georgepsarakis
georgepsarakis / python-to-clojure-guide.clj
Last active Aug 29, 2015
Python small recipes translated to their Clojure equivalents
View python-to-clojure-guide.clj
;; Regular Expressions
;; import re; re.search(r'ab.*', 'cabbage').group(0) # -> 'abbage'
(re-find #"ab.*" "cabbage") ; "abbage"
;; re.match(r'ab.*', 'cabbage') # -> None
(re-matches #"ab.*" "cabbage") ; nil
;; re.match(r'ab.*', 'abracatabra').group(0) # -> 'abracatabra'
(re-matches #"ab.*" "abracatabra") ; "abracatabra"
;; Sequences & Map/Filter
@georgepsarakis
georgepsarakis / mysql-triggers.rb
Last active Dec 13, 2015
MySQL Trigger Command Line Editor in Ruby
View mysql-triggers.rb
#!/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 Mar 3, 2013
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.
View split.sql
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 Jul 8, 2013
Mathematical & Random Numbers MySQL Functions
View math_and_random.sql
-- 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 Dec 20, 2015
Python random helper functions
View random_string.py
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 Aug 13, 2013
Function to return memory consumption for the script itself.
View memory_monitor.py
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 Dec 25, 2015
JS String methods
View string.js
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 Dec 25, 2015
Extra-Lightweight HTML/Markdown Editor (jQuery Plugin)
View lighter.js
/*
<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 Oct 28, 2013
Probability counter (binary)
View probability-counter.py
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 Dec 27, 2015
Redis Emulated Transaction (Python & redis-py)
View redis-transaction.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
You can’t perform that action at this time.