Skip to content

Instantly share code, notes, and snippets.

View mitchellrj's full-sized avatar

Richard Mitchell mitchellrj

View GitHub Profile
@mitchellrj
mitchellrj / fixed_point.py
Created March 27, 2016 18:11
Create and read fixed-point byte representations of numbers in Python
import decimal
class FixedPoint(decimal.Decimal):
def to_fixed_point(self, whole_bits, fraction_bits, signed=False):
# returns bytes aligned to the last fraction bit
total = round(self * (1 << fraction_bits))
num_bytes = (whole_bits + fraction_bits) / 8
if num_bytes % 1:
@mitchellrj
mitchellrj / cue-split.py
Created November 24, 2015 15:44
Script to split a single audio file into multiple files using a Cue file.
#!env python3
import argparse
import datetime
import enum
import math
import os.path
import shlex
import shutil
import subprocess
@mitchellrj
mitchellrj / README.rst
Last active November 14, 2015 11:44
Correct naming and tags of music files using MusicBrainz

Corrects the filenames and metadata of music files.

Assumptions

  • You want your music files named:

    ALBUM/TRACK-NUMBER - ARTIST - TRACK-TITLE.EXT
    
@mitchellrj
mitchellrj / watch-and-revert.sh
Last active August 29, 2015 14:07
Script to watch a file for modifications and revert it immediately
#!/bin/bash
#
# Usage: watch-and-revert.sh filename
#
BACKUP="$(dirname $1)/.backup-$(basename $1)"
function cleanup {
rm $BACKUP
}
trap cleanup EXIT
cat $1 > $BACKUP
@mitchellrj
mitchellrj / base.py
Created July 25, 2014 08:21
Tastypie - make fields read only for object update but not for creation
from tastypie.resources import ModelResource as BaseModelResource
class ReadOnlyFieldsAfterAddMeta(BaseModelResource.__metaclass__):
def __new__(cls, name, bases, attrs):
new_class = super(ReadOnlyFieldsAfterAddMeta, cls).__new__(cls, name, bases, attrs)
read_only_fields_after_add = getattr(new_class._meta, 'read_only_after_add', ())
for field_name in read_only_fields_after_add:
@mitchellrj
mitchellrj / gist:12acfd703bc75beb0236
Last active August 29, 2015 14:04
Python multiple inheritance and super
>>> class MetaMeta(object):
... def __init__(self):
... print ('metameta')
...
>>> class Meta(MetaMeta):
... def __init__(self):
... print ('meta1')
...
>>> class Meta2(MetaMeta):
... def __init__(self):
@mitchellrj
mitchellrj / keybase.md
Created July 8, 2014 11:14
keybase.md

Keybase proof

I hereby claim:

  • I am mitchellrj on github.
  • I am mitchell (https://keybase.io/mitchell) on keybase.
  • I have a public key whose fingerprint is 7F06 1B87 5590 ED92 5FE4 DCD3 27AA 9799 D267 327E

To claim this, I am signing this object:

@mitchellrj
mitchellrj / README.md
Created May 30, 2014 15:54
Draytek Vigor SNMP monitoring with Cacti

This is based on the outdated, official Cacti configuration for ADSL Line and modified for Draytek Vigor support.

Changes from the original include:

  • Extra data sources
  • Removed unimplemented data sources
  • Updated for newer versions of Cacti

Installation & Usage

  1. Download this gist as a .zip file by clicking the button to the left labelled "Download Gist"
@mitchellrj
mitchellrj / media_query.py
Created January 17, 2014 18:06
Basic CSS3 media query parsing for Python & testing against a dictionary of characteristics
import fractions
import re
def parse_media_query(media_query):
class Grouping(object):
def __init__(self, parent, *args):
self.parent = parent
@mitchellrj
mitchellrj / inliner.py
Last active May 4, 2016 09:38
CSS inliner
"""
Take an HTML document and replace all CSS with inline styles, accounting
for all precedence rules. Requires cssutils, cssselect and lxml.Does not
work with pseudo-elements, @font-face, @page and CSS variables as these
cannot be represented by inline CSS.
Usage::
inline_css(html_string, page_url, medium)