Skip to content

Instantly share code, notes, and snippets.

Avatar

Martin Thurau martinth

View GitHub Profile
@martinth
martinth / argparse_fileinput_demo.py
Created Aug 6, 2015
Read from stdin or files in Python (combining argparse and fileinput)
View argparse_fileinput_demo.py
import argpase
import fileinput
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('--dummy', help='dummy argument')
parser.add_argument('files', metavar='FILE', nargs='*', help='files to read, if empty, stdin is used')
args = parser.parse_args()
# If you would call fileinput.input() without files it would try to process all arguments.
View Simple Java Keypresser
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.AWTException;
public class KeyPresser {
public static void main(String[] args) {
int delay = 0;
try {
@martinth
martinth / simple_server.py
Created Jan 25, 2012
simple HTTP server that dumps header
View simple_server.py
import SimpleHTTPServer
import SocketServer
class H(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
print self.headers
httpd = SocketServer.TCPServer(("", 8090), H)
httpd.server_forever()
@martinth
martinth / sign-and-align.sh
Created Apr 10, 2014
A simple script to automate signing and aligning of APK files for upload to the Playstore
View sign-and-align.sh
#!/bin/sh
FOLDER=android/bin
IN_APK_NAME=App-release-unsigned.apk
OUT_APK_NAME=App-release-signed-aligned.apk
KEYSTORE=release-key.keystore
KEYSTORE_ALIAS=app_release
echo ">>> Signing $OUT_APK_NAME..."
@martinth
martinth / decorators.py
Last active Jan 21, 2018
A sample decorator the can be used on plain old functions and also on instance methods
View decorators.py
class func_and_method_decorator(object):
'''A clased based decorator that takes parameters and works on plain functions
and instance methods'''
def __init__(self, *args, **kwargs):
'''The init function is called when a module where the decorator is used
is imported. It gets passed all parameters that are given to decorator
definition. I.e.
@func_and_method_decorator(foo='bar')
@martinth
martinth / googleio13.py
Created Mar 20, 2013
Generates valid click codes for Google I/O 2013
View googleio13.py
forbidden_starts = filter(bool,
'''
000
00100
0010100
00101011
001011
00110
00111000
0011101
@martinth
martinth / assertion_hook.py
Created Aug 8, 2011
A sample exception hook, that prints useful information if an AssertionError occures
View assertion_hook.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os.path
import pprint
sys._old_excepthook = sys.excepthook
def assert_hook(exc_type, exception, traceback):
@martinth
martinth / mde_login.py
Created Aug 3, 2011
mode.de login script
View mde_login.py
# -*- coding: utf-8 -*-
import cookielib
from urllib2 import HTTPCookieProcessor, build_opener
from urllib import urlencode
from BeautifulSoup import BeautifulSoup
# the login data
login_data = {
'login_username': u'USERNAME',
'login_password': u'PASSWORD',
View dump_table.py
from urllib2 import urlopen
from BeautifulSoup import BeautifulSoup
data = urlopen('''http://www.hydro.eaufrance.fr/stations/O7502510&procedure=tousmois''').read()
soup = BeautifulSoup(data)
table = soup.find('table', {'summary': "valeurs mensuelles et annuelles"})
for tr in table.findAll('tr'):
line = []
for td in tr.findAll('td'):