Archive

Archive for February, 2012

Pivotal Tracker

February 29, 2012 Leave a comment

Just started playing with Pivotal Tracker for a side project.

Categories: Links, Programming

People who say it cannot be done…

February 25, 2012 Leave a comment

People who say it cannot be done should not interrupt those who are doing it.

– George Bernard Shaw

Categories: Quotes Tags:

Lazy objects in Django

February 15, 2012 1 comment

Working on one of my Django projects, I created a new context processor to populate some template context variables.  One of them was a list of values pulled from the database that was only used on a few pages, so I tried to wrap it in a SimpleLazyObject:

from django.utils.functional import SimpleLazyObject
def current_presentation(request):
    presentation = SimpleLazyObject(lambda: fetch_current_presentation(request.user))
    presentation_records = SimpleLazyObject(lambda: list(presentation.items.values_list('record_id', flat=True).distinct()))
    return {
     'current_presentation': presentation,
     'current_presentation_records': presentation_records,
    }

That unfortunately does not work:

django.template.TemplateSyntaxError
TemplateSyntaxError: Caught TypeError while rendering: argument of type 'SimpleLazyObject' is not iterable

The problem is that the SimpleLazyObject wrapper does not implement an __iter__ method, so a wrapped object that is iterable loses that property.

The fix: a new subclass of SimpleLazyObject:

class IterableLazyObject(SimpleLazyObject):
    def __iter__(self):
        if self._wrapped is None: self._setup()
        return self._wrapped.__iter__()

Done!

Categories: Programming, Python Tags:

Hacker Olympics Puzzle, Part 2

February 14, 2012 Leave a comment

I am trying to teach myself Erlang, so I gave the Hacker Olympics Puzzle another try in a complete new (to me) language.

Here are the functions that do the work, the main program with the code are in a seperate file.  Everything is in a repository on github.

-module(decode).
-export([decode/1]).

decode(Code) -> convert(process(Code)).

process(Code)                            -> process(Code, [], []).
process([], Unique, _)                   -> Unique;
process([X, Y|Code], Unique, Duplicates) ->
    Pair = {X, Y},
    IsDuplicate = lists:member(Pair, Duplicates),
    IsUnique = lists:member(Pair, Unique),
    process_pair(Pair, IsDuplicate, IsUnique, Code, Unique, Duplicates).

process_pair(_, true, _, Code, Unique, Duplicates) ->
    process(Code, Unique, Duplicates);
process_pair(Pair, _, true, Code, Unique, Duplicates) ->
    process(Code, lists:delete(Pair, Unique), [Pair|Duplicates]);
process_pair(Pair, _, _, Code, Unique, Duplicates) ->
    process(Code, [Pair|Unique], Duplicates).

convert(List)               -> convert(List, []).
convert([], Result)         -> lists:concat(Result);
convert([{X, Y}|T], Result) -> convert(T, [cchar(X) + cchar(Y)|Result]).

cchar(C)           -> cchar(C, 0, "qwertyuiop").
cchar(C, I, [C|_]) -> I;
cchar(C, I, [_|T]) -> cchar(C, I + 1, T).
Categories: Erlang, Programming Tags:

U.S. Interstates as a Subway Map

February 10, 2012 Leave a comment

Categories: Fun Stuff

Hacker Olympics Puzzle

February 8, 2012 1 comment

Last night at the Shenandoah Ruby User Group meeting everybody gave this programming puzzle a shot.  Only after the meeting did I make the connection to regular expressions and was able to cut my Python code down to a tweetable format (excluding the input data).

s = "iirytroyppytpeppowuiyoiuypipiyuyotyoiwiytorouqyiepyirueuiprpptwyiroqwtuyipourrrourorwipowiorutpwtwruuioquteorioriyutwqyyprptuqqquuituyywyuoewtrppqwrtiiruirpwyptpuoyyoeyerpqiotuieripurouwuwyuwrworowitoepoypeipeypiireeorrwripuuteoitpuutturruwiyryeetyrioweytueqrrrtotrpiqetoyepoipiwrwpppiritupwipruooiepoweypiuwrqywuuwyiroqpqpruioopuiuerputpwwuuiqwpiroewrtwiqerteoyeqtutwoeoipioiuqiiiouuoqtuuwtuwiiqwppqitywuqpuutrqiyoeuuutwrwtoqrqyoeyoiporuuioiwryoeruypreprqroiyuqwtuyoytoerwqryeeripryieypiwqirtriurueypiuyuypriuiwptywppupoioyrewooytreteeriiwroietuwypieeeeeqpiiiwrirtppoeyiwpooyyporpruwuoriiyowuytiwpwpryeywotoyirqypyypoeptiuwwoyprquoetepptorwiewprpiywwtuieeiweytrrwiqtwotioryoiptyyyuriiqiyroeeiqepwteuwptpruuypipieuoiiuuweeuryuuowporrooyywyyrryriqyirooitirueruptotpryoqtipippyptiurwyiryooywtryyuurtrutwqiqiiiqwouyeooueoturuowiuwrtruwwiupwopwweoo"
import re
while s:
 r,n=re.subn(s[:2]+'(?=(..)*$)','',s[2:])
 if n<1:
  print sum("qwertyuiop".find(l) for l in s[:2]),
 s=r

A Taste of Their Own Medicine

February 8, 2012 Leave a comment

job fails - Monday Thru Friday: Here's hoping that they accepted his refusal of his denial
see more epicfails

Categories: Fun Stuff