Sorting python dictionaries by value

Several times I have needed to invert a python dictionary in order to sort by the values.

Here is a nice way to avoid that using lambda expressions:

sharperatios = {"prlax": 1.63,"letrx": 1.53,"wasax": 1.59}

tuples = sharperatios.items()

tuples.sort(lambda (k1,v1),(k2,v2):cmp(v1,v2))

for key,value in tuples:

    print key , value

Politics and the Subprime Bailout

Bush seems poised to offer an huge bail-out for sub-prime mortgages that are going to adjust upwards in the near future. “Lenders Agree to Freeze Rates”

The political response of republicans and democrats has been pretty much the same. All of the presidential candidates pays lip service to the notion of personal responsibility, but at the end of the day they all propose allowing people to remain in homes that they could never have reasonably afforded.

This is classic moral hazard. Rewarding people for taking crazy risks is bad policy. Rather than saving my money for a down-payment and living within my means, I should have gotten an interest-only loan for a house that I could only afford on the teaser rate. Then I would get 5 years of subsidized interest rates in this bail-out.

Irrational bubbles like this are painful and they should be. I do not know of any investors who got bailed out after the dot.com bust. They took a risk and lost.

So whoever gets elected in 2008, they will be on the wrong side of this important issue.

Geotagging for science

I have recently started geo-tagging some of my pictures that I put on Flickr. It is fun to browse your neighborhood looking for pictures of familiar places and unusual things. But besides the emerging use of geotagging for restaurant and home searches, I think it has value to those great-great-great grandchildren of ours that will choose to be scientists.

Example: Spider 2007

This week my wife and I found a creepy spider in our apartment. We took several pictures of it before we got rid of it and it occurred to me that if other people see the same spider species in their houses and take geo-tagged pictures, this data could provide really accurate estimates of species range dynamics that would be potentially easy to data-mine out of the user-generated web.

Flickr is also a great community for rating pictures so peer-review of the pictures is also possible.

So when I tag pictures that might have scientific value in the future I am going to try to put myself in the shoes of these great future scientists and add the tags that I would wish to have for research.

The fleecing of Silicon Valley with Pet Rent

Silicon Valley housing prices are pretty high and the rents are as well. This is just the supply-demand situation in this really cool area and I can accept that. :) But there is an evil that lives deep in the heart of the Silicon Valley apartment landscape: Pet rent.

Of course pet deposits are standard in most places and they make sense. A cat can do all manner of things to carpets and floors and I am content to put down a deposit. But at least 1/2 of the big property management companies want pet rent ($25-$50 per pet). What is next? Baby rent, spouse rent, plant rent?

My wife and I are looking at apartments right now and the first thing we ask of a building manager is whether they charge pet rent. If they do I explain that we will not pay pet rent and we leave. I hope there are other people out there that do the same and perhaps eventually the practice will fade away.

Our current place does not charge pet rent so I know it is not impossible to find sanity if you look hard enough.

Cryopid…checkpointing for the masses?

I recently learned of an interested project called Cryopid which enables very powerful checkpointing in Linux.

I guess the rather odd name implies that it puts a single process into cryo-freeze. I would have gone with “carbonite“…but that is just me. :)

I should say that I have yet to try this, but it promises to make checkpointing in Linux much more convenient.

According to the website it has the following current features:

  • Can run as an ordinary user! (no root privileges needed)
  • Works on both 2.4 and 2.6.
  • Works on x86 and AMD64.
  • Can start & stop a process multiple times
  • Can migrate processes between machines and between kernel versions (tested between 2.4 to 2.6 and 2.6 to 2.4).

I am glad to see serious work being done to make this very old technology more accessible to everyone.

Condo Prices

As someone who is interested in buying property (a condo/townhouse) at some point I have been quite interested to see that prices for these types of properties have fallen significantly.

For example this plot shows condo price dynamics in the Southern and Western parts of the United States.

