Skip to content


Build your own kind of dictionary

This is the video from my PyOhio talk on building objects that can act like dictionaries.

The text and programs from my talk on building your own are available here on github. You can probably absorb the information most quickly by reading the talk.rst file.

Please let me know if you find spelling errors or goofy sentences that are hard to understand that I should rewrite.

This is the description for the talk:

My talk is based on a project that seemed very simple at first. I wanted an object like the regular python dictionary, but with a few small tweaks:

  • values for some keys should be restricted to elements of a set
  • values for some keys should be restricted to instances of a type

For example, pretend I want a dictionary called favorites, and I want the value for the “color” key to be any instance of my Color class. Meanwhile, for the “movie” key, I want to make sure that the value belongs to my set of movies.

In the talk, I’ll walk through how I used tests to validate my different implementations until I came up with a winner.

Unlike my talk last year on metaclass tomfoolery, and the year before that on fun with decorators (and decorator factories) I’m hoping to make this talk straightforward and friendly to beginning programmers.

You’ll see:

  • how I use tests to solve a real-world problem
  • a few little gotchas with the super keyword
  • a little about how python works under the hood

Posted in Programming, Python.

The chive dry-erase board girl is a viral marketing campaign

This is all over the intarweb right now.

I’m betting that this is an ad for the monitoring software she supposedly used to track her boss’s time on farmville.

I think we can all agree that invasions of privacy never looked cuter!

Posted in web.

Raphael.js event handlers sometimes require closures

I’m playing around with raphael. In the code below, I use a few loops to make a grid of rectangles. I want each rectangle turn from white to gray when clicked. This is a little tricky in raphael, because you can’t use this or evt.target to get a reference to what got clicked on.


var main = function () {
    var paper = Raphael("grid", 320, 200);
    for (var rownum=0; rownum<3; rownum+=1) {
        for (var colnum=0; colnum<3; colnum+=1) {
            var r = paper.rect(10+colnum*50, 10+rownum*50, 50, 50);
            r.attr({stroke:"gray", fill:"white"});

            r.node.onclick = function () {
                var this_rect = r;
                return function () {
                    this_rect.attr({fill:"gray"});
                }
            }();
        }
    }
};
window.onload = main;

Remember that your typical boring onclick assignments usually look like this:


r.node.onclick = function () {
    alert("your mom");
};

I don’t call the function. I just define it.

Study the r.node.onclick assignment in the main code again, and make sure to notice that the r.node.onclick attribute is assigned to the result of the function defined.

That’s the first big difference. Now you have to figure what the heck is returned by this function. The answer is. . . another function. That other function is what gets linked up to be fired when a click event happens.

Closures are tricky at first, but they’re really useful for situations like this, when you need to pass along references with the functions to operate on them.

Posted in Programming, javascript.

How I cook kale

I’ve watched a lot of kale recipe videos on youtube, and learned a few neat tricks there.

I’ve tried it raw, and I just don’t like it that much. Cooking it may destroy some of the vitamins, but it doesn’t destroy all of them, and I need food to taste good or I won’t eat it.

I bet a really great chef can make raw kale taste delicious.

Anyhow, I’ve been cooking the kale lately by picking the leaves, washing them, stacking them up, rolling them into a tube, and then chopping up the tube. People call this “chiffonade”. You can watch videos on youtube to see how it works. It’s just a fast way of chopping leaves.

Then I throw some butter and olive oil into a skillet on medium heat. And I add some chopped onion and garlic. If I have any crushed red pepper flakes (like from pizza delivery) I’ll throw those in there. I push the onions and garlic around in the oil and butter with a wooden spoon to keep them from burning for about five minutes. The onions will turn a little clear, and the garlic will get golden brown.

At that point, I throw in the chopped up kale. The left-over water on the leaves will steam. Then I just keep stirring it all together.

You can take the kale out whenever you want, so experiment with different cooking times. I like it when it is totally wilted, but before it starts getting brown.

Some times, I’ll add a little more water (like about 1/4 cup) and then put a lid on it to make the kale steam.

Usually I eat kale with a fried egg and toast. Once the kale is nearly done, I’ll push it to one side of the skillet and then crack an egg in the other side and put in a slice of bread in the toaster.

Then that all ends up in a big bowl together, maybe with a few shots of hot sauce on top if I didn’t already add the red pepper flakes. That makes a good breakfast.

The onions get sweet when you cook them, and the combined taste of the onions, the kale, and the pepper or hot sauce really appeals to me.

I’ve experimented with cooking the kale in sesame oil and adding soy sauce, but never really felt like I got it right.

If you really get into kale, you might try growing it. I eat it all so much because it grows so fast. I grow the red Russian variety from seed I purchased at Baker Creek.

Posted in cooking, gardening.

True story

Son #2, Oliver, has been battling constipation for three days now with no relief. He’s been walking around huffing, puffing, squatting all day today. Poor little guy has been miserable.

Anyhow, I turned on some death metal radio show (metal meltdown on 91.1) on the radio really loud, mostly just to pester my wife. After just a minute, suddenly O (that’s what we call him) let out this primal scream and blasted out an epic dook1 into his diaper.

I’d like to see Christian Rock do that.

1. Think about the contents of a family size can of beefaroni.

