Elegantly making a dictionary of lists

In Python I sometimes want to make a dictionary of lists. So usually I would write something like:


mydic = {}
for something in somethingelse:
mydic[somekey].append(something)

This however will not work, if somekey did not exist so far, because it will raise an exception. But there is help! You can use a collection:


import collections
mydic = collections.defaultdic(list)
for something in somethingelse:
mydic[somekey].append(something)

PyQt dynamic uic example

Here is a minimal example for PyQt4, to set up a main window and connect a simple action to some slot. The documentation on doing this is sparse to say the least. So I hope this is helpful for anyone trying something similar. I assume that you have created a ui_MainWindow.ui file with Qt Designer, which contains a form derived from QMainWindow.


import sys
from PyQt4 import QtCore, QtGui, uic

class MyMainWindow(QtGui.QMainWindow):

def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = uic.loadUi("ui_MainWindow.ui", self)
self.connect(self.ui.actionOpen, QtCore.SIGNAL('triggered()'), self, QtCore.SLOT('open()'))

@QtCore.pyqtSlot()
def open(self):
fileDialog = QtGui.QFileDialog(self, "Open file")
fileDialog.show()

app = QtGui.QApplication(sys.argv)
mainWindow = MyMainWindow()
mainWindow.show()
sys.exit(app.exec_())

Not happy with Qt on OS X…

I really don’t get happy with Qt 4.6 on OS X 10.6. Nokia rates Qt on that platform as Tier 2. Which means, it is not fully supported. This results in stupid things happening. With Licq, I currently have the problem that the whole program crashes with the following message:

+[NSUndoManager(NSInternal) _endTopLevelGroupings] is only safe to invoke on the main thread.

The problem here is that Qt 4.6 for 64 Bit is (rightly so) built using the Cocoa API, instead of the C-only Carbon API. Now it seems that Qt uses the NSUndoManager, which in turn has an undocumented (?) function _endTopLevelGroupings. And as the above mentioned message tells you, it is only safe to call that function from the main thread. Since the qt4-gui plugin of Licq runs in a separate thread, this assertion fails, and the whole program crashes. So this basically means you cannot start up your GUI from a secondary thread with Cocoa based Qt. This, I think, constitutes a bug in Qt.
Update: I submitted a bug tracker item, and erijo is producing a minimal example that will hopefully show the bug in action. Now let’s wait and see.