Skip to content

Instantly share code, notes, and snippets.

View durden's full-sized avatar

Luke Lee durden

View GitHub Profile
@durden
durden / signal_disconnect.py
Last active May 24, 2022 07:22
Context manager to temporarily disconnect a pyqt signal
from contextlib import contextmanager
@contextmanager
def slot_disconnected(signal, slot):
"""
Create context to perform operations with given slot disconnected from
given signal and automatically connected afterwards.
usage:
@durden
durden / references.md
Last active September 11, 2017 18:17
References for Python Science apps talk from Pycon 2013, PyTexas 2013, and PyArkansas 2012
@durden
durden / tricks.py
Created March 8, 2013 22:27
Funny python 'tricks'
# Turn list of rows into columns
>>> x = [(1,2,3), (4,5,6)]
>>> zip(*x)
[(1, 4), (2, 5), (3, 6)]
# Reverse key-value mapping in a dict
>>> x = {'a': 1, 'b': 2}
>>> dict([reversed(kv) for kv in x.iteritems()])
" Searches Dash for the word under your cursor in vim, using the keyword
" operator, based on file type. E.g. for JavaScript files, I have it
" configured to search j:term, which immediately brings up the JS doc
" for that keyword. Might need some customisation for your own keywords!
function! SearchDash()
" Some setup
let s:browser = "/usr/bin/open"
let s:wordUnderCursor = expand("<cword>")
@durden
durden / profile_decorator.py
Created March 20, 2013 19:17
Profile decorator take from rwarren talk, 'A brief intro to profiling in Python'
def profile_this(fn):
def profiled_fn(*args, **kwargs):
fpath = fn.__name__ + '.profile'
prof = cProfile.Profile()
ret = prof.runcall(fn, *args, **kwargs)
prof.dump_stats(fpath)
return ret
return profiled_fn
@profile_this
@durden
durden / pyqtsignal_decorator_connection.py
Created April 4, 2013 13:31
Example of using pyqtSignal.connect as a decorator for slot connection
# Example modified from:
# http://pysnippet.blogspot.com/2010/06/qsignalmapper-at-your-service.html
#
# Example showing how QSignalMapper can be used to manage an arbitrary
# numbers of parameterless signals and re-emit them with an argument
# identifying the sender.
#
# Each signal is associated in the QSignalMapper with either an int,
# QString, QObject or a QWidget which is passed as argument to the slot
@durden
durden / signal_connection_decorator_fail.py
Created April 4, 2013 14:00
Example of where using pyqtSignal.connect as a decorator will NOT work
from PyQt4.QtCore import pyqtSignal, QObject
from PyQt4.QtGui import QApplication, QFrame, QGridLayout, QPushButton
class View(QFrame):
openStuff = pyqtSignal()
def __init__(self, parent=None):
super(View, self).__init__(parent)
@durden
durden / debug_signals.py
Created April 11, 2013 21:16
Attach to all available signals in decoratored class and print debug message each time one of the signals is emitted.
def debug_signals(cls):
"""
Attach to all available signals in decoratored class and print debug
message each time one of the signals is emitted.
"""
from PyQt4 import QtCore
def stripArgsFromSignature(signature):
"""
@durden
durden / parse.py
Created May 3, 2013 19:41
Reading weird config file
def _parse_config_file(config_file):
"""
Parse given config file and return dict of settings
[load]
files=a,b,c
[process]
for x in xrange(10):
foo()
@durden
durden / cached_property.py
Created May 8, 2013 19:24
Decorator for read-only properties evaluated only once within TTL period
#
# © 2011 Christopher Arndt, MIT License
#
# Taken from: http://wiki.python.org/moin/PythonDecoratorLibrary
import time
class cached_property(object):
'''Decorator for read-only properties evaluated only once within TTL period.