Python Programming, news on the Voidspace Python Projects and all things techie.
See You All Later
That's it... I'm off to Romania. Have a good time while I'm away.
I've ransacked IT conversations for lots of interesting interviews to listen to. If I find any gems I'll let you know...
I've made a huge amount of progress on the new ini file round tripper - ConfigObj 4. It now reads and writes config files with nested sections .
One of the nicest features is the way it will interface to a validation schema that will let you specify the type of each expected value. It will also convert the value into that type (as well as checking it conforms to that spec).
Of all the config file readers I've seen it has the nicest programmers interface, and I like the syntax for writing config files. For example the way you write list values.
For once in my life I've written quite a few tests - one of them even revealed a bug . At the moment they mainly serve as examples of how to use it.
The writein methods, preserving comments, and the changes to the validation system are all that need completing - plus of course documentation.
It is also now Python 2.2 compatible. The main reason I want it to stay compatible with Python 2.2 is that I'm poor . A lot of my scripts run as CGIs on cheap shared hosting accounts. This is probably not about to change.
I used Movable Python to check compatibility with 2.2 - it wasn't . There were two reasons it wasn't compatible :
- I kept forgetting that if you use constructs like if val in some_string: then val can only be a single character.
- There's no real boolean type in Python 2.2 - this means things like if val is True: don't work. Although val may well evaluate to True, it doesn't have the identity of True. It's easy enough to sort - you just use if val: instead... it's just less readable, marginally slower, and meant changing a lot of code .
Attributes or Members
One of the things I had to decide for ConfigObj was the programmers interface to the data . ConfigParser makes you call a method (a getter) to retrieve data. This is part of reasons that so many other people have written alternative config parsers.
For convenience - almost all of them have opted to provide attribute access to the values of the config file. The values are mapped to attributes of your config object (buy overloading __getattr__ and __setattr__. I think this is unpythonic, and that subclassing dict is a much better way. This is for these reasons :
- Some values can't be attributes. For example print can't be used as an object attribute. This means you have to provide a getter as well. This violates there should be only one obvious way to do it.
- A config file, and it's sections, is a set of keywords mapping to values. This means a mapping object is a very good representation of it. (In fact it's a sequence of mappings - so ConfigObj is actually now an ordered dictionary that keeps to the sequence of the values).
- A dictionary has nice built in ways to iterate over it (and methods like get that allow you to specify defaults). I've overloaded most to stick to the sequence (and allow you to change the sequence) - but it's an interface all Python programmers are familiar with. If you use attributes you couldn't iterate over the attributes because of all of the methods etc...
- If you use attributes to represent the data then you are mixing your methods and real attributes with the data. Leave your object attributes as attributes, and your data as members.
|||And multiline values, list values, and all the other wonderful features .|
|||It's actually a decision I made a while ago - but I wanted to record my reasons.|
Hmm.. I've been a bit quiet recently. I'm also about  to fly out to Romania for a week, to visit the in-laws - so it will be quiet for a little while yet.
The good news is that the long awaited ConfigObj revamp is nearly complete. It already reads config files with nested sections - to an arbitrary depth, using indentation for nesting. It now has a working write method, and an interface to validate.py. This allows you to easily write full validating schema for your config files. It has a very nice programmers interface, and the config file syntax is also nice and simple - no pseudo XML junk .... There's not much left to do  and you can pull the latest version out of SVN. I haven't written new docs (yet) - but for once in my life I've done a barrage of tests that also illustrate most of the functionality.
I've just started using the enigmail plugin for the Thunderbird email program. It provides military grade encryption and digital signing for emails, using openPGP . Very nice . I've started using it because a guy called Nicola Larosa is working on a coding project with me - and he wants my emails signed - so there you go. Very cyberpunk .
Quote of the week from the TBS IT manager, when asked how to solve a printing problem :
"Uhm... write it down ?"
|||Well - on Saturday anyway.|
|||It uses a simple 'INI' file type syntax. Very easy to read/write.|
|||The writein method needs implementing, and comments aren't yet preserved. Everything else works fine.|
|||It's actually a bit easier to use on windoze with the GnuPT client.|
This work is licensed under a Creative Commons Attribution-Share Alike 2.0 License.