Monday, March 29, 2010

Python class attribute annoyance

Python class attributes are fairly handy things, if somewhat visually misleading. Coming from other languages, you might expect this:

  class Foo(object):
    a = 10

To define a class whose instances will have one attribute called a. Not quite. a is actually what most languages call a "static attribute" or "static member" of the class itself, not the instances. Python calls these "class attributes." Once you know this, class attributes are a tool you'll reach for in a number of circumstances, but they have subtle behaviors that can feel like bugs.

For example, today I was trying to do something like this:

  class Foo(object):
    a = 10
    b = a + 5

which works just fine and does what you might expect (a is 10 and b is 15). But, this will yield an error:

  class Foo(object):
    a = 10
    b = [ a+i for i in range(1,11) ]

You might expect b to contain [ 11, 12, ..., 20 ] but instead, you get an error telling you that a isn't defined. [Note: tested in Python 2.6 and 3.1] This subtle flaw exists because that a+i is actually being executed in a nested lexical scope, but because it was created inside of a class body, it fails to inherit what appears to be the parent scope and thus has no access to its lexically scoped variables. There are many ways to accomplish what you might have intended, here, but none of them are very clean. For example:

  class Foo(object):
    a = 10
    b = [ z+i for z in (a,) for i in range(1,11) ]

Now you are passing a as a parameter to that nested scope, so it works perfectly. It's certainly a stilted way to do this, but it works just fine.
Coming, as I do, from Perl, this feels very odd. Perl's OO model is, at best, a framework upon which to build your own. Even still, this kind of scoping problem just never happens. Any lexical scope introduced anywhere in Perl will have a parent scope which is visually quite obvious. Running into such subtle shifts in Python's behavior seems counter to its stated goal of simplicity and elegance.

Friday, March 26, 2010

Safe browsing and virus removal

Sometimes you have to use Windows. There might be a game you like that only runs there or you might need a Windows-only program for work. Whatever it is that draws you to Windows, you know going in that you run the risk of your system becoming compromised (becoming a "zombie," getting a key-logger or any number of other harmful scenarios). OS/X is starting to feel the heat of increased market share as well, in case Mac fans thought they were somehow immune. In 2009 and now in 2010, Mac/OS + Safari did quite poorly in a challenge to compromise browsers. My brother just recently got some sort of malware that caused him to spam the family with bogus links, and I put together this overview of what to do in response. In case it's useful to others, here you go:


1) Always use Firefox to browse the web (Safari and Chrome are getting there, but currently don't have the suite of helpful and stable plugins that Firefox does)
2) Always use the noscript plugin for Firefox and add exceptions with care
3) If you're going to visit a site that might be questionable, use the "Tools -> Start Private Browsing" feature

Doing anything less is roughly equivalent to going on a sex tour of the third world without condoms. That's not a pretty metaphor, but neither is having your system infected with every bot this side of Robbie from Forbidden Planet.

As for cleaning your existing system... it's hard. The best and safest way is to back up your data and then use the re-install/recovery disks that came with the computer. If you want a less drastic approach (that isn't as guaranteed to work), then I suggest one of these resources:

AntiVir removal tool -- Avira, makers of my favorite free antivirus tool
McAfee Virus Removal Tools -- McAfee (about $90)
Symantec Removal Tools -- Symantec removal tools (free?)

I suggest figuring out what you have first. AntiVir, McAfee or Symantec can be used to do a full scan, and should turn this up. If not, try a malware removal tool like Spybot Search and Destroy SpyBot Search and Destroy (but be careful if you do a Web search for it... don't click on ads, and make sure you spelled it correctly).

To keep yourself safe in the future, make sure you have an up-to-date virus scanning tool (AntiVir has a free version that pops up a single ad for their product only, per day, asking you to buy the full version and there are paid programs from Symantec and McAfee). Also, make sure that you run the latest version of your browser (Firefox will auto-update with security fixes, but you should upgrade to the latest major version at least once every 6 months). Don't use IE. but if you really must, make sure it's updated to the very latest version. Microsoft's track record for keeping old browsers secure isn't very good.

Beyond that, consider doing everything that isn't Windows-specific in a virtual machine. You can get an easy-to-use virtual machine manager at and then download the install image for Ubuntu Linux and load it up in the virtual machine. This allows you to do things that would otherwise be unsafe in Windows within a safer environment. It's cumbersome, but the security return on your investment is well worth it.

Monday, March 22, 2010

Peter Watts: An author to check out

Peter Watts was an interesting author before he became national news. But in light of his treatment at the hands of U.S. Border Police and subsequent conviction on a charge which is as absurdly broad as it is injust, I'd suggest that looking into his work now is probably the easiest way to say, "asking a police officer why you're being detained and searched should not be a felony."

You see, Watts was crossing back into Canada when he was pulled over for what has recently become a routine, random exit search. He made a mistake... a big one. He got out of his car and asked why he was being searched. What he didn't know was that getting out of your car is interpreted as a dangerous and hostile act by police. It's an unfortunate consequence of the adversarial relationship we have with our police force (and they with us) in the U.S. What happened then was a series of increasingly wrong decisions on everyone's part. Watts was ordered back in his car, which he did comply with. An officer got into his car and and punched Watts in the face. Watts was then ordered back out of his car. He asked again what was going on and why he was being treated this way. He was ordered to get down on the ground which is when he again asked why (I'm reminded of The Nightmare Before Christmas and the line, "you aren't comprehending the trouble that you're in.") This is when the police maced him with pepper spray.

