I finally discovered how cool github is, and have started putting some code up there. My first entry is Record.py. I'm calling it the cure to the bane of __init__" because
- Mutable data structures are a bane to concurrent (multi-threaded) code.
- Writing self.foo = foo, self.bar = bar, etc, is a huge waste of time.
- When you have lots of data structures in memory, tuple-based data uses 1/4 the memory of class-based data.
So, go to the github repo or use it by following the really easy steps:
- Download Record.py from http://github.com/pthatcher/pyrec/blob/master/Record.py
- put from Record import Record at the top of your code.
- make a class by saying something like class Person(Record("name", "age"))
- Never write __init__ again (unless you want mutability).
Update: A commenter (thanks Dan!) pointed out that this is a lot like namedtuple, added in python 2.6. He asked why use this instead of namedtuple. Well, I have to admit that I probably would have never created pyrec if namedtuple existed 3 years ago. I try to avoid NIH syndrome. But it didn't exist, so I wrote pyrec. But I've been using pyrec for three years, so I have some experience on some little things that make a big difference (to me). Here are a few advantages pyrec has over namedtuple:
- It has a nicer interface. I prefer new(val1, val2) to _make([val1, val2]), alter to _update, and class Person(Record("name", "age")) to Person = namedtuple("Person", "name, age")
- I added the setField methods. That's what I use 90% of the time. Only about 10% of the time do I use alter. setField is a lot more convenient.
- With pyrec, you can safely override __iter__ and __getitem__. For example, in Record.py, you'll see the implementation of a LinkedList. I tried doing that with namedtuple, but the overidden __getitem__ clobers the name lookup and __iter__ the tuple unpacking. You can use tuple.__iter__(rec) to get around the latter, but pyrec's .values is a lot nicer.
- pyrec has .namedValues for ordered (field, value) pairs, unlike _asdict() which throws out the order. For many things I use pyrec for, this matters.
- You can improve it! Have looked at the code for namedtuple? Ugly. This is pretty clean, so you can improve it very easily if you need additional functionality which will work with all of your records.