Tag Archives: python

Python datetime quiz

Without looking at the documentation (or running the code), describe the outputs of the following 4 function calls: from datetime import datetime datetime.datetime.now() datetime.datetime.today() datetime.datetime.now().date() datetime.datetime.today().date() Post your answers as a comment.

Posted in General | Tagged , | 2 Comments

Pitfalls of Python’s ‘is’ operator

I saw this post on Friendfeed, and thought I’d reproduce the code here, because it’s a really interesting Python snippet, and to me, basically says never, ever use the ‘is’ operator.  Here’s the snippet that I just reproduced on Python … Continue reading

Posted in General | Tagged , , , , | 1 Comment

A simple Python script to archive Twitter searches.

Using the twython library, I’ve written this excruciatingly simple script to archive the results of any twitter search.  It just pickles the search results and writes them to a file with the current date & time.  I’ve put this in … Continue reading

Posted in General | Tagged , , , , | Leave a comment

Python Multi-dimensional dicts using defaultdict

I asked previously how to avoid code like this: # Implement d[3][4] = 5 if not 3 in d: d[3] = {} d[3][4] = 5 I found the answer, and it lies in collections.defaultdict. defaultdict is just like dict, except … Continue reading

Posted in General | Tagged , , | 1 Comment

Python: str(None) should return ” not ‘None’

Think about the following code: x = some_function_that_might_return_none() if str(x): do_one_thing() else: do_another_thing() do you think do_another_thing() will ever execute? Did you know that str(None) is ‘None’ and thus: if str(None): this_willl_always_execute() I can see how you might want str(None) … Continue reading

Posted in General | Tagged , , | Leave a comment

Multi-dimensional dicts in Python & Django?

The code I’m working on frequently uses multi-dimensional dicts in Python.  There is a bunch of code that seems to be always doing this: # a,b, and c have come from some external data source, QuerySet, etc. a = ‘axis_1′ … Continue reading

Posted in General | Tagged , , , , | 5 Comments

Subject Oriented Programming (SOP) vs. Object Oriented Programming (OOP)

When I’m programming, I usually anthroporporphize the code and the actors involved, and that means when I’m thinking about program flow, I think of it as if they’re real actors doing the tasks needed, and then I convert that back … Continue reading

Posted in General | Tagged , , , , , , | 3 Comments

summing datetime.timedelta objects in Python

Argh! The following snippet is totally broken: import datetime times = [datetime.timedelta(hours=1), datetime.timedelta(minutes=30)] print sum(times) print min(times) print max(times) It raises “TypeError: unsupported operand type(s) for +: ‘int’ and ‘datetime.timedelta’” The thing is, I want this function to be able … Continue reading

Posted in General | Tagged , , | Leave a comment

If you program, you should watch this.

The video linked below sort of blew my mind. The integration and instant feedback between test & code The notion of “spec” (not “unittest”) The spliraling nature of the way he writes the code. Here’s the blog post with the … Continue reading

Posted in General | Tagged , , | Leave a comment

Announcing Parents Guild

Just a quick note to my readers and searchers that we’ve launched our new parenting website, Parents Guild.  We’ve got question & answering, e-mail notifications, tagging, voting, and a bunch of other parenting-specific features in the works.  We’ve got about … Continue reading

Posted in General | Tagged , , , , | Leave a comment