Saturday, December 11, 2010

Consider the tuple...

I remember learning Python and wondering what a tuple was for. Why wouldn't you just use a list? Or a dict?

What I've come down to is a few thoughts such as:
1) if I want sortable items, use a list. (list.sort in Python is way fast!)
2) if I want to use an item as a key and find the related info (value), put the key/value pair into a dict.
3) if I want to have possibly-heterogenous-but-related items in a specific unchangeable order, consider a tuple.

Tuples can often be overlooked as a data structure in Python - but can be really useful for things like x,y coordinates or GPS coordinates or timestamps or addresses. It's important to have them in order - you don't want to mix up the x and the y, or the hour and the minutes. You can't do much *to* tuples, they have no methods and are immutable(unchangeable), although you can search in them with in. If you have a tuple that needs something changed, you'll just have to replace it with a new tuple.

Of course, a kewl thing in Python is that you can mix and match. You can have a list of tuples. Or a dict of tuples. Tuples are really useful as keys in a dict, because they're immutable, unlike lists. So, for example, you could have a dict, with keys that are tuple of gps coordinates, and values of a place name at that location, or a house price at a particular address, or you can use "in" to find all the keys that have, say, Sunnyvale, as the name of the city, and find out some value related to Sunnyvale.

Lists and dicts get all the press in Python, because they're really useful and fast. But tuples are pretty awesome in their own way...

EDIT to add examples:

In a list mylist = [2,1], if you call mylist.sort() you get [1,2]. If 2,1 is a coordinate point, then sorting it to 1,2 is a REALLY BAD THING and could lead to really bad bugs.

OTOH: mytuple = 1,2 and you call mytuple.sort(), you get an error because (1,2) is not sortable. This is a GOOD THING, leading it to be usable as a hash (key) in a dict, and for identifying specific things.

A list has no business being coerced to a tuple in order to act as a key in a dict. I think that's an abuse of list. You'd be better off thinking of tuples as a record in a database. "213 1st Street" means nothing when sorted in a list ['1st', '213','Street'), but means everything when left as a tuple ('213', '1st', 'Street') as it should be. Tuples are great for data - when the position *means* something, not just an "ordering". People ask, "why don't you just have named field". A phone number doesn't really need named fields for the 800 to mean something and for it to be important that it not have its order rearranged. Take the following two tuples: (408,555,1212) and (555,408,1212). If you treat them just as lists, you could end up sorting them and they'd be "identical". But they're not identical - they're phone numbers for completely different parts of the country and the structure is meaningful. Which means, tuples would be appropriate here, and that structure is what makes them good as keys.

Tuesday, August 3, 2010

Recipes aren't for following

Saw this recipe in my latest issue of _Bon Appetit_ for curried red lentil, kohlrabi and couscous salad. Looked interesting BUT...
DH doesn't like curried lentils so much, and especialy not as a salad. He likes lentils, just not curried. Also, we need to have some extra protein due to diabetes II in the family. Finally, I felt like pulling out my crockpot today.

So....I made two dishes today: Salt Pork with lentils and kohlrabi, and eggplant caponata.

Chopped 3 onions (2 white 1 red) and sauteed them in olive oil until soft
Tossed half into a crockpot with a chunk of salt pork from the freezer and some chicken broth to cover. Turned crockpot on high. Reserved the rest of the cooked onion for caponata.

Chopped 3 bell peppers (2 orange, 1 red), 2 stalks of celery, 2 carrots, and 3 kohlrabi.

I still say Kohlrabi looks like an alien. But it tastes somewehre between a potato and a radish. Interesting. So, I took the leaves and rinsed them, and chiffonaded them and added them to the crockpot. Then I chopped the kohlrabi bulbs. Even though they were small, they had some touch parts, so I will get bigger ones next time. I had tried doing the "boil 30 seconds then shock in ice water" trick to get the skins off but that didn't help. So I just took a paring knife to them.

Dropped about half of the chopped veggies into the crockpot. Added the lentils (about a cup of red lentils) and poured in broth to cover.

What to use for spices?

I decided to go with mustard and paprika(to go with the salt pork); black peppercorns and garlic powder, of course; some cumin seed to add some highlight; and then saw the black coriander. So I added one of those, and ground the works up in the grinder. Added half (about a tsp) to start along with about a tsp of salt.

A couple hours in, I added another tsp or so of spices, and some hot water cuz it was looking a bit dry.

All in all, it cooked about 6 hours, about 2 on high, 3 on low, then another hour on high to finish it off and make sure everything was soft. When it was time to eat, I took out the pork and pulled it into chunks to add to the plates, (removing the fat and bone).

I took a cup plus of the cooking liquid in a small saucepan, brought it to a boil, and added a cup of couscous. After setting 5 minutes, I fluffed it with a fork.

For presentation, I put down a swipe of good dijon mustard on the plate, piled the pork and a half cup of couscous on one side, then filled the rest of the plate with mixed salad greens topped with a cup of the lentil, kohlrabi mix.

Yummy and filling. We had it with a white French burgundy. It didn't need anything else. It was SOOOOO good. Yum! And all because I saw a recipe for curried lentils and couscous... ;-)

Oh - and the caponata got prepped in the meantime so it's ready for tomorrow's dinner. But that's another post...

Monday, July 5, 2010

Living off the Land(line)

So, I just finally hooked up a phone to the landline. Mind you, they installed that at the same time as our internet. I just hadn't actually plugged anything in yet. OTOH - I have used my cellphone extensively. Now, I've got to update my Google Voice number to ring the landline.

It was odd, telling Alex that we needed to get a new phone. It wasn't clear that I didn't mean a cellphone. ;-) So when I walked into Best Buy tonight to grab a new phone, I was prepared to ask for a landline phone. The default is no longer landline - it's cellphone.

Even odder is realizing as I type this that it isn't really a landline! It's a VOIP service with AT&T Uverse. So, I guess we're not on the phone lines anymore, for real. Okay, I realize that many folks have switched already; it's just briefly unsettling to think about the change. (I feel so behind!) Guess now we have finally and truly joined the digital age.

Sunday, June 27, 2010

The importance of being Ernest

We have a new member of the family. He's a small black lab mix, come all the way from Taiwan to be adopted. When we adopted him yesterday, they told us he had been adopted first about a year ago by people who wanted a puppy. Two days ago, instead of returning him to the no-kill, doggie rescue people, the jerks dropped him off at a kill shelter! Luckily, he's chipped and the doggie rescue folks rescued him before he was euthanized. When we adopted him yesterday, they were calling him Turnip! (Shades of Black Adder...)

So, we have determined that his name is going to be Ernest. And he's a Very Good Dog.

Apparently, he's also a Very Clever Dog. He knows how to open doors. The pocket doors are less than 2 seconds for him to open. And now he's figured out how to open the bedroom door. The one with the doorknob that turns. Yeah. He can somehow open a latched door with a doorknob. Oh boy.