from collections import defaultdict nested_dict = defaultdict(lambda : defaultdict(list))
The defaultdict constructor takes any factory as an argument, a requirement that can be easily be met using python lambda functions.
Space for writing on the topics given in the title. Used for remembering things. Updated every now and then, given enough time.
from collections import defaultdict nested_dict = defaultdict(lambda : defaultdict(list))
from qimage2ndarray import array2qimage
class WidgetWithPictureAndQuit(WidgetWithQuit):
def __init__(self, parent=None, buttontext="Quit", margin=10, image=None):
WidgetWithQuit.__init__(self, parent, buttontext, margin)
self.image = array2qimage(image)
self.label = QtGui.QLabel(self)
if self.image is not None:
self.resize_image()
def resize_image(self):
widget_geometry = self.geometry()
button_geometry = self.quit_button.geometry()
width = widget_geometry.width() - 2*self.margin
height = widget_geometry.height() - (button_geometry.height() + 3*self.margin)
width_offset = self.margin
pixmap = QtGui.QPixmap.fromImage(self.image).scaled(width, height, QtCore.Qt.KeepAspectRatio)
if pixmap.width() < width:
width_offset = (widget_geometry.width() - pixmap.width())/2
self.label.setGeometry(width_offset, self.margin, width, height)
self.label.setPixmap(pixmap)
def resizeEvent(self, event):
WidgetWithQuit.resizeEvent(self, event)
self.resize_image()
from PyQt4 import QtGui, QtCore
import sys
class QuitButton(QtGui.QPushButton):
def __init__(self, parent, text="Quit"):
QtGui.QPushButton.__init__(self, text, parent)
parent.connect(self, QtCore.SIGNAL('clicked()'),
QtGui.qApp, QtCore.SLOT('quit()'))
self.resize(self.sizeHint().width(),
self.sizeHint().height())
class WidgetWithQuit(QtGui.QWidget):
"""QWidget that has a QuitButton at the bottom right;
margin specified in the constructor"""
def __init__(self, parent=None, buttontext="Quit", margin=10):
self.margin = margin
QtGui.QWidget.__init__(self, parent)
self.quit_button = QuitButton(self, text=buttontext)
self.align_button()
def align_button(self):
widget_geometry = self.geometry()
button_geometry = self.quit_button.geometry()
width_offset = widget_geometry.width() - (button_geometry.width() + self.margin)
height_offset = widget_geometry.height() - (button_geometry.height() + self.margin)
self.quit_button.move(width_offset, height_offset)
def resizeEvent(self, event):
QtGui.QWidget.resizeEvent(self, event)
self.align_button()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
widget = WidgetWithQuit()
widget.setGeometry(300, 300, 100, 100)
widget.quit_button.setToolTip('Quit on left mouse click')
widget.show()
sys.exit(app.exec_())
cpufreq-set -g performance -c <CPU>
![]() |
| sample diagram generated using yuml |