Archive of published articles on November, 2012

Back home

Python Singletons

18/11/2012

In my opinion, good python libraries and frameworks should spend effort guiding you towards the ‘pit of success’, rather than trying to keep you from failing. They do this by spending most effort on things related to the critical path- clear interfaces, simple implementations, thorough documentation.

Which is why singletons are, to me, the worst form of framework masturbation in python. You will never be able to stop people from doing something stupid if they’re determined (in pure python). In the case of a singleton, that means instantiating more than one instance of a type. So spending effort on ‘designing’ singletons is not just a waste of effort, but actively harmful. Just provide a clear way to use a single instance, and your system should fail clearly if it detects an actual problem due to multiple instances (as opposed to, trying to detect multiple instances to keep said problem from happening).

The best method for singletons in python, then, is- whatever is simplest!

  1. Some form of module or class state is, to me, the clearest. It requires someone reading or using your code to know nothing more than the most basic python. Just prefix your class def with an underscore, and expose an accessor function to an instance stored on the module (or on the class). The capacity for failure is minimal and the behavior is clear (it requires no behavior modification to the type itself).
  2. Overriding __new__ is pretty bad but OK. It requires someone to understand the subtleties of __new__, which is a useful thing to teach someone but, are singletons really the time and place?
  3. Using a metaclass is a terrible solution. It has a higher likelihood of failure (how many people understand the nuances of metaclasses!?). Misdirection even for people just reading your code, trying to understand your type’s behavior. Avoid.
The question to ask yourself before doing any of this is, “is a singleton a technical requirement or an architectural preference?” Ie, a single instance of an application event loop (QApplication, etc) I’d consider a technical requirement and make it foolproof (in C?). But technical requirements are few and far between and should be driven by underlying system/OS requirements rather than your code’s design or architecture. If it’s an architectural preference- “there should only be one instance of this manager/window/cache”- there’s absolutely no reason to confuse your code (especially you object’s behavior!) to achieve it. Just use design, documentation, and examples, to show people the right way to use it.
4 Comments

Taking your dog to Iceland

17/11/2012

In late 2011, my wife and I imported our Boston Terrier, Shoni, to Iceland. Since importing pets commonly comes up on foreigner discussions, I thought I’d dedicate a post to our experiences (sorry, no tech writing today!). I’ll also mention, this is specifically for dogs, at the time we did it. Cats may be different (easier and more people have done it, from what I hear), and rules may change. I’m also not going to bother linking to forms- this is a personal experience, not a guide, and there is some amount of work involved in moving your pet to Iceland!

Overall, the process isn’t too hard. Information is pretty clear (and in English), everyone was very accommodating, and there were no surprises from Iceland’s side. I wouldn’t bother getting a service to do it, I’d just do it myself. And our dog came out no worse for the wear.

Anyway, on to the timeline (I’ll list prices at the end):

1. In early September, I applied for a permit from MAST, which is good for up to a year. I should have done this much earlier. I needed to get the permit at least 30 days (something like that) before importing Shoni, which meant by the time I got the permit, she couldn’t come with us in October. This threw everything off and caused a lot of stress and extra shots. So get your permit ASAP. The rest of the process is filled with timing restrictions, don’t mess this one up.

2. There are two quarantine places. Once is in the far north (Hrisey, near Akueyri), one is nearer to Keflavik. They stagger their intake, so one takes pets for a few days in the middle of the month, the other at the end/start of the month. If you have a choice (and are living in the capital area), I’d choose the one near Keflavik, to avoid an extra return flight and less transport overall. I’ve also heard their English is better. Shoni ended up going to Hrisey though, because I screwed up the permit and that’s what worked out timing-wise.

3. There are certain shots that the dog needs to have at least 60 days from departure. There are other shots that she needs no more than 30 or 60 days from departure. Read everything and schedule everything in advance. Don’t mess this up! Though we did a little bit but talked to MAST and everything was fine.

4. Make sure you have an airline-ready travel crate, and make sure your dog is used to sleeping in it! A regular crate won’t do, you may need a special crate for air travel. We lucked out, Casady’s aunt and uncle had one they weren’t using, from their recent international move.

5. Shoni flew from Texas to New York in mid-October, after our wedding, in coach with my Aunts. This was the least harrowing part of the entire trip, even though a 10kg dog in the cabin is too big! If you can book your pet in the cabin, do it (it’ll also be cheaper). Try it even if they may be too big- she flew Jet Blue, and it made me like that airline even more. If she must go cargo, I’ll note that airlines have restrictions and cautions for certain dog breeds during certain months. Boston Terriers are brachiocyphallic (snub-nosed), so they can overheat and die during travel, especially during summer months (usually sitting out on the tarmac). Different airlines have different records and different breeds have different accident rates. But do your research and be safe (if the dog is worth the price you’re going to pay to move her, she’s worth this risk, I’d say). We sent Shoni’s (empty) crate as baggage.

6. It was difficult and a huge burden to place on someone (my Aunts) to prep a dog for an international move. We did a bunch of stuff in Austin, but there was stuff that needed to be done 30/10 days before departure, that my Aunts needed to do. Shoni ended up getting several unnecessary shots; there was lots of confusion and stress in the month she was with my Aunts. On the other hand, breaking the trip into two parts was easier on Shoni. I would have been scared for her life if she went on an 18 hour trip in her crate. The key takeaway is that better planning and less procrastination would have made things easier, but even with mistakes along the way everything turned out fine.