In the end, Watts was charged with assault and failure to comply. The officers claimed that he struck first, but this was later refuted in court and different officers' stories were shown to diverge. Anyway, the bottom line is that he wasn't convicted of assault, but under current statutes, that failure to get down on the ground after being hit carries exactly the same penalties. That's right, he committed the same crime by inaction as by attacking a police officer.

So, on the one hand, I'd request that people please support this author. He acted in a way that certainly didn't help, but no one deserves to be hit, bullied and pepper sprayed just for (literally) getting out of line. That's not the kind of country we're supposed to be living in.

On the other hand, we clearly need to change this law. Police should be the first in line demanding that it be so. After all, under the current law, you have nothing to lose once you fail to comply with an officer. Getting physically abusive won't escalate the nature of your crime (at least in terms of the assault charge, there might be additional charges that would apply at that point)! That's just wrong, and police and average citizen alike should demand that failure to comply be separated out with a reduced penalty so that real criminals and people who are just slow to recover from being punched in the face aren't treated the same.

To those who are unsympathetic toward someone who is attacked in this way, keep in mind that purely for selfish reasons, the United States really can't afford to make every non-U.S. Citizen afraid to visit the U.S. Tourism and the sorts of business exchanges that require face-to-face meetings are essential to our continued economic growth. Incidents like this reduce the number of people coming to the U.S. which directly reduces the number of jobs in the U.S.

Friday, March 19, 2010

Following the Health Care Bill

I've been trying to catch up on the state of the health care bill... it's not easy. There are some good sources, though. PBS has a really excellent comparison on the House, Senate and reconciliation versions of the bill and the Washington Post has a very informative timeline of the changes that the bill will bring. There are some details there that I hadn't realized previously:

All employers over 50 employees would be required to provide insurance for their employees or pay a substantial fine per employee (though you don't pay for the first 30, even if you're over 50). Most people will be required to get insurance if they don't get it from their company, but there will be exceptions (those now covered by Medicaid, those who file a waiver for religious reasons, etc.)

The religious reasons exemption kind of bothers me. I understand that there are those who don't want to seek medical care because they feel that their deity of choice doesn't approve. That's fine, but I don't get to opt out of paying for highways because I don't have a license... and I'm fine with that. It's just one of those infrastructure costs. If the fees for not getting insurance were about the same as the cost of insurance, then I wouldn't see any problem with removing the religious exemption.

Another great source is the Christian Science Monitor (no relation to my above statements). They've always been a great news source, and that continues to be the case. Their coverage of health care reform includes some interesting insight, including the point that requiring people who don't get coverage through work to buy insurance is partially a way to increase coverage, but also helps the insurance companies as many new customers will be young people who are less at risk for expensive conditions. This means that the insurance companies will be able to immediately recover some of their lost margins due to being forced to insure those with pre-existing conditions. Of course, they'll still try to raise rates in response, but I'm seeing some major resistance to that... my company just announced that they're switching providers, probably due to increased rates, though I don't know for sure. Point being that competition will probably prevent these rates from getting too far out of control, though not as much as if there'd been a public option.

The New York Times has a nice opinion piece that covers some of the bickering that's going back and forth right now. It does a reasonable job of tearing apart some of the talking points on both sides (I like the insight about gaming the C.B.O.)

Of course, you can always browse the top stories from lots of sources via Google news, which I like to do every couple of days to keep up on the twists and turns.

There's an interesting bit on the Wall Street Journal site about how the bill's proposed system "does not adequately pay doctors and hospitals in some areas for treating Medicare patients," but that appears to be an existing Medicare problem that this will would expand by increasing enrollment. Pelosi says they're working that one out, now.

