Learning Spanish

For the past few years I have been trying to learn Spanish. My main motivation is that I enjoy traveling in Latin American and speaking the language opens up many opportunities. I have tried several different methods, cds, books, rosetta, immersion school in Costa Rica, personal tutors etc. They all have their benefits but in many ways my current method is my favorite when all the time and cost is factored in.

What I am doing now is paying for a personal tutor who lives in Guatemala to talk with me over Skype twice a week. The cost is fair and I get the added benefit of learning about the culture there which is exactly the vocabulary and context I am interested in. Things like local politics etc. My favorite thing I learned had to do with the most recent presidential elections in Guatemala. The president is elected for 6 years and cannot be re-elected, and neither can they be followed by a close family member. Last year, the president and first lady got divorced so that she could run for president but in the final weeks the Supreme Court ruled her ineligible, although I think the divorce was still valid.

I also learned about the difference between Elote and Maiz.

There are several services you can go through to find a tutor, I ended up just working directly with mine. If you are interested in trying it out, let me know in the comments and I will hook you up.

Posted in Uncategorized | 1 Comment

Coder’s Block

I have tried before to write about what makes programming hard. I’m not really happy with that post or the followup. I still believe that Complexity in all forms is the biggest problem thought its not the only one. Sometimes designing an algorithm can be really challenging even for something which feels pretty simple. Same goes for things like performance optimization. It can be pretty difficult to properly analyze a system to determine what parts are slowing it down even if the system (or at least the part of if you have any control over) is pretty small.

But even beyond all that, much of the time it seems that the really hard thing about building Software isn’t writing the code, it’s deciding what code to write.  Its one reason we see so many projects which amount to “X now written in Y”. The objective and scope are clear and easily understood. Lets fire up vim and have at it!

Currently I’m building a large software product, hoping to turn it into money in the future. I don’t have specific tasks or deadlines besides the ones I set for myself and I sometimes fall victim to Coder’s block. Writer’s block isn’t an inability to type – it is a failure to be able to decide what to type next. Coder’s Block is similar.  When I am stuck (aka. hitting Command-R on news.ycombinator.com) it is rarely because I am faced with something I don’t know how to do. It is way more likely that I just can’t decide which of three different methods I could use to do it.

One way I deal with this is I try to keep two different Todo lists. One is the hard decisions I still have to make about how the software should work (Ex. what is the best representation for a huge list of genomic variants). The other is just a list of things I can knock out whenever I am stuck on the first list (Ex. Fix the report CSS styles so they don’t look like crap).  When I was a manager, I tried to encourage this habit among the coders who worked for me, but It mostly came out like I was trying to make them double up on work. Um sorry.

Another technique is to just code your way out of it. This works pretty well for small to medium size blockages. The other day I was stuck trying to decide what kind of reporting system I wanted to build. I wasn’t getting anywhere in my head so I just starting using python to think with.  I think I have written it about 3 different ways in the past week but now I am pretty happy with the result.  It certainly is easier to play around with options and keep transforming your code using new technologies than It was trying to do this with a C++ app using Boost and other crap.

Sometimes it is hard to get up the desire to use either approach. That’s when it’s time to step away from the keyboard and go for a bike ride.  Sure you could sit there and refresh hacker news, but it will be there when you get back.

Posted in programming | Leave a comment

Sshuttle

Thanks to @elasticdog I now have sshuttle all set up on my laptop. He has an excellent guide here. Personally I just went with the brew install option and its working just fine. Seems much easier to maintain than setting up a real VPN on the server side.

There is really no server side config at all. Its all just magic.

Posted in Uncategorized | Leave a comment

Accessing the Child Class In Django Multi-Table Inheritance

Django has 3 different ways to deal with model inheritance:

The differences are described pretty well in the linked documentation so I am not going to recapitulate them here. I just want to talk about a specific pattern I have been playing with for Multi-table inheritance.

In Multi-table inheritance the base class has its own separate table. This makes it possible to query all the objects from all the subclasses at once.

So if we have:

class Animal(models.Model):
    def speak(self):
        print "Generic Animal Sound"
 
class Dog(Animal):
    def speak(self):
        print "Woof!"
 
class Cat(Animal):
    def speak(self):
        print "Meow!"

The Animals.objects.all() will find all the Cats and Dogs in the DB. The trouble is they will all be of type Animal and so if you run:

Dog.objects.create()
Dog.objects.create()
Cat.objects.create()
 
for animal in Animal.objects.all():
    animal.speak()

you get:

Generic Animal Sound
Generic Animal Sound
Generic Animal Sound

There is a way to access the child class specific information. animal.dog.speak() will produce Woof! if animal is actually a Dog. It will however toss an exception if it is a Cat. Worse is that testing this out generates some useless Db queries which becomes even more impractical as the number of subclasses increases. One approach is to store the name of the child class with the base class so you only have the one extra query needed to grab the data for the child object. If we create an Abstract base class like:

