Read my book

I wrote books about Webpack and React. Check them out!

Friday, November 11, 2011

Utilities - pyqa 0.5.0, iterplus 0.2.0, speccer 0.5.0

Nothing's more fun than building some utilities that make it easier to build more utilities! I just spent some time wrapping up a few of these, namely speccer, iterplus and pyqa. Depending on the sort of development you do, you might find some of these handy. I've developed them mainly for my own consumption but I really don't mind if someone else wants to use them too.

I've discussed speccer, a testing tool, briefly earlier. I developed iterplus based on my post about infinite lists in Python.  pyqa is something I came up with to make it easier to write those scripts that ask a bunch of questions and then generate something based on those. Each utility is available at PyPI. The following examples should give you a better idea of them.


Speccer

I developed speccer because I'm not that fond of Python's unittest syntax. It's way too verbose for my tastes. I have used pytest for a long time. It's less verbose than unittest. Still, I feel there's one "assert" too much. Why not to just treat asserts implicitly?

Speccer does just this. Essentially it's transforms these implicit asserts to code utilizing unittest and then runs that code. That's enough for me. The following example illustrates the syntax. Now try to think what that would look like written in unittest. :)


Here's an implementation of that spec:


If you want to see more complex examples, check out the source of the tool itself. I also used it to test jsopo, my since abandoned JS operator overloading hack. I'm certain there are some bugs in the tool due to the way it works. If you find some issue you want to get fixed, just let me know. The runner probably could be a bit smarter too.

Iterplus

iterplus grew out of my ventures to the world of Haskell. It provides a few simple functions that yield some extra power to Python. The following examples demonstrate this:


pyqa

It's pretty boring to write scripts that ask the user a bunch of things and then do something based on the answers. In order to make this boring task less boring, I ended up writing pyqa. It contains just one function, "ask". This function deals with the user input loop based on the given data and outputs something you can put somewhere (ie. into a template). The following example illustrates basic usage:


The example depends on the following question definition:

And this template:

As you can see pyqa takes care of some heavy lifting for you leaving you to focus on writing your questions and template logic. I think YAML and Mustache suit this task quite nicely.

Conclusion

All in all it's pretty cool to write little utilities like these. It's even better if someone finds them useful. Each tool is available under permissive MIT license. You are free to do pretty much anything you want with them as long as you retain attribution.