really, windows? 2014, and you do this to me?

 

the most depressing thing to happen to me today, looked a lot like this:

images

i was holding my iphone at the time.

the anachronism stayed with me for most of the day.


 

don’t even get me started on the sheer silliness of permanent deletion in an age of near-zero-cost storage. who permanently deletes anything anymore?


 

check this out, for an even more stark relief.

i think that, in an average year, i spend about as much time dealing with phone apps (100k loc or less) as i do dealing with a random windows version (40m loc). i’ll let you figure out which of the two category of experiences is more rewarding and/or pleasant.

Intermission: using Twisted to serve Django via wsgi

and now, while on break from our ongoing tutorial (table of contents).

are you tired of starting django and twisted via two separate processes, possibly having them run in two different shells for debugging purposes? i know i am.

fortunately, django and twisted can inter-operate – Django supports the Python wsgi interface, and twisted can be used to serve wsgi applications. this is actually the ideal method for having the two interact with each other (and is the first post in this tutorial that runs 100% counter to my disclaimer)

detailed documentation for this process is available at the twisted documentation site http://twistedmatrix.com/documents/current/web/howto/web-in-60/wsgi.html

give a shot to modifying your current tutorial code to make this work.


there are two components required to properly serve the django portions of this tutorial as wsgi resources:

  • configuring Django to work as twisted wsgi resource
  • serving the static components of django through twisted

this is slightly more complicated than it sounds, since, in newer versions, django dynamically searches through an ordered list of directories when serving its static components. using twisted to mimic that same functionality can take some work. i opted to use a custom directory scanning twisted resource

there aren’t very many decisions to make here; most of the work has to do with reading the documentation, and figuring out how to access the specific, relevant interfaces. if you’re stuck, you can find a clear example here; this basic resource might help, too.

 


tried it? given up? since i’ll be relying on this work when expanding the tutorial, it might be worth your time to see how i did it (and see if it makes sense to you). take a look at this commit/diff to get a list of the specific changes i made – let me know if it makes sense (or if i made a mistake/have a typo)


the git repository for the ongoing chat tutorial has this completed step tagged as v.0.2.1 – if you’ve already cloned the git repo, you can check out a clean version like so:

git checkout v0.2.1

javascript speed test

Aside

given the following function:

  function addInts(anArray){
     var result = 0;
     for (var i = 0; i < anArray.length; ++i){
        if (anArray[i] < 21) {
           result = result + anArray[i];
        } else { 
           result = result + anArray[i] - 21;
        }
     }
     return result;
  }

what would you expect will run faster?

    addInts(sortedArray); #sorted
    addInts(unsortedArray); #unsorted

 


 
check your answer


 

[More programming riddles]

javascript anagram detector

Aside

what’s the shortest anagram detector you know how to write, in javascript?


there must be a shorter way to do this. highlight below to see a solution.


var anagramDetector = function (x,y) {
   if (x.length != y.length) return false;
   var alpha_x = x.split("").sort().join("");
   var alpha_y = y.split("").sort().join("");
   for ( j = 0; j<y.length;++j) {
       if (alpha_x[j] != alpha_y[j]) return false;
  }
  return true;
}

PS: there’s a cool method for solving this for a fixed alphabet, using primes


[More programming riddles]

detecting anagrams in python

Aside

what’s the shortest anagram detector you know how to write, in python?


this is one that keeps feeling like it should be a one liner (because it’s very similar to this one liner), but i suspect can’t be done in one line

highlight to see a solution.


def fn(x,y):
    if len(x) != len(y):
        return False
    for i in x:
        y = y.replace(i,'',1)
    return not len(y) > 0

PS: there’s a cool method for solving this for a fixed alphabet, using primes


[More programming riddles]