class KnowsChild(models.Model):
    # Make a place to store the class name of the child
    _my_subclass = models.CharField(max_length=200) 
 
    class Meta:
        abstract = True
 
    def as_child(self):
        return getattr(self, self._my_subclass)
 
    def save(self, *args, **kwargs):
        # save what kind we are.
        self._my_subclass = self.__class__.__name__.lower() 
        super(KnowsChild, self).save(*args, **kwargs)

And derive Animal from it.

class Animal(KnowsChild):
    def speak(self):
        print "Generic Animal Sound"

Then we can run:
Dog.objects.create()
Dog.objects.create()
Cat.objects.create()
for animal in Animal.objects.all():
    animal.as_child().speak()

and get:
Woof!
Woof!
Meow!

I am still honestly not sure if I like this or not but its working out well so far for a reporting system I am making where ReportItems can be one of several different types but sometimes I just want to work with the elements in the base class. A lot depends on how often you need to know the specifics of the child class.

Posted in programming | Leave a comment

Oversampling Canvas for better quality

The html5 canvas is a very fast and simple immediate mode graphics system, but unfortunately the image quality (especially text rendering) is often lacking. A quick and easy way to address this is to oversample the canvas.  This works by making the internal size of the canvas a multiple of the size you want it to appear on the screen. Doubling the size is generally good enough for most purposes.

The following image (made with chrome) shows the difference. The text on top was rendered with a standard canvas and the bottom text was rendered with an oversampled canvas.

 

The code for this is pretty straightforward.

In the document you can set up a canvas with double the resolution “canvas_big”  you need and then use a css style to resize it to the same visual size as “canvas_small”.

<!DOCTYPE html>
<html>
<head>
  <!-- A style to scale down the oversampled canvas -->
  <style> #canvas_big { width: 300px; } </style>
</head>
<body>
  <!-- This is a standard html5 canvas -->
  <canvas id="canvas_small" width=300 height=50></canvas>
  <br />
  <!-- This canvas is oversampled. -->
  <!-- Its internal size is 600 but it is set to display at 300 -->
  <canvas id="canvas_big" width=600 height=100></canvas>
</body>

In the javascript you can use scale on the oversampled canvas so that the same coordinates you were using before will still work on the larger canvas.

<script type="text/javascript" language="javascript">
  // Get the context for the standard canvas
  context = document.getElementById('canvas_small').getContext('2d')
 
  // Draw some text
  context.font = "22pt Arial"
  context.fillText("This text looks bad", 20, 25)
 
  // Grab the context for the oversampled canvas
  context = document.getElementById('canvas_big').getContext('2d')
  // Scale the transform so the coordinates are the same as before
  context.scale(2,2)
 
  // USe the same drawing commands as before
  context.font = "22pt Arial"
  context.fillText("This text looks better", 20, 25)
</script>
</html>
Posted in Uncategorized | 2 Comments

Rorschach Test

I’m not personally a fan of either Ron Paul or Thomas Friedman, but I think these two videos are interesting as some kind of Rorschach test of your feelings about national security.

Ron Paul:

Ron Paul Ad

 

Tom Friedman

Tom Friedman on Charlie Rose

 

Posted in Uncategorized | Leave a comment

The Grok Point

Whenever I have a significant programming task to complete I usually begin the process not by thinking about all the data structures I want, or how the output will look, or what will the biggest concerns for efficiency etc.

What I am usually doing at the start is trying move as fast as I can towards the “Grok Point”. This is the point in the process where you have gained a full understanding of what you are working with. Specifications,  Example Code, Documentation, Tests, etc all of these are tools on the way to this moment as are throwaway script, staring at code,  bike rides, and caffeine.

Before the Grok Point – the code is still doing things you don’t quite follow,  you can’t find the right channel of data in the new undocumented file format,  or you are unable to get even one triangle to draw on the display. After the Grok Point – that data schema you are forced to use may still be lame but you are now its master.

Reaching this point doesn’t mean you are close to done, mostly it just means you can really get started. Generally it means you now finally have a good boundary of the problem. There may still be a huge wildfire out there but you have fire breaks all around it and you know its not going to get out. The Grok Point should be where your ability to plan or schedule begins to have some meaning. Anything can happen, the fire does sometimes jump over the river, but we have at least accounted for all the known unknowns and should be able to really think about how you are going to finish.

I feel that we often don’t give the moment the full credit it is due. We move from start to end tweaking, pushing, testing, etc until we are done. When you have spent some time working on something and reach the Grok Point I think its a good time to stop and reflect. Make notes of what you need to so you can be sure you won’t forget but then maybe its time to step away from the keyboard for a bit. Enjoy it, it feels good.

This is also a great time to think about your goals again for this task.  You are smarter now than when you began. You have just inflected from “WTF is this doing?” to “A-ha!”. At this point anything is possible. Once you start coding again you will be making choices, some of which may be hard to undo.

These moments of inspiration are sacred, use them wisely.

