Wednesday, October 22, 2014

Why I prefer Python to Java, in two code samples.

Why I prefer Python to Java, in two code samples.


Vagrant/Ansible trick: autoreload

When testing Ansible scripts, it's nice to have your Vagrant VM automatically re-provision itself when you change the configuration.  In this way just saving a file lets you see the VM reinstall packages and so forth, with errors appearing immediately.

The following runs a tiny reprovisioning script each time your Ansible YAML files change:

pip install watchmedo

watchmedo shell-command --drop --pattern='*.yml' --command='clear; vagrant provision'

Hidden features of Python - Stack Overflow

Hidden features of Python - Stack Overflow -- lots of great, semi-obscure features!

Saturday, October 11, 2014

How to write dumber tests

Tests are hard.  The goal is to write a little bit of stuff that finds bugs in your code, you don't want to write unit tests so complex that they have bugs.  Charles (Luke) Lee breaks down some of the challenges, and gives solutions. Video from PyTexas 2014.

I have an entire rant about mocks, but that will wait for a later blog post.

How to write dumber tests

Tuesday, September 2, 2014

preview: Python Library class

I'm developing a course on Python, and another on Professional Software development. Enclosed is a piece, showing the main basic modules in Python and why to use them.

Saturday, August 23, 2014

best practices with Python's multiprocessing module

Thanks to dano and others on StackOverflow, I have more best practices for Python's multiprocessing module:

1) put everything in an if __name__=='__main__' block, vs having top-level code.  This is a Windows thing, but is good practice in general. Main-level code will be executed even if the module is imported, as opposed to being executed directly.

2) I'd previously been using signal.pause() to wait for subprocesses to exit and/or die.  Alas this has two challenges: a) this isn't available in Windows, and b) if a subproc dies, the parent will still hang around until explicitly killed/interrupted.

Here's an example of the right and wrong way to wait for a subproc.  If the signal.pause() path is enabled, then the program will run forever, ignoring the child proc that exits with an error.  The other path is more verbose, but correctly runs on Windows, and handles children correctly.


    import multiprocessing, signal, sys, time
    def wait_crash():
    if __name__=='__main__':
        procs = [ multiprocessing.Process(target=wait_crash) ]
        for proc in procs:
        if 0:
            signal.pause()              # XXX: not on Windows
            for proc in procs: