Simple Process Timer in Python.

Profiling code is important.

In python, the easiest way to time your code is to do the following:

from datetime import datetime
start = datetime.now()
# Some long-running process
end = datetime.now()
print "process run in %s" % (end - start)

This solution works, but it can be annoying to write, and you’ll have to remove the debug lines before you ship your code to production. Once or twice, this is okay. But doing it over and over again can get annoying.

Do this in one line:

with timer() as t:
    print "Hello, world!"
    # Some long-running process.

which returns

Hello world!
process run in 0:00:00.001030

It’s cleaner, and you can easily toggle it if your application is off of DEBUG mode. Code below:

from datetime import datetime
import time
 
class timer:
    def __enter__(self):
        self.start = datetime.now()
        return self
 
    def __exit__(self, type, value, traceback):
        self.end = datetime.now()
        print "time: %s" % (self.end - self.start)
 
with timer() as t:
    print "Hello, World!"
    time.sleep(2)

or gist.

Posted in python | Leave a comment

On The Edge of Chaos

On the edge of chaos.

via Creative Emergence

Posted in ideas | Leave a comment

Dawson on “The Gains of Drudgery”

Came across an interesting post this morning that related to some thoughts I’ve been having about my own personal work ethic. It takes from a piece written by William James Dawson, called The Gains of Drudgery. I’ll paste the first paragraph here, the rest is after the fold.

By drudgery, I mean work that in itself is not pleasant, that has no immediate effect in stimulating our best powers, and that only remotely serves the purpose of our general advancement. Such a definition may not be perfect, but it expresses with reasonable accuracy what we usually understand by the term.

Read More »

Posted in links, quotes | Leave a comment

Interesting Article from good.is on the “Information Arms Race”

[...] The rise of microtargeting is a function of new logarithms—and computers fast enough to process them—that are able to capture all this trash and turn it into gold. Over the years, the data-mining industry has become adept at recycling information about the websites we visit and the products we buy. Rumor has it that some high-end companies, including Omaha Steaks, can now make more money by selling their customer pedigrees to data-mining firms than they can from selling their product

via good.is.

Interesting read. It’s a bit higher-level than I would have hoped, but it does a great job introducing some of the many ways that organizations are leveraging seemingly minute, uncorrelated data in ways that will add values to organizations.

Posted in business, links | Leave a comment

There exists a continuous rail service that goes between Moscow & Vladivostock. It spans 9,259km.

And Google Maps knows about it. In fact, it recommends it if you ask for Public Transit directions between the aforementioned cities.


View Larger Map

Brilliant. In the words of Kurt Vonnegut, ”bizarre travel plans are dancing lessons from God.” More info on the railway on Wikipedia.

Posted in quotes, travel | Leave a comment

Thin Python Cache Decorator

def cache(func):
""" A thin middleware that caches based on function name and arguments. """
    def _inner(*args, **kwargs):
        a_str = "&".join([str(a) or None for a in args])
        kw_str = "&".join(["%s=%s" for key, val in kwargs.iteritems()])
        key_name = slugify("%s:%s:%s" % (func.__name__, a_str, kw_str))
        cached = cache.get(key_name)
        if not cached:
            cached = func(*args, **kwargs)
            cache.set(key_name, cached)
        return cached
    return _inner

This is designed with django in mind, but you can replace cache.set and cache.get with any cache cache framework of your choice. Also, I use slugify because many cache frameworks don’t like spaces in cache key names. Slugify replaces them with dashes.

Usage is simple:

@cache
def expensive_method(a,b=True):
    ...
Posted in Uncategorized | Leave a comment

On Meditation

Third, meditation can help a man “be his own man” and feel comfortable in his own skin.  That constant stream of input we face each day often carries messages of what we’re supposed to think or feel.  Talking-heads spout off opinions as if they were facts. Advertisers try to convince us that buying such-and-such product will make us feel more virile and manly. Meditation allows us to be alone with our own thoughts and discover what we really think about the world and ourselves.

via Art of Manliness

Posted in Uncategorized | Leave a comment

