House for Sale

hr978635-1.jpg

$219,900 will get you this lovely house just outside of Nashville.

If you are interested, please talk to our realtor, John Fairhead at (615) 383-6964.

A Default Bug in Django

Not to say that this is a bug in django but rather a bug in how I had written some of my models.

Just wanted to point out a quick tip for anyone else who may have done the same thing as me as well as for any of the django experts out there that might suggest a better way.

What was happening was that I began to notice a wild discreptancy between two datetime columns in several models. They should have been identical, however, they were both getting their dates differently:

...
date_column_one = models.DateTimeField(default=datetime.now())
date_column_two = models.DateTimeField(auto_now_add=True)
...

What seems to be happening with this configuration is that datetime.now() is executed once when the server starts (and/or the module is imported) instead of what I thought would happen when I initially wrote these and that datetime.now() would get executed anytime an instance of the model was created and saved as a new record.

My solution was to override the save method:

...
date_column_one = models.DateTimeField()
...
def save(self):
    if not self.id:
        self.date_column_one = datetime.now()
    super(MyModel, self).save()
...

I am sure there is probably a better way and if you know of one, please leave a comment or send me an email and let me know!

Learning Cocoa: Creating a Preference Pane

I’ve been digging into Cocoa to learn how to write native applications on the Mac. Objective-C is actually pretty nice, once I get past the funky bracket syntax and the notion of sending messages instead of calling functions.

