Python 2.6 introduced namedtuple
, and I have to say: It rocks.
Back to flipping out…
Python 2.6 introduced namedtuple
, and I have to say: It rocks.
Back to flipping out…
If you need to do statistical analysis on data in your Oracle database,
DBMS_STAT_FUNCS
is your friend, especially the SUMMARY
function.
Back to flipping out…
I’ve just finished solving Problem 16. It was a fairly straightforward problem, and at first I couldn’t decide how I wanted to solve it, so I decided to do it both ways and compare them. Here’s the script with both versions of the solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
Clearly, the string
-based solution is more compact, but it has
to be slower, right? string
s are slower than int
s and all that.
Thanks to the optimization efforts of the Python implementers, the
string-based solution is actually ~167% faster on my machine (I used
cProfile on a 10000-iteration loop to make these
measurements).
Back to flipping out…
If you’re using acts-as-taggable-on, you may have noticed the documentation is a bit… sparse. The following are some of the more helpful resources I’ve found.
Acts as Taggable on Steroids
(the
inspiration for acts-as-taggable-on
)Finally, I ran into errors with trying to build a tag cloud when there were no tags to count. My solution was to embed
1
|
|
(or similar) into whichever method on the controller was responsible for
the page to contain the tag cloud so that the default is an empty list
instead of nil
.
Back to flipping out…
Rails 2.3.2 apparently forgot to rename app/controllers/application.rb
to app/controllers/application_controller.rb
, so
Getting Started with Rails fails with the following
error:
uninitialized constant ApplicationController
A simple mv
should fix things right up.
Back to flipping out…
Reversion has to be one of the more clever applications of object serialization I’ve come across lately.
Back to flipping out…
This one took me a while, largely because I didn’t understand what they meant by backtracking. I was thinking of backtracking to a particular point, so I kept trying to use acyclic graphs, sometimes called forests. That’s not really what they meant, though. In the context of Problem 15, no backtracking just means you can’t go up and you can’t go left. After working a 3x3 version of the problem, I thought the values looked a little familiar; I was right. It’s a counting problem we covered in combinatorics oh so many years ago: Permutations with Repeated Elements. There are 40 steps to any given path (we have to go 20 steps down and 20 steps right). Since the definition of backtracking makes any given move down indistinguishable from any other move down, and any given move right indistinguishable from any other move right, the answer is 40!/(20!20!).
Back to flipping out…
One of the classic problems with HTTP Authentication has been the way browsers implement it: there’s no easy way to “log out” of a site. Once you’ve provided authentication tokens for a site, the browser keeps sending it. This decision has put a sharp dent in the adoption of standards-based authentication schemes. It doesn’t have to be this way. Bug 300002 is an enhancement request for Firefox that would add a UI element for telling the browser to stop. So far, this request has languished in undeserved obscurity; let’s put an end to that. Please vote up Bug 300002. If Firefox were to remedy this usability problem with HTTP authentication, maybe other browser makers would follow suit, and standards-based authentication would once again be seen as a viable option.
Back to flipping out…
Problem 14 was a fairly straightforward problem to solve, but I liked it a lot. One reason I liked it is that I enjoy playing with mathematical sequences, but also because it marked my first foray into TDD in Python. I’d heard about doctest, and I really like the concept, so I decided to use it when solving this problem. Here’s the code I used to find the solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
You can execute these tests (assuming Python 2.6) from the command-line
with python -m doctest -v problem14.py
. The output will resemble:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Not only does doctest
make it easy to do TDD, it reminds me somewhat
of literate programming.
It’s obviously not the same, but I think it encourages the sort of
documentation that Knuth would approve. I know it definitely made me
think more about documenting these simple functions.
Back to flipping out…
In an earlier post about JavaScript idioms, I talked about a common idiom for copying an array:
1 2 3 4 |
|
In a comment, Jesse offers the following alternative:
1
|
|
To my eye, this is clearly preferable, and I hope more people use it in the future. Thanks for pointing it out, Jesse.
Also: here’s a good list of JavaScript idioms.
Back to flipping out…