Skip to content

Instantly share code, notes, and snippets.

View skyl's full-sized avatar

Skylar Saveland skyl

View GitHub Profile
@skyl
skyl / gist:849500
Created March 1, 2011 17:20
stashing a mediocre idea
import djredis.models as djr
class MyMod(models.Model, djr.DredisMixin):
#...
currency = models.CharField(max_length=3)
djr_currency = djr.String()
cache_fields = (('currency', 'djr_currency'),)
@skyl
skyl / gist:866920
Created March 12, 2011 01:30
shruti system
In [35]: shrutis = {
....: 0: (1/Decimal('1'),),
....: 1: (256/Decimal('243'), 16/Decimal('15')),
....: 2: (10/Decimal('9'), 9/Decimal('8')),
....: 3: (32/Decimal('27'), 6/Decimal('5')),
....: 4: (5/Decimal('4'), 81/Decimal('64')),
....: 5: (4/Decimal('3'),),
....: # 27/Decimal('20'),
....: 6: (45/Decimal('32'), 729/Decimal('512')),
....: 7: (3/Decimal('2'),),
@skyl
skyl / tcp_client.py
Created May 1, 2011 06:00
slut tcp_client.py
(tut)skyl@skylarwerk:~/Code/gui/slut$ python tcp_client.py
/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/twisted/internet/_sslverify.py:5: DeprecationWarning: the md5 module is deprecated; use hashlib instead
import itertools, md5
You are running [slut v0.9.0] happy hacking!
2011-05-01 01:57:45.984 python[79813:903] *** __NSAutoreleaseNoPool(): Object 0x1014e3d50 of class NSCFData autoreleased with no pool in place - just leaking
fCoreAudioDriverUID AppleHDAEngineInput:1B,0,1,0:1
Enabled joystick: OSCulator HID 1
Enabled joystick: OSCulator HID 2
-> Default interaction model loaded.
[F11] -> toggle fullscreen
@skyl
skyl / gist:1359979
Created November 12, 2011 03:15
multidimensional k-means cluster finder in python
import math
import random
class ClusterCenters(object):
"""
>> d = ((0,0,0), (0,1,0), (0, 2, 0), (1,2,0), (55, 55,50), (50,50,50), (-100,-100,-100), (-80,-80,-100), (60,60,50))
>> cc = ClusterCenters(d, 3)
[[55, 55, 50], [0, 1, 0], [-90, -90, -100]]
>> cc = ClusterCenters(d, 2)
@skyl
skyl / gist:1410323
Created November 30, 2011 19:01
Python order of operations gotcha
In [61]: l
Out[61]: [1, 2, 3, False]
In [62]: i
Out[62]: 1
In [63]: not i in l
Out[63]: False
In [64]: (not i) in l
@skyl
skyl / gist:1422095
Created December 2, 2011 06:43
canvas img linear filter in coffeescript
minmax = (value, low, high) -> Math.min Math.max(low, value), high
class ImageProcessor
constructor: (@img, parent=document.body) ->
@canvas = document.createElement("canvas")
@context = @canvas.getContext("2d")
@canvas.width = @img.width
@canvas.height = @img.height
parent.appendChild @canvas
@skyl
skyl / gist:1531186
Created December 29, 2011 02:15
Fill varnish with requests
import urllib
import requests
exposure_liability_map = {
"people": [
#("RESDQ", "All residents Day"),
("RESNQ", "Night Residents"),
("RESFDQ", "Female residents Day"),
("RESN4Q", "Night residents < 4yo"),
],
@skyl
skyl / kwacros.py
Created February 1, 2012 05:03
Django Template macros with args and kwargs
#
# templatetags/kwacros.py - Support for macros in Django templates
#
# Based on snippet by
# Author: Michal Ludvig <michal@logix.cz>
# http://www.logix.cz/michal
#
# modified for args and kwargs by Skylar Saveland http://skyl.org
#
@skyl
skyl / linkify.coffee
Created February 22, 2012 03:25
linkify phone numbers in coffeescript
s = """
Extensions are not supported.
Alphabetic phonenumbers eg 1888-FOR-RILZ are not supported.
Of course, you'll want to prevent injection by stripping html tags first.
It switches the numbers to international format (or sth) - suggestions welcome!
706 555 5001put some
ill formed(404) 555-1397text in here
+34712938479 to make it interesting...5
404.555.1239.
@skyl
skyl / gist:1961812
Created March 2, 2012 22:17
Which is actually cleaner? (django templates)
{% exec %}
try:
f = durs[duration]
f.field.widget.attrs['placeholder'] = duration
print f
except KeyError:
pass
{% endexec %}
OR