Skip to content

Instantly share code, notes, and snippets.

@simon-weber
simon-weber / gist:1361260
Created November 12, 2011 23:02
Rails: for 'a belongs_to b', a.b.id != a.b_id
irb(main):010:0> t = Tag.new(:rfid => 999, :item_id => 1)
=> #<Tag id: nil, rfid: 999, created_at: nil, updated_at: nil, item_id: 1>
irb(main):011:0> t.item
=> #<Item id: 1, description: "burger", price: #<BigDecimal:b6f6f46c,'0.5E1',4(8)>, created_at: "2011-11-12 03:16:19", updated_at: "2011-11-12 03:16:19">
irb(main):012:0> t.item_id = 2
=> 2
irb(main):013:0> t.save
@simon-weber
simon-weber / makefile
Last active October 11, 2015 17:37
makefile for a sparse parallel kernel source tree
KERNEL_MAKE_ARGS =
KERNEL_DIR = ../linux-2.6.26.5
# this isn't actually a list
DO_NOT_COPY = makefile
all: rm_moved_files src_copy
.FORCE:
@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.
@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 / 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 / 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 / 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
#!/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 / 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
@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.