Skip to content

Instantly share code, notes, and snippets.

@brake
brake / ascii2hex.clj
Created April 19, 2017 14:35
ASCII to Hex String in Clojure
(defn ascii->hex-string
"Perform Hex Encoding of a string: String(208A) -> String(32303841)"
[s]
(->> s
(map #(Integer/toHexString (int %)))
(apply str)))
@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
@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 / 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 / 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 / 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 / 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 / forever.clj
Created August 19, 2016 20:55
Clojure macro to organize an infinite loop
(defmacro forever [& body]
`(while true ~@body))
@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 / 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.