Skip to content

Instantly share code, notes, and snippets.

View stlehmann's full-sized avatar
:bowtie:

Stefan Lehmann stlehmann

:bowtie:
View GitHub Profile
@stlehmann
stlehmann / app_path.py
Created March 28, 2014 06:55
Determine the application path of a Python EXE
def app_path():
"""
Return the path of the application.
"""
if getattr(sys, 'frozen', False):
return os.path.dirname(sys.executable)
return os.path.dirname(__file__)
@stlehmann
stlehmann / singleton_decorator.py
Created March 28, 2014 10:02
Decorator for singleton classes
def singleton(cls):
"""
Provide a decorator for the Singleton pattern
"""
instances = {}
def getinstance():
if cls not in instances:
instances[cls] = cls()
@stlehmann
stlehmann / temp2lcd.py
Created March 29, 2014 12:16
Raspberry Pi - Python code for reading temperature data from a connected sensor on the w1 bus. The temperature data is printed out on a connected Adafruit 16x2 LCD plate.
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
from time import sleep
def read_temp():
"""
Read temperature from w1 bus.
"""
with open("/sys/bus/w1/devices/28-000004ac7761/w1_slave") as f:
text = f.read()
secondline = text.split("\n")[1]
@stlehmann
stlehmann / temp_with_warning.py
Last active May 13, 2017 21:45
Raspberry Pi - Read temperature, print it to a lcd plate and indicate warnings with backlight color.
#!/usr/bin/python
"""
Read the temperature from the connected sensor on the w1 bus and write it
to the LCD plate. If the temperature is below TEMP_LIMIT_L the backlight
color changes to blue. If the temperature is above TEMP_LIMIT_H the
backlight color changes to red.
This script uses the Adafruit LCD Plate as output device. Documentation and
about infos for Python implementation can be found here:
@stlehmann
stlehmann / combinations.py
Created March 31, 2014 20:56
Find all combinations of two lists
>>> import itertools
>>> list_1 = [1, 2, 3]
>>> list_2 = [4, 5, 6]
>>> list(itertools.product(list_1, list_2))
[(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]
@stlehmann
stlehmann / scale_image.py
Last active August 29, 2015 13:57
Display an Image in a QTableView cell via a Model and scale the image depending on the cell size.
IMAGE, NAME, SHOP = range(COLUMN_COUNT)
class MyItem(object):
def __init__(self, name, image_filename, price)
self.name = name
self.price = price
self.image = QPixmap()
self.image.load(image_filename)
@stlehmann
stlehmann / epytext2rst.py
Last active August 29, 2015 13:58
A Script that converts Epytext Markup Language to ReStructuredText.
import re
import os
from optparse import OptionParser
from glob import glob
#Set up option parser
parser = OptionParser()
(options, args) = parser.parse_args()
#define regex
@stlehmann
stlehmann / .vimrc
Last active November 21, 2018 13:30
My default .vimrc file
" Vim Plug Package Manager and minimal packages
call plug#begin('~/.vim/plugged')
Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' }
Plug 'sickill/vim-monokai'
call plug#end()
" Colorscheme
colorscheme monokai
" Common tweaks
@stlehmann
stlehmann / exec_script.py
Created May 6, 2014 15:21
Execute a Python script from Python console
#Python2
execfile("./filename")
#Python3
exec(open("./filename").read())
@stlehmann
stlehmann / Makefile
Created July 11, 2014 06:55
AVR GCC Makefile template
# Hey Emacs, this is a -*- makefile -*-
#----------------------------------------------------------------------------
# WinAVR Makefile Template written by Eric B. Weddington, Jörg Wunsch, et al.
#
# Released to the Public Domain
#
# Additional material for this makefile was written by:
# Peter Fleury
# Tim Henigan
# Colin O'Flynn