Skip to content

Instantly share code, notes, and snippets.

View niwinz's full-sized avatar

Andrey Antukh niwinz

View GitHub Profile
(defn clj->js
"Recursively transforms ClojureScript maps into Javascript objects,
other ClojureScript colls into JavaScript arrays, and ClojureScript
keywords into JavaScript strings."
[x]
(cond
(string? x) x
(keyword? x) (name x)
(map? x) (.strobj (reduce (fn [m [k v]]
(assoc m (clj->js k) (clj->js v))) {} x))
@niwinz
niwinz / autocurry.clj
Last active August 29, 2015 13:56
Auto currying for clojure (a la haskell but more explicit)
(defmacro defc
[identifier bindings & body]
(let [n# (count bindings)]
`(def ~identifier
(fn [& args#]
(if (< (count args#) ~n#)
(apply partial ~identifier args#)
(let [myfn# (fn ~bindings ~@body)]
(apply myfn# args#)))))))

Every so often I have to restore my gpg keys and I'm never sure how best to do it. So, I've spent some time playing around with the various ways to export/import (backup/restore) keys.

Method 1

Backup the public and secret keyrings and trust database

cp ~/.gnupg/pubring.gpg /path/to/backups/
cp ~/.gnupg/secring.gpg /path/to/backups/
cp ~/.gnupg/trustdb.gpg /path/to/backups/

or, instead of backing up trustdb...

@niwinz
niwinz / views.py
Last active August 29, 2015 14:05
Generic Views
from django.core.urlresolvers import reverse
from django.core.paginator import Paginator
from django.core.paginator import InvalidPage
from django.core.paginator import EmptyPage
from django.core.paginator import PageNotAnInteger
from django.views.generic import View
from django.template import RequestContext
from django.template.loader import render_to_string
from django.shortcuts import render_to_response
@niwinz
niwinz / multimethods.py
Last active August 29, 2015 14:13
clojure like multimethods implemented in python3
from threading import Lock
from inspect import isclass
from functools import partial
def isa(cls_child: "class", cls_parent: "class") -> bool:
if not isclass(cls_child):
return False
if not isclass(cls_parent):
return False
@niwinz
niwinz / async_psycopg2.py
Created April 19, 2012 20:53 — forked from FSX/async_psycopg2.py
A module for asynchronous PostgreSQL queries in Tornado.
#!/usr/bin/env python
__author__ = 'Frank Smit <frank@61924.nl>'
__version__ = '0.1.0'
import functools
import psycopg2
from tornado.ioloop import IOLoop, PeriodicCallback
@niwinz
niwinz / django_interface.py
Created April 25, 2012 21:30
Django websockets with tornado, gevent and zeromq
# -*- coding: utf-8 -*-
from gevent import monkey; monkey.patch_all()
import gevent
from gevent_zeromq import zmq
class WebSocketHandler(object):
def __init__(self, _id, in_queue, socket):
@niwinz
niwinz / sigdispatch.py
Created June 27, 2012 14:08
Correct monky patching of django User.
# -*- coding: utf-8 -*-
from django.db.models import signals
def model_class_prepared(sender, **kwargs):
if sender._meta.app_label == 'auth' and sender._meta.module_name == 'user':
print "Monky patching user..."
email_field = sender._meta.get_field("email")
email_field.max_length = 200
email_field._unique = True
@niwinz
niwinz / gist:3049907
Created July 4, 2012 22:39
Cache decorator for view methods (django)
from django.core.cache import cache
def cacheable(cache_key, timeout=3600):
def paramed_decorator(func):
def decorated(self):
key = cache_key % self.__dict__
res = cache.get(key)
if res == None:
res = func(self)
cache.set(key, res, timeout)
@niwinz
niwinz / solution1.py
Created July 4, 2012 22:43
kata: make list of list as plain list of elements
#from functools import reduce # uncomment if python3+ is used
lmb = lambda x, y: x+reduce(lmb, y, []) if isinstance(y, list) else x + [y]
reduce(lmb,[1,['a','b'],2,[[1,2, [3]]],'c'], [])