a blog by Marius Gedminas

Posts tagged Python

Rackspace OSS

I've a Jenkins server running tests for most of my open-source projects now: https://jenkins.gedmin.as/. It was not too difficult to set up: Send an email to Jesse Noller about that free Rackspace Cloud hosting for OSS projects, expecting to be rebuffed because all my projects are small and insignificant. Receive a positive response with the instructions. Yay! Sign up for an account, type in all my credit card details and personal phone numbers, get it verified by a phone call (I hate phone calls, but apparently it prevents fraud or something).

Adding "Edit on GitHub" links to Sphinx pages

Documentation pages on ReadTheDocs have a nice sidebar with extra "Show on GitHub" and "Edit on GitHub" links. Here's how you can have those for your own Sphinx documentation: Create _ext and _templates subdirectories. Create a file _ext/edit_on_github.py that hooks into html-page-context to add a couple of template variables. Create a file _templates/sourcelink.html that uses those two variables to insert "Show on GitHub" and "Edit on GitHub" links in the sidebar.

Logging levels and hierarchies

I remember when the logging package seemed big and complicated and forbidding. And then I remember when I finally "got" it, started using it, even liked it. And then I've discovered that I didn't really understand the model after all. Consider this: we have two loggers root \ -- mylogger configured as follows: import logging root = logging.getLogger() root.setLevel(logging.INFO) root.addHandler(logging.FileHandler("info.log")) mylogger = logging.getLogger('mylogger') mylogger.setLevel(logging.DEBUG) mylogger.addHandler(logging.FileHandler("debug.log")) What happens when I do mylogger.debug('Hi')?

trace.py and os.fork()

Here's a simple Python script: import subprocess with open('hi.txt', 'w') as f: subprocess.Popen(['echo', 'Hello'], stdout=f).wait() Here's what it does: $ python2.7 hi.py $ cat hi.txt Hello And here's what happens if you try to use the trace module to trace the execution: $ python2.7 -m trace --trace hi.py (lots of trace output omitted) So far so good, but take a look at hi.txt: $ cat hi.txt subprocess.py(1170): _dup2(errwrite, 2) --- modulename: subprocess, funcname: _dup2 subprocess.py(1164): if a == b: subprocess.py(1166): elif a is not None: ...

logging.config.fileConfig gotcha

If you use logging.config.fileConfig (e.g. because you use paster serve something.ini to deploy your WSGI apps) you should know about this. By default fileConfig disables all pre-existing loggers if they (or their parent loggers) are not explicitly mentioned in your .ini file. This can result in unintuitive behaviour: (if you don't see the embedded example, you can find it at https://gist.github.com/1642893). If you have Python 2.6 or later (and you should), you can turn this off by passing disable_existing_loggers=False to fileConfig().