A Better Timesince Template Filter For Django

Django's timesince filter: 2 hours, 38 minutes

My timesince filter: 2 hours ago

If you like my version here's how to use it:

Make a directory (if one doesn't already exist) in your app's directory called "templatetags" --- it should live in the same level as models.py, views.py, etc. Create a file called app_extras.py and put the following code into it.

from django import template
import datetime, pytz

register = template.Library()

def time_since(date, default="just now"):
    now = datetime.datetime.utcnow().replace(tzinfo = pytz.utc)
    diff = now - date
    periods = (
        (diff.days / 365, "year", "years"),
        (diff.days / 30, "month", "months"),
        (diff.days / 7, "week", "weeks"),
        (diff.days, "day", "days"),
        (diff.seconds / 3600, "hour", "hours"),
        (diff.seconds / 60, "minute", "minutes"),
        (diff.seconds, "second", "seconds"),
    for period, singular, plural in periods:
        if period:
            return "%d %s ago" % (period, singular if period == 1 else plural)
    return default

In your template you'll load these extra template filters like this:

{% load poll_extras %}

And then while rendering the form field I used the variable I just created like this:

{{ article.pub_date|time_since }}

Record Video With Swift

Here's a simple example that illustrates how to record video with the Swift programming language on iOS.

First check to see if a camera source type is available:

if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {

            println("captureVideoPressed and camera available.")

            var imagePicker = UIImagePickerController()

            imagePicker.delegate = self
            imagePicker.sourceType = .Camera;
            imagePicker.mediaTypes = [kUTTypeMovie!]
            imagePicker.allowsEditing = false

            imagePicker.showsCameraControls = true

            self.presentViewController(imagePicker, animated: true, completion: nil)


        else {
            println("Camera not available.")

Then use this delegate method to actually save the video to the user's camera roll:

func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info:NSDictionary!) {

        let tempImage = info[UIImagePickerControllerMediaURL] as NSURL!
        let pathString = tempImage.relativePath
        self.dismissViewControllerAnimated(true, completion: {})

        UISaveVideoAtPathToSavedPhotosAlbum(pathString, self, nil, nil)


Here's a complete project that I shared on Github.

I tested this on iOS7 and iOS8.

Find Unique Named Entities Using Python And NLTK

I'm exploring a concept that requires natural language analysis to programmatically find the subjects of a given piece of text which, in my case, are just the unique named entities.

The Python NLTK library is just what I needed, but it has some non-trivial API changes since version 3.0 which broke many of the recent NLTK examples on the Internet.

Here's an example of what I ended up doing in light of those changes to extract the unique named entities:

import nltk

def parts_of_speech(corpus):
    "returns named entity chunks in a given text"
    sentences = nltk.sent_tokenize(corpus)
    tokenized = [nltk.word_tokenize(sentence) for sentence in sentences]
    pos_tags  = [nltk.pos_tag(sentence) for sentence in tokenized]
    chunked_sents = nltk.ne_chunk_sents(pos_tags, binary=True)
    return chunked_sents

def find_entities(chunks):
    "given list of tagged parts of speech, returns unique named entities"

    def traverse(tree):
        "recursively traverses an nltk.tree.Tree to find named entities"
        entity_names = []
        if hasattr(tree, 'label') and tree.label:
            if tree.label() == 'NE':
                entity_names.append(' '.join([child[0] for child in tree]))
                for child in tree:
        return entity_names
    named_entities = []
    for chunk in chunks:
        entities = sorted(list(set([word for tree in chunk
                            for word in traverse(tree)])))
        for e in entities:
            if e not in named_entities:
    return named_entities

entity_chunks  = parts_of_speech("George Bush is the 41st President of the United States.")
named_entities = find_entities(entity_chunks)

print named_entities

This was adapted for NLTK 3.x and was based this example.

Custom Font Awesome Icon Size, Border, and Inverse Colors

It's completely possible to customize a Font Awesome icon by changing the size and even adding a border to it.

Here's an example of making the icon white and then adding a blue border to it:

.outline-blue {
    color: white;
    text-shadow: -1px 0 #4169E1, 0 1px #4169E1, 1px 0 #4169E1, 0 -1px #4169E1;

And here's some CSS for changing the size using a class:

.size-24 {
    font-size: 24px;

Then in your HTML use the outline-blue and size-24 classes like this:

<a href="/inbox"><i class="fa fa-inbox size-24 outline-blue"></i>Bob's Inbox</a>

Set The Height Of A TextArea In Zurb Foundation

Setting the height of a TextArea in Zurb Foundation is quite simple. Just set the rows attribute like this:

<textarea rows="5" placeholder="Large TextArea" name="fieldName"</textarea>