Skip to content

Instantly share code, notes, and snippets.

@brake
brake / new_iTerm2_tab_from_dir.scpt
Created May 22, 2016 13:38
Open a new iTerm2 tab with folder selected in Finder. If iTerm was not previously started - starts it.
-- Stefan van den Oord, 2010-12-29
-- Constantin Roganov 2015-06-27
-- The "cd to" command for iTerm2 (added: opening in new tab of iTerm 2
tell application "Finder"
set _cwd to POSIX path of ((folder of (front window)) as alias)
end tell
tell application "iTerm"
activate
@brake
brake / create_dir_with_files.bat
Created June 1, 2016 08:22
Create a new directory and move desired files here (like Create directory from selection in Mac Finder)
@echo off
set proceed=true
if "%1"=="" set proceed=false
if "%2"=="" set proceed=false
if %proceed%==false (
echo.
echo Create directory and move desired files to it.
echo.
@brake
brake / coroutine.py
Created June 10, 2016 22:04
Python decorator turning generator into coroutine
def coroutine(gen_fn):
"""Python decorator turning generator into coroutine"""
def inner(*args, **kwargs):
gen = gen_fn(*args, **kwargs)
gen.next()
return gen
return inner
@brake
brake / forever.clj
Created August 19, 2016 20:55
Clojure macro to organize an infinite loop
(defmacro forever [& body]
`(while true ~@body))
@brake
brake / alert.clj
Created September 15, 2016 21:25
Message box in Clojure
(ns alert.core
(:import (javax.swing JOptionPane)))
(defn alert
[^String msg]
(JOptionPane/showMessageDialog nil msg))
@brake
brake / clipboard.clj
Created September 15, 2016 21:30
Text IO for system clipboard with Clojure
(ns clipboard.core
(:import (java.awt.datatransfer DataFlavor Transferable StringSelection)
(java.awt Toolkit)))
(defn get-clipboard
[]
(-> (Toolkit/getDefaultToolkit)
(.getSystemClipboard)))
(defn slurp-clipboard
@brake
brake / kdf.py
Last active October 12, 2016 10:13
Example Key Derivation Function based on Pycrypto module
from binascii import unhexlify
from functools import partial
from Crypto.Protocol import KDF
from Crypto.Hash import SHA512, HMAC
_SALT = unhexlify('48B755AB80CD1C3DA61182D3DCD2E3A2CA869B783618FF6551FB4B0CDC3B8066') # some salt
_KEY_LENGTH = 32
key_derivation_fn = partial(
KDF.PBKDF2,
@brake
brake / StreamZip.java
Created March 9, 2017 17:11 — forked from kjkrol/StreamZip.java
Zipping streams using JDK8 with lambda
import java.util.Iterator;
import java.util.function.BiFunction;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
/**
* Zipping streams into parallel or sequential stream using JDK8 with lambda
*
* @author Karol Krol
*/
@brake
brake / avoid_late_binding.py
Created March 25, 2017 15:53
Instance methods dynamic generation depending from __init__'s parameter, avoiding a late binding
from types import MethodType
def fn_factory(i, obj):
"""Function factory - avoids a late binding in closures"""
def fn(self):
return i
return MethodType(fn, obj)
class Aaa(object):
def __init__(self, n):
@brake
brake / scratch.py
Created March 30, 2017 19:59
Metaclass for attributes of specific type sets the attribute b to attribute name
class Meta(type):
def __new__(cls, name, bases, attrs):
for n, v in attrs.viewitems():
if isinstance(v, E):
v.b = n
return super(Meta, cls).__new__(cls, name, bases, attrs)
class E(object):
def __init__(self, a):
self.a = a