Skip to content

Instantly share code, notes, and snippets.

@rockymeza
rockymeza / gist:1244432
Created September 27, 2011 05:53 — forked from gavinwahl/gist:1244417
relation algebra
function Relation(schema, data)
{
this.schema = schema;
this.data = data;
// only supports equals for now
this.s = function(attribute, value) {
var index = this.schema.indexOf(attribute);
var new_data = this.data.filter(function(datum) {
return datum[index] == value;
@rockymeza
rockymeza / input_histogram.c
Created December 29, 2011 18:12
histogram of character frequencies
#include <stdio.h>
#include <limits.h>
#define CHAR_MIN_OFFSET CHAR_MIN - CHAR_MIN
#define CHAR_MAX_OFFSET UCHAR_MAX - CHAR_MIN
main()
{
int i, count[CHAR_MAX_OFFSET];
char c;
@rockymeza
rockymeza / gist:2986238
Created June 25, 2012 03:03
backslash in LaTeX
\usepackage{xspace}
\newcommand\bs{{\textbackslash}\xspace}
%% in content later on
``Look at her, a prisoner of the gutter \bs condemned by every syllable she utters''
@rockymeza
rockymeza / chain_defaults.py
Created May 14, 2013 16:22
Chain |default in django
>>> from django.template import Template, Context
>>> Template('{{ foo|default:bar|default:"test" }}').render(Context({'bar': 1}))
u'1'
>>> Template('{{ foo|default:bar.baz|default:"test" }}').render(Context({'bar': 1}))
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/XXX/site-packages/django/template/base.py", line 140, in render
return self._render(context)
File "/XXX/site-packages/django/template/base.py", line 134, in _render
return self.nodelist.render(context)
@rockymeza
rockymeza / backupmodel.py
Created July 23, 2013 22:30
Takes a model class and backs up all of the rows in a JSON file.
from django.conf import settings
from django.core.serializers import serialize
def backupmodel(model_class):
"""
Takes a model class and backs up all of the rows in a JSON file.
"""
log.info("Backing up %s", model_class)
@rockymeza
rockymeza / dedent.py
Created July 24, 2013 17:42
Django template tag that dedents all lines.
import re
from django import template
register = template.Library()
leading_whitespace_re = re.compile(r'^\s+', flags=re.MULTILINE)
@register.tag(name='dedent')
def do_dedent(parser, token):
@rockymeza
rockymeza / django-strorages.json
Created August 5, 2013 16:00
AWS IAM Policy for S3 for django-storages
{
   "Statement":[
      {
         "Effect":"Allow",
       
         "Action":[
            "s3:ListAllMyBuckets"
         ],
         "Resource":"arn:aws:s3:::*"
      },
#!/usr/bin/env python
"""
A command to check redirects.
Expects stdin in to look like this:
http://example.com/old-url,http://example.com/new-url
Outputs to stderr:
@rockymeza
rockymeza / gist:6176247
Created August 7, 2013 17:19
vimium memory consumption over time.
2013-08-07 08:36 enabled
2013-08-07 08:36 12,992
2013-08-07 08:36 16,836
2013-08-07 08:37 19,888
2013-08-07 08:37 22,680
2013-08-07 08:39 24,556
2013-08-07 10:07 188,036
2013-08-07 10:12 195,804
2013-08-07 10:34 225,264
2013-08-07 11:18 297,664
@rockymeza
rockymeza / base.py
Last active December 21, 2015 07:19
New as_view
class View(object):
# ...
def __init__(self, request, args, kwargs):
self.request = request
self.args = args
self.kwargs = kwargs
@classonlymethod
def as_view(cls, **initkwargs):
"""