Prices for single family homes do not seem to exhibit the same trend and it will be interesting to see how long the decline in condo prices lasts.

Ubuntu 6.10 Finally makes MythTv with IVTV easy.

As I looked at what is new in Edgy, I was surprised and very glad to see a new MythTV version and even more importantly IVTV packages to make setting up PVR-XXX cards easier.

https://wiki.ubuntu.com/Install_IVTV_Edgy

I have had a good deal of trouble getting ivtv to work in Dapper and so I am crossing my fingers that having these packages will make it just work.

I can’t wait to try it out tonight!

There and back again, West LA to Westlake Village.

Once upon a time I was a student at UCLA. I lived nearby and either walked or took the bus.

My new and very exciting job is not near me or UCLA. In fact it is a 35 mile drive each way. As it happens I have two very comparable choices for route on my commute. I can take the very beautiful Pacific Coast Hwy and look at the ocean on my commute. My other option is to take the 405 and 101 freeways which are two of the most congested commuter arteries known to human-kind.

So each morning I start out in West Los Angeles (near Marina Del Rey) and take one of the two routes (PCH or 405) to Westlake Village.

On the plot of one month of data I have the commute times vs. my starting time. For each route I plot the data points, the route average (bold line) and standard deviation range lines (thin lines).

So while I do enjoy the PCH route I am going to be driving the 405/101 route for the near future since the early results are pretty solidly in favor of it. As the statistics change I will post updates for the those waiting breathlessly for information on my commute.

The compiler is smarter than the preprocessor (Item 2)

Effective C++ pp. 13-17 3rd Ed.


The title of this entry in the book is “Prefer consts, enums and inlines to #defines”. As Dr. Meyers mentions this is just a more explicit way to say that you should use the power of the compiler whenever possible. So the practice of using #defines to create mathematical constants is just not a good idea. The preprocessor does not have to keep the name of the preprocessor definition; it can simply replace all occurrences with the defined value. This can make debugging harder. Also the preprocessor can make multiple copies of the constant that can add bloat to the compiled code. Therefore one should prefer the following:const double NatLogBase = 2.71828;to #define NAT_LOG_BASE 2.71828
This is pretty obvious stuff to many experienced C++ programmers. As mentioned in this item, class-scoped static constant integral-type declarations can also assign the variable in the header file. While it pains me to use this feature I do. I use this method rather than the “enum hack” mentioned below. This is needed for the declaration of arrays that have a symbolic constant as the size. Since enums can be used in place of ints it is common to define an enum in the header file of a class for the same reason. A simple example of this can be found here. Static const members were added to the language to replace the enum hack, but Dr. Meyers points out in Effective C++ that it is illegal to take the address of an enum, so this can allow more access control over how constants are used.I will not go into detail about preprocessor macros. They are crazy and should never be used. Use an inline function (possibly templated) instead since it is just as efficient and prevents bizarre side effects and does not require special spacing.

C++ is a multi-paradigm language (Item 1)

Effective C++ pp. 11-13 3rd Ed.


This is an important point for people coming from a language such as Java which does not have the flexibility (and complex syntax of C++).Some of the important programming methods and paradigms offered in C++ include:

  • Procedural Programming (due to the lineage of C)
  • Object-Oriented Programming (Standard OO features including multiple inheritance and dense, but powerful syntax))
  • Generic Programming (Template programming exemplified by the STL)
  • Meta-programming (Run meta-programs inside your compiler for fun and profit! This feature is actually a side-effect that was discovered rather than engineered. Template meta-programming in C++ has some common ground with functional languages such as Haskell. For instance all variables are immutable and recursion is used extensively.)
  • Functional Programming (As mentioned above, C++ meta-programming does have some elements of functional programming. but it is debatable to claim that C++ is a function programming language.)

The key point is that C++ was not engineered to encourage best practices for a particular style of programming the way that Java was. This fact explains both the great power and significant complexity of the language. This also means that best practices may be very different depending on which part(s) of the C++ language you are using.