Instantly share code, notes, and snippets.

@georgepsarakis
georgepsarakis / parallel-merge-sort.py
Last active Dec 9, 2018
Parallel Merge Sort - Example Code & Benchmark
View parallel-merge-sort.py
from __future__ import print_function
import random
import sys
import time
from contextlib import contextmanager
from multiprocessing import Manager, Pool
class Timer(object):
"""
@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 / sqlite-kv-restful.py
Created Oct 28, 2014
Simple SQLite-backed key-value storage Rest API. Built with Flask & flask-restful.
View sqlite-kv-restful.py
import os
import sqlite3
from hashlib import md5
from time import time
import simplejson as json
from flask import Flask
from flask.ext import restful
from flask import g
from flask import request
@georgepsarakis
georgepsarakis / options.py
Last active Jan 4, 2016
Extended version of the argparse Python module for command line option parsing.
View options.py
from __future__ import unicode_literals
import re
import argparse
from functools import partial
class Options(argparse.ArgumentParser):
def __init__(self, **kwargs):
self._parsed = False
self._options = None
@georgepsarakis
georgepsarakis / github-repos-gists-backup.py
Last active Dec 3, 2017
Github repositories & Gists backup script
View github-repos-gists-backup.py
#!/usr/bin/python
import os
import json
import argparse
import time
import requests
from functools import partial
class GithubBackup(object):
def __init__(self, parameters):
@georgepsarakis
georgepsarakis / itertools.php
Created Dec 15, 2013
Map, filter with generator for numerically-indexed and associative arrays. Inspired from Python itertools - http://docs.python.org/2/library/itertools.html.
View itertools.php
<?php
class Generator implements Iterator {
private $index = 0;
private $array = array();
private $keys = array();
private $filter = NULL;
private $mapper = NULL;
private $size = 0;
private $seek = 0;
private $last = NULL;
@georgepsarakis
georgepsarakis / extended-list.py
Created Dec 12, 2013
Extended list. Maintains a dictionary for faster lookups (optionally keeps hash of item object representation by marshalling) and with some operator overloading can perform mathematical operations among instances.
View extended-list.py
from collections import MutableSequence
import marshal
from operator import add, div, mul, neg, mod, sub
from operator import ge, gt, le, lt, eq
from itertools import imap
'''
Extended list class
- fast lookups by maintaining a dictionary with the values
- numeric operations between 2 instances are possible (like Octave matrices)
@georgepsarakis
georgepsarakis / python-c-module-example.c
Last active Dec 29, 2015
Python C Module example: building a simplistic hash table with the help of some Redis libraries (https://github.com/antirez/redis - http://redis.io/).
View python-c-module-example.c
/*
- setup.py
from distutils.core import setup, Extension
module = Extension('ht', sources = ['zmalloc.c', 'sds.c', 'dict.c', 'python-c-module-example.c'])
setup(name = 'Hash Table', version = '1.0', ext_modules = [module])
- install.sh
# Fetch dependencies from https://github.com/antirez/redis
wget https://raw.github.com/antirez/redis/2.6/src/fmacros.h
wget https://raw.github.com/antirez/redis/2.6/src/config.h
View cron-file-syntax-check.py
#!/usr/bin/python
import argparse
import os
import re
'''
**** Crontab file syntax checking ***
Useful for scripts stored under /etc/cron.d
and for automating crontab task distribution
'''
@georgepsarakis
georgepsarakis / cli.py
Last active Dec 28, 2015
REPL-CLI boilerplate for simple DSLs with built-in help, autocomplete (like Redis-CLI)
View cli.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import argparse
import readline
import rlcompleter
import shlex
import os
import re
try: