Skip to content

Instantly share code, notes, and snippets.

@Suor
Suor / diffs.py
Created October 7, 2012 01:34
A dicts diff and patch utilities
def diff(old, new):
"""
diff(a, b) = b - a
a + diff(a, b) == b
"""
return strip_dict(map_dicts(diff_atom, old, new))
def diff_atom(old, new):
return None if old == new else (old, new)
@Suor
Suor / seqmaker.py
Created October 30, 2012 02:19
Sequence maker
from itertools import groupby
inc = lambda x: x + 1
dec = lambda x: x - 1
def partition_by(f, coll):
for _, group in groupby(coll, key=f):
yield list(group)
@Suor
Suor / trust-proxy.js
Created November 8, 2012 08:51
A connect middleware, broken yet
module.exports = function (ip) {
ip = ip || '127.0.0.1';
// Function to test whether or not the request is coming through the trusted reverse proxy.
var trust = function (req) {
return req.connection.remoteAddress === ip;
};
// Function to fetch the remote address from the browser that connected to the reverse proxy.
var remoteAddress = function (req) {
@Suor
Suor / 0_usage.py
Created November 18, 2012 07:43
Classes as namespaces in python
class checks(namespace):
immutable = lambda old_value, new_value: old_value == new_value
def gte(other):
if callable(other):
return lambda value: value >= other()
else:
return lambda value: value >= other
@Suor
Suor / activate_app.py
Created December 14, 2012 14:38
Switch to an app named (start it if needed) or toggle between its windows. Could be assigned to hotkey. Got it here - http://somanov.wordpress.com/2009/12/02/window-shortcuts-for-linux-desktops/
#!/usr/bin/env python
import os
import sys
import commands
program_name = sys.argv[1] # the program to be focused
# get all windows which contain "program_name" from wmcontrol
candidates = sorted([x.strip() for x in commands.getoutput(""" wmctrl -l -x | awk -v win="%s" 'tolower($0) ~ win {print $1;}' """ % (program_name, )).split("\n") if x !=''])
@Suor
Suor / fuzzy.js
Created December 20, 2012 03:48
fuzzy matching try
var q = query.replace(/\s+/, ' '),
re = new RegExp('^(.*?)' + q.split('').join('(.*?)'));
function score(junk) {
if (!junk) return Infinity;
return junk.slice(1).reduce(function (len, item) {return len + item.length}, 0);
}
return list.reduce(function (best, item) {
var junk = item.title.match(re),
suor$ npm install node-gd
npm http GET https://registry.npmjs.org/node-gd
npm http 304 https://registry.npmjs.org/node-gd
> [email protected] install /home/suor/projects/mediavault/node_modules/node-gd
> node-waf configure build
Checking for program g++ or c++ : /usr/bin/g++
Checking for program cpp : /usr/bin/cpp
Checking for program ar : /usr/bin/ar
@Suor
Suor / pkdiff3
Created February 14, 2013 06:07
kdiff3 call wrapper for kdesvn
#!/usr/bin/perl
s~^.*//~/~ for @ARGV;
system "kdiff3", @ARGV;
# kdiff3 %o %m %n -o %t
@Suor
Suor / 00_original.py
Last active December 14, 2015 09:19
Code Review
# Это оригинальный код, всё веселье ниже и по пунктам
limit = 5
def intify(array):
latest = []
for i in array:
row = []
for j in i:
row.append(int(j))
@Suor
Suor / dict-comprehension-review.py
Created March 15, 2013 07:57
Dict comprehension review
errors = {}
for key, value in bundle.data.items():
if not some_test(value):
if key not in errors:
errors[key] = []
errors[key].append('required.')
# R1: использовать defaultdict
from collections import defaultdict