Nosetests: Capturing log messages written to stderr

This is a tip for using the Python logging module in conjunction with unit-tests.
When using the root logger to write debug messages, e.g.

 import logging
..
logging.debug('x = %s' % x)

to capture the messages and write them to the console when running nosetests, pass ‘root’ to the –log-debug nosetests option. E.g.

nosetests test_module1.py -s --log-debug=root
Advertisements

Vim: pasting text without it cascading

If you’re like me and use highlight + middle click pasting in Linux a lot, you may have tried to paste text in a Vim window at some point… with surprising results.

For example, highlight the following text:

import re
for test_string in ['555-1212', 'ILL-EGAL']:
    if re.match(r'^\d{3}-\d{4}$', test_string):
        print test_string, 'is a valid US local phone number'
    else:
        print test_string, 'rejected'

Pasting it into a Vim terminal window when in insert mode will give you something like this:

mport re
for test_string in ['555-1212', 'ILL-EGAL']:
    if re.match(r'^\d{3}-\d{4}$', test_string):
                print test_string, 'is a valid US local phone number'
                                else:
                                                        print test_string, 'rejected'

Eh?

To make it paste as expected, first use the following command in Vim:

:set paste

Paste your text and it will come out formatted correctly. When you’re done, unset it with:

:set nopaste

(Python code example from http://wiki.python.org/moin/SimplePrograms)