Skip to content

Instantly share code, notes, and snippets.

@np1
np1 / 0_reuse_code.js
Created May 1, 2014 16:54
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@np1
np1 / num_repr.py
Last active August 29, 2015 14:00
String representation of large numbers in Python
import math
def num_repr(num):
""" Return up to four digit string representation of a number, eg 2.6m """
if num <= 9999:
return str(num)
digits = int(math.floor(math.log10(num)) + 1)
#!/bin/sh
for file in *.flac; do flac -cd "$file" | lame -h -b 320 - "${file%.flac}.mp3"; done
@np1
np1 / gist:7281766
Last active December 27, 2015 06:29
Get current playing station and song (if available) from radiotray requires python-dbus
#!/usr/bin/python
import dbus
import pickle as pickle
jar = "/dev/shm/radiotray-now-playing"
dname = "net.sourceforge.radiotray"
dobj = "/net/sourceforge/radiotray"
def getsong():
from __future__ import print_function
"""
Utilities for 256 color support in terminals.
Adapted from:
http://stackoverflow.com/questions/1403353/256-color-terminal-library-for-ruby
The color palette is indexed as follows:
0-15: System colors
@np1
np1 / playsong.py
Created September 16, 2013 00:37
Play (almost) any song with 2 lines of python code (requires mplayer)
import urllib2 as u, sys as s, json as j, subprocess as p
p.call(["mplayer", u.urlopen(j.loads(u.urlopen("http://ex.fm/api/v3/song/search/%s" % "+".join(s.argv[1:])).read())['songs'][0]['url']).geturl().split("#")[0]])
# usage:
# python playsong.py {artist and/or song name}
#
# eg:
#
# python playsong.py rolling stones paint it black
@np1
np1 / gist:5664953
Created May 28, 2013 18:27
ffmpeg and mencoder useful commands
# Convert video from one format to another
# (uses file extensions to determine format)
ffmpeg -i inputfile.mp4 outputfile.flv
# Resize a video
# the -1 lets ffmpeg choose the width, maintaining aspect ratio
ffmpeg -i outfile.mp4 -vf scale=720:-1 resized.mp4
@np1
np1 / gist:5653451
Last active December 17, 2015 18:29
Gtk get tree selection rownumber and contents #Python #Gtk #Treeview
def on_tree_selection_changed(self, selection):
model, treeiter = selection.get_selected()
if treeiter is not None:
rowcontents = model[treeiter][0:3] # for a 4 column treeview
rownumobj = model.get_path(treeiter)
rownum = int(rownumobj.to_string())
@np1
np1 / parseYTfeed.py
Last active December 17, 2015 18:09
Parse YouTube channel RSS feed data without additional libraries
#!/usr/bin/python
import urllib2
import time
from xml.etree import ElementTree as etree
class YTFeedParser(list):
def __init__(self, url):
rawfeeddata = urllib2.urlopen(url).read()
tree = etree.fromstring(rawfeeddata)