Skip to content

Instantly share code, notes, and snippets.

latest_comments = Comments.objects.order_by('date')[:10]
<ul>
{% for comment in latest_comments %}
<li><a href="{{ comment.get_absolute_url }}">{{ comment.title }}</a></li>
{% endfor %}
</ul>
@debrouwere
debrouwere / gist:744830
Created December 17, 2010 11:53
virtual environments in npm, the node package manager (rough sketch)
# run this in a test directory using a regular bash prompt to get an idea of how virtual environments would work, roughly
alias npm-env-init="mkdir ./env; mkdir ./env/packages; echo 'root = ./env/packages' > ./env/.npmrc; echo 'Created a new virtual environment for node packages in ./env'"
alias npm-env-workon="export npm_config_userconfig=./env/.npmrc; export NODE_PATH=./env/packages;"
alias npm-env-ls="nvp workon; nvm ls active;"
# we make a virtual environment
npm-env-init
# we switch to that virtual environment
npm-env-workon
# we install a package
npm install underscore
@debrouwere
debrouwere / gist:809993
Created February 3, 2011 19:20
Nested resources in django-tastypie
from sandbox import models
from tastypie import fields
from apibase.resources import CamayakModelResource
from django.conf.urls.defaults import url
class ModelResource(CamayakModelResource):
def override_urls(self):
urls = []
for name, field in self.fields.items():
@debrouwere
debrouwere / draughtsman_handler.coffee
Created October 25, 2011 21:20
A full working example of a filetype handler in the draughtsman server.
stylus = require 'stylus'
module.exports = (app) ->
app.get /^(.*\.styl)$/, (req, res) ->
stylus(req.file.content).render (err, css) ->
if err
res.send err
else
res.contentType 'text/css'
res.send css
@debrouwere
debrouwere / shorten.coffee
Last active December 12, 2015 09:19
Shortlinks for static site generators... for heroes. (No databases, no web apps, just NGINX rewrites.)
fs = require 'fs'
routing = require './utils'
postDir = 'posts'
shortlinksFile = 'shortlinks.conf'
stream = fs.createWriteStream shortlinksFile, {flags: 'a'}
shortlinks = (fs.readFileSync shortlinksFile, 'utf8').replace(/\s+$/).split '\n'
tail = shortlinks.slice(-1)[0]
counter = parseInt (tail.match /(\d+)\s/)?[1] or 0
page = require('webpage').create()
page.onConsoleMessage = (msg) ->
console.log 'CONSOLE:' + msg
page.onResourceRequested = (params, request) ->
isJavaScript = params['url'].slice(-3) is '.js'
isJQuery = (params['url'].indexOf 'jquery') isnt -1
if isJavaScript and not isJQuery
# encoding: utf-8
"""
First, run `authenticate.py` which will launch a new browser window that
will let you give this app permission to access your Google Analytics
data. The OAuth2 token will work for one hour and will be in
`ga-oauth2-token.json`.
Then, just run this file using `python analytics.py`.
@debrouwere
debrouwere / cas.coffee
Created June 8, 2014 22:45
Using content-addressable storage to efficiently and continually archive new versions of an HTML page including all related resources (images etc.)
### An interesting thing about news website homepages is that, while they
change all the time, the media on them doesn't change quite that fast: both
actual images but also stylesheets, javascript, logos and so on. Therefore,
when archiving these pages, it is possible to achieve significant space
savings by modifying the links to every image or other resource in the HTML to
instead refer to a file path that's a hash of the file's content: a type of
content-addressable storage.
The storage size can be further reduced by storing e.g. a day's worth of HTML
for one page (e.g. one fetch every hour) into a single lzip file, as LZMA can
@debrouwere
debrouwere / input.py
Created June 11, 2014 15:57
Take arguments from both stdin and as command-line flags
#!/usr/bin/env python
# encoding: utf-8
"""
This on the command line
echo '{"d": 44}' | ./input.py --a 33 --b 44 --c
turns into this argument passed to your function