Posted in Uncategorized | Leave a comment

Doing what you aren’t supposed to do with Django tests

So I wanted to run my django test scripts against my development database. The django test system really wants to create a database from scratch but I wanted to see whether I could run against my local scratch copy since its already got a lot of data in it and I am too lazy to make fixtures etc.

Some googling produced alot of very helpful “You are doing it wrong!” comments which is fine as far as that goes but I am not good at taking advice.

Digging a little deeper I found you can override the DjangoTestSuiteRunner. Now we are getting somewhere.

put this in settings.py:


TEST_RUNNER = 'myapp.test_extras.NoTestDbDatabaseTestRunner'

And then define the NoTestDbDatabaseTestRunner:


from django.test.simple import DjangoTestSuiteRunner
class NoTestDbDatabaseTestRunner(DjangoTestSuiteRunner):
    def setup_databases(self, **kwargs):
        pass
    def teardown_databases(self, old_config, **kwargs):
        pass


I’m feeling pretty good about this. I try a simple “test” which just counts the number of items in a particular table.

Only what I get is a completely wiped out local db. Nice.

The trick is that my TestCase was using


from django.test import TestCase

which has its own ideas about what to do with the database and it was wiping it out. Switching to:


unittest.TestCase

or

from django.test import SimpleTestCase

if you are using django trunk. Looks like it fixes the problem, but it doesn’t make me feel any better.

Maybe I should just listen more.

Posted in Uncategorized | Leave a comment

No Virgina, there is no such thing as Write Once Run Anywhere

I think the first time I heard “Write Once Run Anywhere” was the uscd-p system pascal.

From Wikipedia:

UCSD p-System achieved machine independence by defining a virtual machine,

UCSD p-System began around 1974 as the idea of UCSD’s Kenneth Bowles[1], who believed that the number of new computing platforms coming out at the time would make it difficult for new programming languages to gain acceptance.

Ok I was 10 in 1974 and probably wasn’t paying attention right at the start but its safe to say the idea has been around a long time. But read the article and weep about the fact that 37 years later we are still chasing (or hiding from) the same paper tiger.

Being cross platform is easy until you try to actually touch something about the platform. JNI anyone?

Its not just about companies trying to embrace and extend. Its just a hard problem and its everywhere.
Here’s a fun little PySide snippet adapted from some code which bit me yesterday.

from PySide.QtCore import QSettings
settings = QSettings('foo','bar')
settings.setValue('key',False)
a=settings.value('key')
if a:
    print "Windows"
else:
    print "Mac"

So this will print Windows on Win7 and Mac on Snow Leopard. The reason is apparent if you add this line:
print a, type(a)

On Win7 you get:
false <type 'unicode'>
On the Mac:
False <type 'bool'>

Ok so maybe you think the Mac is correct and the Win7 version of PySide has a bug. Doesn’t matter. The worst part is you can “fix” this by changing:
settings.setValue('key',False)
to
settings.setValue('key',int(False))  # or a var which is <type 'bool'>; with a value of False

And it all works. The windows registry (in PySide’s world at least) seems to understand Ints but converts bools to strings. Yay!

Of course we can all think of other examples. The point is just to remember that there is no cross platform development environment and there is unlikely ever to be one.

For a while people wanted to think that the Web was the answer. You know make some cool web app and if you obey the standards it will work for everyone without any special hacks. Ha. Good one.

With current trends in Virtual Machines, I think Write Once Run Anywhere might happen because we will just ship the App as a complete Virtual Machine and the OS will just be something like VMWare with a switcher. Then the switcher will get more and more complicated and there will be competing companies making them. Each will add their own features to differentiate themselves until…

Posted in programming | Leave a comment

Making a nice little Netbook

Step 1: Get an Acer Aspire One
I got mine at Costco, last time I was there they were on sale/instant rebate. Model D255E

Step 2: Add Ubuntu Natty Narwal
I went with splitting the disk in 1/2 because its pretty big and If I want to punish myself I can still run windows 7.

Step 3: Upgrade the Ram
I had a few things which didn’t work for me with 1GB (under windows) so I bumped it to 2G for < $25.00.
This part worked for me:
Kingston ValueRAM 2GB 1333MHz PC3-1066 DDR3 SO-DIMM Notebook Memory (KVR1333D3S9/2GETR)

http://www.amazon.com/gp/product/B0039Z81JK.

I had tried an different one from crucial that did not work.

The upgrade is not too hard. The trickiest part is prying out the keyboard. You can push the little tabs back with a tiny flat screwdriver and then you pry out the keyboard. I recommend a credit card to help. Then you remove 4 screws marked “DOOR” and you can then push out the door at the bottom to access the Ram. There are vids/howtos on Youtube.

Step 4: Add a wireless mouse and keyboard

Step 5: Connect it to a big monitor

Less cool factor than an Ipad but cheaper and no real limits on what kind of software it can run.

Posted in Uncategorized | Leave a comment