Posted in Cleveland Life.

Building A Sane Development Environment

For all these tasks, make them easy to do or you won’t really do it.

Start everything in a clean environment

Don’t hack in an environment filled up with artifacts from the previous release. Clear them all out. Make sure that your environment looks like what you released for 1.0 before you start 1.1.

Encourage lots of sandboxes

Be able to explore mutually exclusive solutions to the same problem and not worry about conflicts.

Make your environment realistic

Design your sandbox so that it resembles your production environment.

Just to be clear, I don’t mean that development must be identical to production. I mean that if you anticipate bottlenecks in production, figure out how to model them.

Deploy during development

Deployment should be boring. But it usually isn’t, because developers overlook trivial dependencies they created along the way.

You’ll quickly discover what you forgot to add to your deploy scripts last time if you start every session like this:

  1. Reset your sandbox.
  2. Check out your code.
  3. Run the deployment script.
  4. Now start working.

By the time you are ready to release, you will have simulated the deployment process many many times.

Posted in Programming.

I just submitted my talk for PyOhio 2010

I’d be happy to get any crititicism ahead of the presentation. I’m hosting the code examples and the talk material here.

Building your own kind of dictionary

Python level: novice

Description

My talk is based on a project that seemed very simple at first. I wanted an object like the regular python dictionary, but with a few small tweaks:

  • values for some keys should be restricted to elements of a set
  • values for some keys should be restricted to instances of a type

For example, pretend I want a dictionary called favorites, and I want the value for the “color” key to be any instance of my Color class. Meanwhile, for the “movie” key, I want to make sure that the value belongs to my set of movies.

In the talk, I’ll walk through how I used tests to validate my different implementations until I came up with a winner.

Unlike my talk last year on metaclass tomfoolery, and the year before that on fun with decorators (and decorator factories) I’m hoping to make this talk straightforward and friendly to beginning programmers.

You’ll see:

  • how I use tests to solve a real-world problem
  • gotchas with the super keyword
  • a little about how python works under the hood.

Extended description

I’m not done with the slides, but all my code examples are finished. You can read it all online at my github repository here.

Outline

  1. What kind of object I want
    1. Tests define the expected behavior
    2. How to run those tests
  2. First implementation (subclass dict)
    1. How the implementation is defined
    2. Examine test results
    3. Examine the C code behind the dict class to see why my subclassed __setitem__ method won’t get called from the parent class
  3. Composition-based implementation
    1. Explain composition vs inheritance
    2. Examine test results
    3. Point out irritating need to manually redefine every related dictionary method on the container class
    4. Show how to use __getattr__ to avoid all that boring wrapper code
    5. Show how __getattr__ doesn’t play nice with inspection tools
  4. UserDict.UserDict
    1. Explain implementation
    2. Examine test results
    3. Add a new test that uses this class as a parent for a subclass
    4. Explain how UserDict.UserDict is not a new-style class, so the super keyword behaves differently
  5. UserDict.DictMixin
    1. Explain implementation
    2. Examine test results
  6. PEP 3119 and why it is nice
    1. duck-typing, why it is awesome, why it isn’t perfect
    2. abstract base classes
    3. As of python 2.6, don’t use UserDict.DictMixin; use collections.MutableMapping

Posted in Python, self-aggrandizement.

Moving on

Last week marked the end of my employment with OnShift Inc. What started off as possibly the worst idea for a business conceivable (I’ll justify this claim later) is now an enterprise.

It has been a really good experience. I got to build something all myself. I had some really patient customers that politely filed bug report after bug report, and met with me over and over again to discuss their frustrations.

And then the business crossed one of those inflection points.

We started getting sales. Customers became happy to be references. More than once, people offered to pay for the product out of their own salary after they sat through a demo.

Some fantastic developers and designers joined the team.

In March I sat in the back of the room during a training. At the end, the trainer pointed me out. Usually I take that moment to do my little talk about how this is a new product, and these users are going to run into bugs or things that just seem goofy. I tell them I really want to hear about them, so that I can fix them as soon as possible.

But before I started my speech, everyone started clapping.

So that’s why I have to / get to / want to / need to leave. For about four years now, I’ve been banging rocks together trying to start a fire. And now I got one. So now I’m going to take a break, and perhaps more importantly to OnShift, get out of the way.

I’ll be happy if the business makes it big. I’ve been fully vested in my shares for a while now and I keep those after I leave. But this was never about getting rich. This was about proving something. I aimed to prove that I could design a product and then start a business around it.

I’ve been focused on getting that fire lit for the last four years. Now it is lit.

As for that worst possible biz plan, I’m not going to spell it out. Instead I’ll cram all the bullet points from my partner’s slides into one run-on word salad:

Orthodox Judaism SMS location-based time of Sunset on Friday mobile microblogging opt-in advertising

Posted in startup life.

Farm report spring 2010

Posted in Cleveland Life, gardening.

pitz 1.0.5 now supports bash colorization

Today is day one of my week off. The rainy weather kept me inside, so I added a bash coloring to pitz. The screen shot below shows an example:

screenshot

pretty colors

It took me a while to figure out how to include the jinja2 template files with the package, but you can now download pitz from PyPi here.

Posted in Programming, Python, pitz.