Skip to content

Instantly share code, notes, and snippets.

@narma
narma / pyhon_realgroupby.py
Created July 19, 2010 14:37
Real groupby of python itertools
def realgroupby(it, key):
from itertools import groupby, chain
d = {}
for k, ii in groupby(it, key):
if k in d:
d[k] = chain(d[k], list(ii))
else:
d[k] = list(ii)
return d
@narma
narma / gist:858372
Last active January 4, 2023 15:09
Why ruby is better than python
", ".join(["%s: %s" % (k,v) for k,v in filter(lambda k,v: v, some_hash.items())])
@narma
narma / flatten.py
Created April 6, 2012 12:36
python flatten
def make_flatten(fl_types=None):
if fl_types is None:
fl_types = (list, tuple)
def flatten(x):
for i in x:
if isinstance(i, fl_types):
for a in flatten(i):
yield a
else:
@narma
narma / tablize.README
Last active October 2, 2015 21:47
create table from plain data list with rows, columns in each chunk
""" from range(1, 18) with 3 rows and 4 columns it's create like this:
4 columns
#############
1 4 7 10 #
2 5 8 11 # 3 rows
3 6 9 12 #
13 16
14 17
15
"""
@narma
narma / gist:3359558
Created August 15, 2012 12:07
clojure test
(defn factors-sorted-by-product [start end]
(map rest (let [r (reverse (range start end))]
(reverse (sort-by first (for [x r y r :when (>= x y)]
(list (* x y) x y)
)
))
)
))
(defn get-diff-factors [vals]
@narma
narma / gist:3369482
Created August 16, 2012 11:29
clojure-euler-4
(import '(java.lang Math))
(defn palindrome? [n]
(= (->> n str reverse (apply str))
(str n)))
(defn test-for-products [n pmax pmin]
(some #(let [p (/ n %)]
(and (<= p pmax) (>= p pmin)))
(for [i (range pmin (inc pmax)) :let [m (mod n i)] :when (= m 0)] i)))
@narma
narma / gist:3667979
Created September 7, 2012 17:32 — forked from marekjelen/gist:3256675
New PostgreSQL on OpenShift
POSTGRESQL_VERSION="9.2.1"
# Installing DB (one-time)
cd $OPENSHIFT_DATA_DIR
wget http://ftp.postgresql.org/pub/source/v${POSTGRESQL_VERSION}/postgresql-${POSTGRESQL_VERSION}.tar.gz
tar xf postgresql-${POSTGRESQL_VERSION}.tar.gz
rm postgresql-${POSTGRESQL_VERSION}.tar.gz
@narma
narma / arel_back.rb
Created October 16, 2012 09:54
arel in rails 2.3
# put into lib/arel_back.rb
module ArelBack
module ActiveRecord
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
@narma
narma / find-pin.clj
Last active December 20, 2015 23:08
(ns find-pin)
(defn ^String to-pin
[^Integer x]
(let [v (str x)]
(.concat (apply str (repeat (- 4 (count v)) 0)) v)))
(defn ^java.lang.Boolean valid-pin?
[^String v]
(and
@narma
narma / gitstat.sh
Last active December 22, 2015 15:48
git log --shortstat --since "5 months ago" | gawk 'BEGIN {
printf "\n%-10s %-10s %-10s %-10s %-40s %-5%%s\n", "total", "insert",
"deleted", "changed f", "author", "";
total_changed=0;
total_insert=0;
total_del=0
}
$1 ~ /^Author:/ {
cur_author=substr($0, index($0,$2))
}