No matter what it will be an interesting ride. You can follow the current odds of passage over at Intrade, which I've always been fascinated by as a predictive tool. They're edging up toward 85% right now, but have recently been below 50%, I assume as a result of various announcements coming out about who's voting which way.

Friday, March 12, 2010

iTunes vs. Amazon

Have you ever compared iTunes and Amazon for music download services? I have, and while both services have something unique to offer, I can quickly point you to the two largest differences between the two. Here's a link to a page on Amazon:

 Free Albums on Amazon

And here's a link to a similar service through iTunes.

 Free on iTunes

You will notice these differences:
  1. The iTunes link doesn't go to a Web page that has the items on it. Instead it goes to a nearly blank page that, if you have the right browser features, will allow you to launch iTunes to go to the iTunes store.
  2. The Amazon mp3 store has 120 free albums for download. The iTunes store has two songs and a music video. No, I'm not kidding, check it out.
Enough said? Of course, free music isn't all there is, but I figure it's worth going through those 120 albums from Amazon first and then maybe browsing their over 1,500 individual free songs and then think about which music service you want to patronize for your non-free music purchases.

Thursday, March 4, 2010

Avatar: The Last Airbender

When I started seeing ads for the Avatar: The Last Airbender movie from M. Night Shyamalan, I was a bit surprised. All I knew of the animated TV series was that it was on Nickelodeon and everyone knows that there are two kinds of shows on that network: drivel aimed at keeping kids quiet while their parents do something else and shows that get canceled fast (c.f. Invader Zim). Throw in the confusion caused by the James Cameron movie, Avatar, which had nothing to do with the show, and I just needed to watch a bit of it to get the facts straight.

So, with every expectation of hating it, I watched the first season on Netfix via their TiVo instant-watch player. I was hooked.

To be sure, it's a kid's show. The characters are mostly children; the stories tend to be simple morality plays; and there's all the cute animals you could want (or orders of magnitude more if you're like me). So why did I like it? For starters, it's a solid story that's well written and that always draws me in. For another, the story isn't your average U.S. fare.

Let me describe the outline (spoiler-free), first. The story takes place in a fantasy world where four nations represent the four alchemical elements of fire, air, water and earth. This is not merely symbolic. There's a sort of shamanistic magic called "bending" in this world, by which people can manipulate these elements (the Fire Nation's benders can manipulate fire, and so on). These nations are currently at war and the only one that can end the war is Aang, the Avatar. An Avatar is born to each generation and has the ability to manipulate all four elements, but the current Avatar is 100 years late and a child who hasn't been trained in anything but the art of air bending.

The tale goes pretty much where you'd expect: he seeks out masters of the other three arts to train him and encounters friends and adventures along the way. Standard children's fantasy. Except...

What I didn't expect was a story about the nature of personal transformation and the acceptance of responsibility for one's own destiny. The story explains, in the basic terms of a show aimed mostly at children, many of the Eastern traditions of energy manipulation and meditation along with reincarnation, karma and some anti-totalitarian politics for good measure. It's a bit like the Promethea comic, but aimed at a younger generation and with far less sex.

I strongly suggest seeing the original before M. Night takes a swing at it. Even if his movie is excellent, I really don't think you'll want to watch the series after the movie, just because it's such an investment of time.

We're just about to start the third season and can't wait to find out what happens to our now favorite characters.

Tuesday, March 2, 2010

Has Anyone Ever Used a Meta-Naming Scheme?

Today at work, we set out to name some conference rooms that had previously only ever had numbers. Lots of themes were suggested from local landmarks to moons and planets from Star Wars. That got me to thinking about the Geeky Science Fiction Themes Meta-Theme. Here's how it works: when you need a name for your machine, project, conference room or whatnot, and you want to be assured that you'll never run out of names, just start using the names of cliches from science fiction and fantasy. Here's a few names you could use:
  • Alien Units of Measure
  • Alternate Earth Presidents of the United States
  • Powerful Artifact Weapon That Only the Prophesied One May Wield
  • Landmarks of Science Fiction That Were Turned Into Terrible Movies
Imagine planning a meeting and telling your co-workers to meet you in Alien Animals That Are Really Just Chimeric Earth Animals at noon! It's guaranteed that they'll remember!

Some tips: make sure the first word is unique as many people will tend to refer to a room by that word (e.g. "Alien," "Alternate," "Powerful," and "Landmarks" from the above list). Also

Meds... I need to find my meds....