We've Moved

The blog has been retired - it's up for legacy reasons, but these days I'm blogging at blog.theodox.com. All of the content from this site has been replicated there, and that's where all of the new content will be posted. The new feed is here . I'm experimenting with crossposting from the live site, but if you want to keep up to date use blog.theodox.com or just theodox.com

Thursday, January 2, 2014

The sounds of (Python) Silence

After a long vacation with my children, I've been meditating on the virtues of silence.

Python is a glorious toybox bursting with fun gadgets to delight TA's near and far.  You can easily use it to stuff anything from database access to a serial port controller into your copy of Maya, which is a always fun (and occasionally useful).  However the plethora of Python libraries out there does bring with it a minor annoyance - if you grab something cool off the cheeseshop you don't know exactly how the author wants to communicate with users.  All too often you incorporate something useful into your Maya and suddenly your users have endless reams of debug printouts in their script listener -- info that might make sense to a coder or a sysadmin but which is just noise (or worse, slightly scary) for your artists.

If you're suffering from overly verbose external modules, you can get a little peace and quiet with this little snippet. The Silencer class is just a simple context manager that hijacks sys.stdout and sys.stderr into a pair of StringIO's that will just silently swallow any printouts that would otherwise go to the listener.

If you actually need to look at the spew you can just look at the contents of the out and error fields of the Silencer.   More commonly though you'll just want to wrap a particularly verbose bit of code in a with... as block to shut it up.  You'll also get the standard context manager behavior: an automatic restore in the event of an exception, etc.