Skip to content

Instantly share code, notes, and snippets.

@georgepsarakis
georgepsarakis / parallel-merge-sort.py
Last active November 6, 2020 12:56
Parallel Merge Sort - Example Code & Benchmark
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 August 29, 2015 14:20
Python small recipes translated to their Clojure equivalents
;; 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 October 28, 2014 09:16
Simple SQLite-backed key-value storage Rest API. Built with Flask & flask-restful.
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 January 4, 2016 11:48
Extended version of the argparse Python module for command line option parsing.
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 February 22, 2021 11:27
Github repositories & Gists backup script
#!/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 December 15, 2013 17:18
Map, filter with generator for numerically-indexed and associative arrays. Inspired from Python itertools - http://docs.python.org/2/library/itertools.html.
<?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 December 12, 2013 08:17
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.
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 December 29, 2015 22:59
Python C Module example: building a simplistic hash table with the help of some Redis libraries (https://github.com/antirez/redis - http://redis.io/).
/*
- 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
@georgepsarakis
georgepsarakis / cron-file-syntax-check.py
Created November 28, 2013 09:39
Crontab file syntax check
#!/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 December 28, 2015 19:49
REPL-CLI boilerplate for simple DSLs with built-in help, autocomplete (like Redis-CLI)
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import argparse
import readline
import rlcompleter
import shlex
import os
import re
try: