Tuesday, February 28, 2006

Compound specs, and error messages

The wonderfully simple and powerful spec module, that comes with the QP Web Application Framework, allows you to build arbitrarily compound specifications against which you can check attribute values, or whole objects even. Btw, I have just realised that David Binger has actually just given a lightning talk over the weekend at PyCon 2006 about it.

Spec offers a generous range of basic specs, such as string (str or unicode), pattern, instance, sequence, and so on. All very useful. It also offers what it calls ConnectiveSpecOperators, that are basically compound specs, examples being no (must not match any), both (must match all), and
either (must match any). As a side note, the names both and either are used instead of all and any to avoid nameclashing with python 2.5 same-name builtins.

One little problem with these compound specs, though, is that you do not know which of the sub-specs caused them to fail, or to succeed for that matter. It would be nice if the first sub-spec instance that caused the match or the failure be somehow bubbled up to the root spec, that can then be queried for by client code if desired, and using that information for example to provide more appropriate error messages to the trying-to-not-get-annoyed-with-technology end user.