Skip to content

Instantly share code, notes, and snippets.

View dir01's full-sized avatar

Andrew Zhukovskiy dir01

  • Tel Aviv, Israel
View GitHub Profile
class StepsProcessor(object):
def __init__(self, steps, filename):
self.steps = steps
self.filename = filename
def process(self):
for i, step in self.enumerate_steps_to_process():
try:
self.process_step(step)
except Exception:
@dir01
dir01 / gist:2133462
Created March 20, 2012 09:39
Add previews to textareas
(function($) {
$.fn.previewTextareas = function() {
var $el = this;
var run = function(){
var textareas = $('textarea', $el);
textareas.each(initializeTextarea);
};
@dir01
dir01 / gist:2187561
Created March 24, 2012 20:37
Find stuff in torrents folder not added to torrent client
ls -1 /data/torrents/ | while read NAME; do grep -qrF "$NAME" /var/transmission/config/torrents/ || echo $NAME; done
@dir01
dir01 / interface_proxy.py
Created March 29, 2012 09:06
Interface proxy
# -*-coding: utf8 -*-
import unittest2
from zope.interface import Interface
from zope.interface.declarations import implements, classImplements
class Storage(object):
CLASS_TEMPLATE = """
class %(class_name)s(object):
def __init__(self, instance):
@dir01
dir01 / dynamic_form.py
Created August 17, 2012 10:03
Django dynamic forms
import unittest2
from django import forms
class DynamicForm(forms.Form):
"""
Allows to alter fields amount and requireness via spec, e.g.:
form = DynamicFormSubclass(spec={
'last_name': False, # Drops field from initial form
'gender': {'required': True},
@dir01
dir01 / fake_mail.py
Created October 3, 2012 10:12
Fake SMTP server for testing purposes
# -*- coding: utf8 -*-
import base64
import os
import tempfile
from smtpd import DebuggingServer
from json import loads, dumps
class TestSmtpServer(DebuggingServer):
# -*- coding: utf8 -*-
from datetime import timedelta
from fabric.context_managers import settings
from fabric.contrib.files import sed, exists
from fabric.operations import sudo, run, put
from fabric.state import env
useful_software_list = [
'htop',
@dir01
dir01 / removeRedundantPoints.ts
Created April 9, 2019 09:29
Given an array of points, remove those, which are placed on a straight (and almost straight) line between two neighbor points
type Point = {
$type: "PlainPoint";
x: number;
y: number;
};
const removeRedundantPoints = (points: Point[]) => {
const redundantIndexes: number[] = [];
for (let i = 0; i < points.length - 2; i++) {
const uniq = <T>(arr: T[], fn: (el: T) => Object = (a) => a) =>
arr.reduce<T[]>(
(acc, curr) => (acc.some((a) => fn(a) === fn(curr)) ? acc : [...acc, curr]),
[],
);
test("uniq", () => {
const deduped = uniq([{ a: 2 }, { a: -2 }], (el) => Math.abs(el.a));
expect(deduped).toEqual([{ a: 2 }]);
});
const groupBy = <T extends { [k in K]: string }, K extends string>(
arr: T[],
attr: K,
) => {
return arr.reduce<{ [k in T[K]]?: T[] }>((result, item) => {
const k = item[attr];
if (result[k] === undefined) {
result[k] = [];
}
(result[k] as T[]).push(item);