As long as the recursion limit of your installation is not reached, you can flatten nested lists by using this function: def flatten(o): if isinstance(o, list): nonlist = [x for x in o if not isinstance(x, list)] listelems = [x for x in o if isinstance(x, list)] listelems = [flatten(x) […]
python
In some cases, you may want to make your documentation available online. Personally, I don’t like building the documentation at three places: in the office, on my notebook and on my machine at home. Hence, I played around with subversion. Here is the post-commit hook: #!/bin/sh DIR=$(mktemp --directory) REPOS="$1" TXN="$2" […]
Usually, python tracebacks look like this To me, this output is hard to follow. Here is what I like more… … and how to get this for your code import sys, traceback, math def fsexcept(exc_type, exc_value, exc_traceback): frames = traceback.extract_tb(exc_traceback) framewidth = int(math.ceil(math.log(len(frames))/math.log(10))) filewidth = 0 linewidth = 0 functionwidth […]
There is a quite simple file format in order to store atom positions. The text based format can hold single conformations or multiple frames in order to keep track of conformational changes. Although parsing of text based files is considered an easy task, speed might get important if you scan […]
Adding the values from two lists in a pairwise fashion is a rather common programming issue in the daily data processing routine. As python is not known to be the very best basis for high throughput calculations, one should know python’s limits. Starting from the following script, #!/usr/bin/env python l1 […]
Have you ever wondered when the fireworks actually start and how long people remain busy? Yes, everybody is making fun of those who cannot wait until midnight. But most of the people are getting it right: Or the more interesting part: My notebook has pulled an all-nighter to record the […]
During the installation of calibre, I got this error: * Building of dev-python/cssselect-0.7.1 with CPython 3.2... python3.2 setup.py build -b build-3.2 running build running build_py creating build-3.2 creating build-3.2/lib creating build-3.2/lib/cssselect copying cssselect/parser.py -> build-3.2/lib/cssselect copying cssselect/xpath.py -> build-3.2/lib/cssselect copying cssselect/tests.py -> build-3.2/lib/cssselect copying cssselect/__init__.py -> build-3.2/lib/cssselect warning: build_py: byte-compiling […]
A few days ago, I asked myself whether pop music is getting faster over time. In order to check this, I looked for the music tempo in beats per minute (BPM) for most of the billboard top ten since 1970 and calculated the average BPM over time. As you can […]
Today I was asked to calculate the Franck-Condon factors (FC) for nitrogen using the harmonic approximation. As I won’t go into physical details, here’s a two-sentence description: FC factors give a hint on relative intensities of radiation from a molecule and vary from 0 (no radiation) to 1 (quite high […]
I really like emails. Unfortunately, most of them disturb me when working on a particular issue or talking to people. However, as I wish to be recieve an answer rather quickly, I am keen to answer emails quickly, as well. So how to combine the urge to be available all […]
There’s one caveat for iterating over xml.etree elements in python. Although the following code works on iterators – just like in C++ – the code sliently fails in checking consecutive children of the element being iterated over import xml.etree.ElementTree as ET xml = ET.parse('filename.xml') root = xml.getroot() for elem in […]
Let’s assume, we are interested in deriving all allowed term symbols for any electron configuration. Normally, this involves drawing a lot of arrows and coping with possible combinations. The following description is rather terse. Throughout the following text, all electrons in the last unoccupied shell share the same quantum number […]