7. Again, read the paperwork. There’s stuff you need to fax 5 days before your pet arrives. There was, not surprisingly, a lot of stress in the last few days. Lots of calls with MAST and my family and making sure everything was good to go.

8. With everything set, Shoni flew Icelandair cargo in mid-November from JFK to Keflavik. It was painless and we got confirmation she arrived, the worst parts were over. The quarantine people picked her (and presumably other pets) up, drives them to Hrisey (or wherever your quarantine is), and they start their quarantine. There is no way to interact with your pets AFAIK, even if you are on the same flight (sometimes you may see them in the luggage area but it isn’t like you can take them out to pee!).

9. Once in quarantine, we asked for updates/photos, and got them. The “warden’s” English was not great so it was difficult to communicate, so I needed to have a friend translate some emails between English and Icelandic. Shoni looked nervous and the place was obviously pretty sterile, but she looked safe and healthy. Shoni is very well-adjusted and adaptable, so if you have a nervous pet she may fare worse. Also, I think you can visit your pets- but I wasn’t about to go to Akureyri as a new resident in December, but I probably would have visited her in Keflavik.

10. After 4 weeks (right before Christmas, for us!), Shoni’s time was up. And here’s another reason to choose Keflavik over Hrisey- Icelandic winter weather is fickle and her flight was delayed several times. She was on one of the last flights from Akureyri. We picked her up from the airport, took her home in a cab, and all was well. She didn’t have any of her toys or blankets she was sent with- the stuff has to be boiled and rarely survives it.

11. Costs. All are approximate.

  • Permit: $240 (including transfer fee)
  • Flight Austin->JFK: $100
  • Flight JFK->Keflavik: $450
  • Akureyri->Reykjavik: $100
  • Quarantine: $1600 (varies with pet size)
  • Customs fee: $100
  • Vet bills: $700 or so, not sure
  • Total: About $3500

In the end, it wasn’t a question of whether or not it was worth it- there was no question, she was coming. However, procrastination complicated things. And it is expensive. Bigger dogs will be more expensive, cats are smaller and require less shots so would be cheaper.

Anyway, I hope this post is helpful, and feel free to ask me any questions about my experience.

6 Comments

Is QA a good stepping stone?

9/11/2012

I’ve always heard that it was difficult to move from QA into development (game design/programming/art/production). I thought this was smart- QA people should be there to be QA people, not doing a job only because they hope it would lead to something else.

And at some companies, it works. My previous job was at a well-regarded studio, working on a huge game and IP, in a wonderful city, and lots of developers were looking for work (and we had the money to hire them, if not keep them after ship…). People really wanted to work there (I once interviewed someone as I was near the end of my tenure there, and told him pretty clearly, “you won’t like programming here.” And of course he took the job anyway). It didn’t make sense to spend effort training QA people when you could just hire the people you wanted directly.

However, I now work at a place that is much more difficult to hire for. Outside people respect the company and know the game, but the audience is smaller than one of the biggest IPs and most successful game studios of all time. And especially, it’s in Iceland. Iceland is a wonderful country, but we’ve had several people turn down offers or on-site interviews because their family said “no way” (myself almost included).

In my opinion, it makes more sense to hire QA people (often out of the EVE or local Icelandic community), and set them up on a career path that leads outside of QA. There will never be a serious shortage of applications for a QA job, but it can take a year to hire and relocate a senior person (and you are taking a big risk on that time and expense!). And to a large extent, this is what we do (and while we have sometimes failed at providing career paths, I still think it remains the goal).

I quite like this new strategy- not only does it produce good results over time, it also seems to have more integrity- QA people are too often treated like second class citizens, rather than devs-to-be. I’m not sure if my mind will change if I move on to another studio, but it’s at least how I feel now. I wonder how other studios approach this issue?

(Side note- I am not saying there isn’t a need for senior and permanent QA people, I’m just saying, there are lots of people in QA that don’t want a career in QA. There should also be career development inside of QA, but that shouldn’t be the only career development)

No Comments

Teaching python decorators

5/11/2012

A few weeks ago, I held a demo about context managers and decorators. Teaching context managers was straightforward, as expected. Decorators, however, are a much more complex concept. So I used Steve Ferg’s article about the right way to explain python decorators. This was a live demo with little preparation (just wrote down his steps and knew I would write decorators to cache and timeout), to a variety of skill levels, and it was unanimously successful. Everyone came away with a much clearer idea of how decorators work- I saw a lightbulb go off in everyone’s head.

I don’t know why it took so long to figure out how to teach decorators (even harder than first figuring out how to use them!), but Steve’s method works, and I’d encourage you to use it to teach anyone who’s interested.

No Comments

PSA: It isn’t dark magic, it’s your anti-virus software

2/11/2012

We’ve all had it- sometimes, under certain conditions, your code errors with an IOError because Windows can’t delete a file. It seems random, and it is. Well, in case you weren’t aware, it’s almost definitely your anti virus software (http://blogs.msdn.com/b/oldnewthing/archive/2012/09/07/10347136.aspx) or at least, some other crazy shit going on.

So a few weeks ago I gave up and said, forget it, I’m just going to write retrying rmtree/remove functions. It took a long time because I refuse to do cargo-cult programming on problems like this, so once I saw that article, I went ahead with it.

I just wanted to spread this information in case you’re in the same situation I was in. You may as well harden and test your little helper retying-delete function I know you have hidden away somewhere, and make it a part of your core libraries.

<No code provided- this is the hacky shit that cannot have a straightforward design and IMO shouldn’t leave your studio, so you’re on your own to figure out an acceptable design and implementation!>

No Comments