Skip to content

Instantly share code, notes, and snippets.

View etscrivner's full-sized avatar
🦋

Eric Scrivner etscrivner

🦋
View GitHub Profile
@etscrivner
etscrivner / custom-commands.el
Last active August 29, 2015 14:22
Emacs snippet to kill a line backwards to the first whitespace character.
(defun kill-back-to-indentation (arg)
"Ignore ARG. Kill lines backwards to the first whitespace character."
(interactive "p")
(let ((start (point)))
(back-to-indentation)
(kill-region start (point))))
(global-set-key (kbd "C-c DEL") 'backward-kill-to-start-of-line)
;;; Example (Python): (The cursor is $)
@etscrivner
etscrivner / descriptor_fail.py
Created June 11, 2015 20:51
Subtle and strange problem with descriptors in python.
test = [['A'], ['B'], ['C']]
class ValueDescriptor(object):
def __init__(self):
self.value = None
def __get__(self, obj, objtype):
return self.value
@etscrivner
etscrivner / descriptor_fix.py
Created June 11, 2015 21:00
How to fix the descriptor issue.
import collections
test = [['A'], ['B'], ['C']]
class ValueDescriptor(object):
def __init__(self):
self.value_store = collections.defaultdict(lambda: None)
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(rainbow-delimiters-depth-1-face ((t (:foreground "dark orange"))))
'(rainbow-delimiters-depth-2-face ((t (:foreground "deep pink"))))
'(rainbow-delimiters-depth-3-face ((t (:foreground "chartreuse"))))
'(rainbow-delimiters-depth-4-face ((t (:foreground "deep sky blue"))))
'(rainbow-delimiters-depth-5-face ((t (:foreground "yellow"))))
@etscrivner
etscrivner / closure_objects.py
Created July 7, 2015 16:15
Simple python closure objects
"""
Simple message-passing closure objects example similar to SICP 3.1.1
"""
def make_account(balance):
# In Python 2.x we have to close over a mutable variable, so we are using
# obj.__setitem__(...) assignment rather than regular assignment which
# attempts to create a new variable within the current scope when each
# closure is initially compiled. Otherwise cannot assign the balance value
>>> account("deposit")(25)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "closure_objects.py", line 3, in deposit
balance = balance + amount
UnboundLocalError: local variable 'balance' referenced before assignment
@etscrivner
etscrivner / streams.hs
Created July 14, 2015 05:36
Haskell lazy evaluated sqrt approximations
average :: Float -> Float -> Float
average x y = (x + y) / 2
sqrtImprove :: Float -> Float -> Float
sqrtImprove guess x = average guess (x / guess)
sqrtStream :: Float -> [Float]
sqrtStream x = 1.0 : (map (\guess -> (sqrtImprove guess x)) (sqrtStream x))
main :: IO ()
@etscrivner
etscrivner / genetic-algorithm.scm
Created July 22, 2015 04:00
Genetic Algorithm Implementation In Scheme
(define (remove-at index list)
(map car (filter (lambda (x) (if (= (- (cdr x) 1) index) false true))
(map cons list (iota (length list) 1)))))
(define (random-shuffle lst)
(sort lst (lambda (x y) (equal? 0 (random 2)))))
(define (make-random-bit-mask size num-ones)
(if (< size num-ones)
(error "Invalid size -- MAKE-RANDOM-BIT-MASK" size num-ones)
@etscrivner
etscrivner / celery_speed.py
Created September 8, 2015 18:06
Simple python script to monitor celery queue processing speed in redis.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
celery_speed
~~~~~~~~~~~~
Simple tool to see how fast a celery queue in Redis is being consumed.
Usage:
celery_speed --redis-host REDIS_HOST --queue-name QUEUE_NAME
Project/Task % Time Estimation Error
MID-2215 216%
MID-2213 39.20%
MID-2214 34.40%
MID-2258 32.90%
MID-2079-PP 86.00%
MID-2079-LO 63.10%
MID-2079-PE -15.30%
MID-2079-PR 18.00%
PWS-43 116%