Skip to content

Instantly share code, notes, and snippets.

View sshirokov's full-sized avatar
💭
I am a 🐈

Slava Shirokov sshirokov

💭
I am a 🐈
View GitHub Profile
@sshirokov
sshirokov / Resize With Glee
Created February 20, 2009 22:42
Resize Emacs Windows With C-S-<arrow keys>
;;; Window resizing crap
(global-set-key (kbd "S-C-<left>") 'shrink-window-horizontally)
(global-set-key (kbd "S-C-<right>") 'enlarge-window-horizontally)
(global-set-key (kbd "S-C-<down>") 'shrink-window)
(global-set-key (kbd "S-C-<up>") 'enlarge-window)
@sshirokov
sshirokov / monkeypatch.py
Created May 22, 2009 03:32
Functions to use, and generate monkeypatches for classes.
def monkeypatch(target, name = None, func = None, backup_prefix = "default_"):
'''
Decorator.
Binds target.name = func
Creates target.{backup_prefix + name} = target.name if property exists.
This allows the patched properties to depend on the existence of the original
property on the target.
'''
if not func: return lambda func: monkeypatch(target, name, func)
if not name and func.__name__[0] is not '<': name = func.__name__.strip('_')
@sshirokov
sshirokov / chains.py
Created May 22, 2009 03:37
Functions to create class methods chain-friendly (as in A.b().c().d()) and a function to create function chains (as in: make_consuming_chain(a, b, c)() #=> c(b(a())) )
def make_consuming_chain(*functions, **kwargs):
'''
Return a function that will call functions in sequence, passing the return down the chain of functions
'''
return reduce(lambda acc, f: lambda *args, **kwargs: f(acc(*args, **kwargs)), functions)
def make_chainable(method):
'''
Make a method returning none return self
'''
(defun toggle-dedicate ()
(interactive)
(set-window-dedicated-p (selected-window)
(not (window-dedicated-p (selected-window)))))
(global-set-key [(control c) (shift d)] 'toggle-dedicate)
@sshirokov
sshirokov / render_fragments.js
Created July 6, 2009 03:58
The early stages for a framework to render a site using JS-defined fragments
//Example fragments, extra magic withheld
window.fragments = {
name: {
url: context.project_property_url.to_project_url().to_property_url('name'),
location: '.project_title'
},
background: {
_interval: 60000,
@sshirokov
sshirokov / wtFSM.py
Created July 21, 2009 05:37
A borderline DSL for describing simple state machines in python
class State(object):
transitions = []
def __init__(self, *transitions_or_data):
if len(transitions_or_data) and reduce(lambda acc, e: acc and type(e) == Transition, transitions_or_data, True):
type(self).transitions = transitions_or_data
else:
self.data = {
1: lambda: transitions_or_data[0],
0: lambda: None,
@sshirokov
sshirokov / require_post_or.py
Created September 26, 2009 16:04
@require_POST_or(url_name_or_callable): Run this method if POST, call another or redirect away otherwise.
from django.core.urlresolvers import reverse
from django.views.decorators.http import require_POST
from django.http import HttpResponseRedirect, HttpResponseNotAllowed
def require_POST_or(other):
def _d_closure(func):
def wrapped_func(request, *args, **kwargs):
r = require_POST(func)(request, *args, **kwargs)
if type(r) == HttpResponseNotAllowed:
if callable(other): return other(request, *args, **kwargs)
@sshirokov
sshirokov / mapchain.py
Created October 18, 2009 17:06
Helper to map a chain of functions in order to a given list.
def mapchain(*functions_then_list):
'''
mapchain(f..fn, list)
Map the functions in sequence from left to right across list
mapchain(f1, f2, f3, f4, list) => map(f4, map(f3, map(f2, map(f1, list))))
'''
if len(functions_then_list) < 2: raise TypeError("mapchain(f..fn, list) requires at least one function and a list")
ops, items = functions_then_list[0:-1], functions_then_list[-1]
if not reduce(lambda a, b: a and b, map(callable, ops), True): raise TypeError("All but the list must be callable")
return reduce(lambda a, b:\
@sshirokov
sshirokov / erlang-yaws.el
Created October 25, 2009 22:14
Run an erlang repl with yaws loaded and serving
;; Run a emacs repl with yaws loaded and configured
(defvar yaws-path "~/Applications/YAWS")
(defun run-erlang-yaws ()
(interactive)
(let ((inferior-erlang-machine-options `("+K" "true"
"-pa" ,(concat yaws-path "/lib/yaws/ebin")
"-yaws" "debug"
"-run" "yaws"
"-yaws" "id" "default")))
(run-erlang)))
@sshirokov
sshirokov / erlang-yaws.el
Created October 26, 2009 14:13
Interim showoff of erlang-yaws.el (Which may be useless)
(require 'cl)
(require 'erlang-start)
;; Run a emacs repl with yaws loaded and configured
(defvar yaws-path "~/Applications/YAWS")
(defvar yaws-listening-regex "Yaws: Listening")
(defvar yaws-extra-paths ())
(defvar yaws-extra-flags ())
(defadvice inferior-erlang-wait-prompt (before send-ret-before-waiting last () activate)