Skip to content

Instantly share code, notes, and snippets.

@simon-weber
simon-weber / dual_decorator.py
Created April 3, 2014 15:31
Use a paramaterized Python decorator without calling it.
def dual_decorator(func):
"""This is a decorator that converts a paramaterized decorator for no-param use."""
# modified from http://stackoverflow.com/a/10288927/1231454.
@functools.wraps(func)
def inner(*args, **kw):
if ((len(args) == 1 and not kw and callable(args[0])
# Exceptions are callable; the next line allows us to accept them as args.
and not (type(args[0]) == type and issubclass(args[0], BaseException)))):
return func()(args[0])
@simon-weber
simon-weber / travis-secure-redefine.py
Last active October 15, 2017 18:44
Python script to allow use of TravisCI secure arg variables that would normally be defined on each line of a build matrix, but are too large for the key size when combined.See https://github.com/travis-ci/travis-ci/issues/1736 for motivation. If you just need to secure a lot of sample data, do something like https://github.com/travis-ci/travis/i…
#!/usr/bin/env python
from __future__ import print_function
"""
./%s 'cmd' <id-argname> <redef-argname> [<redef-argname>...]
Given the current environment:
IDARG=1
FOO1=foovalue
BAR1=barvalue
FOO2=foo-unused
@simon-weber
simon-weber / logutil.py
Created December 8, 2013 03:48
A context manager to temporarily disable all logging in Python that supports previous calls to logging.disable.
from contextlib import contextmanager
import logging
@contextmanager
def all_logging_disabled(highest_level=logging.CRITICAL):
"""
A context manager that will prevent any logging messages
triggered during the body from being processed.
:param highest_level: the maximum logging level in use.
@simon-weber
simon-weber / rchandler.py
Last active July 31, 2018 13:58
An example of using a StackContext to store request data globally in Tornado. See https://groups.google.com/d/msg/python-tornado/8izNLhYjyHw/TNKGa9fgvpUJ for motivation and further discussion.
import tornado
class RequestContextHandler(tornado.web.RequestHandler):
def _execute(self, transforms, *args, **kwargs):
# following the example of:
# https://github.com/bdarnell/tornado_tracing/blob/master/tornado_tracing/recording.py
global_data = {} # add whatever here, e.g. self.request
#!/usr/bin/env bash
# executables prefix
_prefix="/usr/bin"
# git executable
_git="$_prefix/git"
# branch from which to generate site
_origbranch="source"
# branch holding the generated site
@simon-weber
simon-weber / checkout-dates.sh
Created April 27, 2013 22:19
Checks out my entire sample source tree (for https://github.com/simon-weber/Predicting-Code-Popularity) to a certain date, in parallel. The argument to -P is the number of processes to use.
find /localdisk/winter-snapshot/code/ -mindepth 2 -maxdepth 2 | xargs -P 32 -n 1 -I {} bash -c 'cd $1; git checkout -q $(git rev-list -n 1 --before="2012-11-30" $(git rev-parse --abbrev-ref HEAD)) -- || pwd' -s {} 2>&1 | tee outputfile
@simon-weber
simon-weber / proboscis_after_class_bug.py
Created February 28, 2013 04:21
Reproduce a bug in proboscis.after_class.
from proboscis import test, after_class, TestProgram
@test
class Test(object):
@after_class
def will_skip_but_should_not(self):
pass
@test
@simon-weber
simon-weber / gmusic_autoplaylists.py
Last active November 27, 2019 21:57
approximations of Google Music auto playlists
from operator import itemgetter
from gmusicapi import Api
api = Api()
api.login('me@gmail.com', 'my-password')
# => True
lib = api.get_all_songs()
@simon-weber
simon-weber / muspy2gcal.py
Created February 16, 2013 19:37
A dirty little script that will make Google Calendar events for muspy releases. Fill in 'XXXXX' with the relevant values.
import os.path
import requests
#Google client api imports
import gflags
import httplib2
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
@simon-weber
simon-weber / gist:4960475
Created February 15, 2013 13:47
old gmusicapi code to do a generic metadata copy between audio files
import logging
import mutagen
log = logging.getLogger(__name__)
def copy_md_tags(from_fname, to_fname):
"""Copy all metadata from *from_fname* to *to_fname* and write.