I have struggled to find good sources of help and information on the web. Maybe that is in part because this is a pretty narrow segment of developers (versus something like Ruby, Python, or C#). That being said, I have found pretty fast and good help can be found on the cocoa-dev mailing list.

I first set out to learn about and build a preference pane to capture configuration values that I want for the application I am building (more on that to come once I am ready to unveil what I have planned). In doing so I found some resources that helped quite a bit, yet left a little to be desired.

First the documentation was written for 10.4, but I was working with 10.5, so some of the screenshots were throwing me off. Second, there was one detail in particular that was troubling me and if it weren’t for help from a John Stiles from the cocoa-dev listserv, I would still be spinning.

The one little trick that was keeping me spinning was un-checking “Release When Closed” on the Panel Attributes inspector page:

If you don’t do that then all will work well until you got to select the application menu after closing the preference pane window. The application will freeze and then crash. Makes perfect sense now that it was pointed out to me.

New git Book on Peepcode

There is a new book on Peepcode that covers git pretty deeply and in a way that is more easily digestible for the non-Linux hacker. It also comes with quite a bit of video demonstrating concepts in the book. Both very high quality items are sold together for a very low $9.

This graphic from page 10 of the book made me smile:

Picture 5.png

Finding Commits to Push

After some tireless searching for a git command that would yield a listing of local commits that have not been pushed to a target, I popped into the #git IRC channel and posed the question. I was promptly answered by Mikachu:

git log origin..

Yes, that command includes the two periods.

Obama and Economics

There is a fantastic series of statements/questions from George Will to Senator Obama, most of them highlighting what George refers to as Obama’s cognitive dissonance.

Good stuff.

Here is a sampling of what is in the full article:

You say, “The insurance companies, the drug companies, they’re not going to give up their profits easily when it comes to health care.” Why should they? Who will profit from making those industries unprofitable? When pharmaceutical companies have given up their profits, who will fund pharmaceutical innovations, without which there will be much preventable suffering and death? What other industries should “give up their profits”?

ExxonMobil’s 2007 profit of $40.6 billion annoys you. Do you know that its profit, relative to its revenue, was smaller than Microsoft’s and many other corporations’? And that reducing ExxonMobil’s profits will injure people who participate in mu-tual funds, index funds and pension funds that own 52 percent of the company?

You say John McCain is content to “watch [Americans'] home prices decline.” So, government should prop up housing prices generally? How? Why? Were prices ideal before the bubble popped? How does a senator know ideal prices? Have you explained to young couples straining to buy their first house that declining prices are a misfortune?

Telling young people “don’t go into corporate America,” your wife, Michelle, urged them to become social workers or others in “the helping industry,” not “the moneymaking industry.” Given that the moneymakers pay for 100 percent of American jobs, in both public and private sectors, is it not helpful?

You favor raising the capital gains tax rate to “20 percent or 25 percent.” You say this will not “distort” economic decision making. Your tax returns on your 2007 income of $4.2 million show that you and Michelle own few stocks. Are you sure you understand how investors make decisions?

During the ABC debate, you acknowledged that when the capital gains rate was dropped first to 20 percent, then to 15 percent, government revenues from the tax increased and they declined in the 1980s when it was increased to 28 percent. Nevertheless, you said you would consider raising the rate “for purposes of fairness.” How does decreasing the government’s financial resources and punishing investors promote fairness? Are you aware that 20 percent of taxpayers reporting capital gains in 2006 had incomes of less than $50,000?

You want “to reduce money in politics.” In February and March you raised $95 million.

When looking at this, it would seem that Obama doesn’t understand that much when it comes to macro-economics.

Still No Recession…

Just don’t tell anyone…

Most people think we are in a recession

The doom-and-gloomers out there would have us believe that we are in a recession.

This remains factually incorrect.

With a growth in this most recent quarter, Q12008, of 0.6 %, it’s obvious that the economy is not booming, however, it is not shrinking either.

The classical definition of a recession:

If there are two consecutive quarters of decline in economic activity as measured by a decrease in GDP, the economy is said to be in a recession.

Why is it, I wonder that pundits argue that we are in a recession? Are they responding to the fact that most people now believe we are in a recession? Or are the politicians playing to a crowd?

Good Job Little Brother

I was proud to see a local student finds my brother to be an exceptional teacher and even a “mentor and hero”:

KYUWON LEE, of Ravenwood High School, Parents: Heesang and Eunsook Lee Special Teacher: Parker Altman. Mr. Altman, despite his intimidating exterior, truly has a kind heart. In class, he makes government interesting and fun to learn, especially during the presidential primaries. I’m always excited to go to his class. I’ve learned a lot from Mr. Altman as a teacher, a mentor and my hero.

Duck Typing for Mock Objects

Wikipedia defines Duck Typing as:

In computer programming, duck typing is a style of dynamic typing in which an object’s current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class. The name of the concept refers to the duck test, attributed to James Whitcomb Riley, which may be phrased as follows:

“If it walks like a duck and quacks like a duck, I would call it a duck.”

Coming from a strong background in C#, it took awhile for this feature of Python to seem useful. I was use to creating interfaces and creating a proper inheritance chain implementing these interfaces and/or defining abstract base classes, all to have a good set of mock objects to use in unit tests.

I really love the ability to quickly whip out a duck typed class in Python to make simple mock objects so that I am testing my code in a unit test and not boundary objects (e.g. urllib). I don’t need to test whether my request is properly handled by the server at the urllib endpoint and that the response is properly read and processed. I need to instead make sure that my request conforms to a published specification and that the expected result/response from the server is processed properly. This also allows my tests to run offline.

Here is an example of some mock objects that I use in pyphanfare:

# pyphanfare/tests/mocks.py
from pyphanfare.tests.testdata import data

class URLopener:
    def open(self, url):
        params = url.split('?')[1].split('&') 
        for param in params:
            tokens = param.split('=')
            if tokens[0].strip() == 'method':
                return URLhandle(tokens[1].strip())
    def addheader(self, *args):
        self.headers = args

class URLhandle:
    def __init__(self, method):
        self.method = method
    def read(self):
        return data[self.method]

pyphanfare.tests.testdata is just a Python module that contains test data in the form of a dictionary object indexed by method name that I am calling on the API having a value of XML that is expected in the response.

Authenticating Requests for the Phanfare API in Python

Phanfare has decided to implement authenticated requests in a way that feels a bit clunky in design and a bit tricky to figure out how to get working as it isn’t well documented.

On the initial Authenticate request call, they send back a cookie string in XML that you are supposed to use in sending an HTTP Header, Cookie, with each subsequent request.

Cookie: phanfare2=COOKIE_STRING; domain=.phanfare.com

Being a Python library, pyphanfare uses urllib’s URLopener utility class for the HTTP calls. In order to send a cookie with a URLopener originated request you’ll want to do the following:

cookie_str = 'phanfare2=COOKIE_STRING; domain=.phanfare.com'
opener = URLopener()
opener.addheader('Cookie', cookie_str)
xml_str = opener.open(URL).read()