Focus Time Hack (OSX)

Block facebook and reddit on your Mac; focus longer and get work done instead.

Open up a terminal, copy and paste this:

sudo echo "127.0.0.1\tfacebook.com\n127.0.0.1\treddit.com" >> /etc/hosts


										
Posted in Uncategorized | Leave a comment

User Interfaces that Involve Uncertainty

Marco Polo

I installed Marco Polo today. It seems like a useful application, but it left me wondering. What are some ways to represent decisions that involve uncertainty in user interfaces?

The first mental framework that came to mind was the different types of measurement scales. An ordinal scale might make sense, but it’s hard to order things when there might not be an inherent ordering. Ratio and Interval wouldn’t do the trick here. At the same time, this nominal scale just doesn’t work in my mind.

iTunes has a nice interface for building playlists.

iTunes Smart Playlist

It’s an interface for defining and setting rules; maybe this would be more appropriate for Marco Polo’s interface? To go a little bit deeper, what does it mean to have 50% confidence that my Monitor is Color LCD?

I’m curious about this now. What are some other common UIs that involve making decisions based on uncertainty?

Posted in design | Leave a comment

Google Circles

I started using Google Plus three weeks ago. I like it a lot; I think I’ll keep using the service. The UI works, a lot of my friends are already on it, but most importantly it solves a problem that has not yet been solved online: easy, logical information sharing.

Logical information sharing.

Information sharing is a two-piece puzzle. It’s about the systems behind the social graph (which trust me, can probably already figure out who my Virginia friends are and separate them from my Beirut friends), but even more importantly, my experience of knowing that the system can segment my friends into groups.

It’s cool if a system knows who is related to whom, but that’s only half of the puzzle. I, the user, have to know about this as well, and I have to have absolute trust in the system. I have to know the boundaries of my social circles, and I need an explicit agreement with the system that shows me who is in which group. For a while, I was skeptical of the answer being a better UI, but this might just be the winning solution.

It’s slightly uncanny, because up until this point, my claim is that Google Plus wins because the system can segment my friends into circles. This seems strange, because with all of Facebook’s engineering talent, they have to have been able to figure out how to do this automatically. Splitting people up into groups is a problem solved by computer scientists time and time again.

I recall watching a friend make a Facebook list – few people do it, but those who do it tend to be engaged with the lists for the long term. My hypothesis is that once a user knows who is and is not in a specific list (or circle, as we call it now), we will see medium-to-long term increases in that user’s engagement.

Posting something on facebook takes me a lot of thought; I deal with some internal strife. One must “write with their audience in mind”, which is absolutely true. Google Plus lets me segment my audience, which is why I like it a lot.

Systems are interesting – they’re about the underlying technology, but more importantly, the involved stakeholders (in this case: end users). Up until this point, nobody has created a system that lets the user feel in control of their social circles. Algorithms can easily match this, but my premise is that it’s more importnat for the user to feel some semblance of control. A user who knows exactly who is (and perhaps even more importantly, whois not) in a social circle, is more willing to share things.

I used to be an avid LiveJournal user. My friends and I were active livejournal users around the time we were in middle school and early high school (ages 13-16). I downloaded my old posts and was surprised to see what I was sharing. I went into every detail of my life. Everything, from the track meets I ran in, my academic performance, down to the girls I had a crush on. Even more interesting was the fact that out of over 1,000 posts, only 20 of them had no privacy settings whatsoever.

Only 20. That means that 98% of my interaction with LiveJournal involved posting content to specific groups. I was comfortable sharing everything because I knew that a rant about somebody, for example, would not get into that person’s feed.

I felt the same way yesterday. I posted something to Google Plus, into my California Social Circle. These are my San Francisco friends. Nobody from Beirut. Nobody from UVa. Only people who would find it relevant. Strangely enough, I felt completely comfortable doing it. Even more interesting was I had a lot more feedback than I would have gotten if I had put it on Facebook or Twitter.

Interesting! Sounds like the SNR is down. Well done, Google Plus.

Posted in Uncategorized | Leave a comment