Skip to content

Instantly share code, notes, and snippets.

@Achifaifa
Achifaifa / mpl-gb.py
Created July 18, 2016 20:28
Gameboy colour palette for matplotlib
cdict={'red': ((0.0, 0.0, 0.15),
(0.25, 0.15, 0.48),
(0.50, 0.48, 0.139),
(0.75, 0.139, 0.155),
(1.00, 0.155, 0.155)),
'green': ((0.0, 0.0, 0.56),
(0.25, 0.56, 0.98),
(0.50, 0.98, 0.172),
(0.75, 0.172, 0.188),
@Achifaifa
Achifaifa / parameters_as.py
Created June 30, 2016 17:23
decorator to automatically convert python function parameters
from functools import wraps
conv={"int":int, "str":str, "bool":bool}
def arguments_as(typechoice):
def decorator(f):
@wraps(f)
def func_wrapper(*args, **kwargs):
return f(*[conv[typechoice](i) for i in args], **kwargs)
@Achifaifa
Achifaifa / search.py
Created August 27, 2015 11:47
Searches in a file/directory for a string. use with ./search.py <term> <file>. File types to be searched can be specified in the extensions variables. Using this with ./ as a path does weird stuff.
#! /usr/bin/env python
"""
Searches for a string inside text files
use: search.py <term> <file>
"""
import os, sys
def process(name,term):
@Achifaifa
Achifaifa / rle.py
Created May 9, 2015 20:25
python functions to encode and decode strings and files with rle
def encode(string):
counter=1
previous=out=""
for ind,i in enumerate(string):
if i==previous: counter+=1; previous=i
if ind+1==len(string) or i!=previous:
if counter>2:
if ind+1==len(string): out+="%i%s"%(counter,i)
else: out+="%i%s"%(counter,previous)
else:
@Achifaifa
Achifaifa / newyear.py
Created January 1, 2015 12:32
We missed the stupid new year thing, so I had to do this.
#! /usr/bin/env python
import os,time
if __name__=="__main__":
print "Get ready!"
for i in range(3,0,-1):
print "%i..."%i,
time.sleep(1)
# Quarter stuff
@Achifaifa
Achifaifa / search.py
Last active August 29, 2015 14:08
Reads author and title from input and plays a random matching song via VLC
#! /usr/bin/env python
import json,subprocess,urllib2
jsonshit=json.loads(urllib2.urlopen("http://developer.echonest.com/api/v4/song/search?bucket=id:spotify&bucket=tracks&results=100&api_key=MZFEPELDSKFX4WAMA&artist=%s&title=%s"%(raw_input("group? ",),raw_input("title? ",))).read())
print jsonshit["response"]["songs"][0]["artist_name"],jsonshit["response"]["songs"][0]["title"]
processeddata=json.loads(urllib2.urlopen("https://api.spotify.com/v1/tracks/"+jsonshit["response"]["songs"][0]["tracks"][0]["foreign_id"].split(':')[2]).read())
subprocess.call(["vlc", processeddata["preview_url"]])
@Achifaifa
Achifaifa / search.py
Created November 1, 2014 11:47
echonest search script
#! /usr/bin/env python
import json,urllib2
jsonshit=json.loads(urllib2.urlopen("http://developer.echonest.com/api/v4/song/search?results=100&api_key=MZFEPELDSKFX4WAMA&artist=%s"%raw_input("group? ",)).read())
for x in jsonshit["response"]["songs"]: print "%s - %s"%(x["artist_name"],x["title"])
@Achifaifa
Achifaifa / number.py
Created October 17, 2014 00:31
Python program to solve a stupid math riddle. The set thing worked out nicely, and it was a good excuse to use list generators ;)
number=1234567890
while 1:
if number%12==number%11==number%10==number%9==number%8==number%7==0 and set(list(str(number)))==set([str(a) for a in range(10)]):break
else: number+=10
print number
@Achifaifa
Achifaifa / pricecalc.py
Created August 21, 2014 11:21
Program to calculate prices from base price, VAT and transport costs
#!/usr/bin/env python
import sys
try:
price=int(sys.argv[1])
tax={"A":21,"B":10,"C":4}[sys.argv[2]]
transp=(price*int(sys.argv[3]))/100
totvar=price+((price+transp)*tax/100)
print "Base price: %i + %i%% VAT + %i%% transport = %i" %(price,tax,transp,totvar)
except:
@Achifaifa
Achifaifa / russian.py
Last active August 29, 2015 14:04
Russian roulette for console. We really needed this.
#!/usr/bin/env bash
import os,random
if __name__=="__main__":
os.system('clear')
bullets=["O","O","O","O","O","O"]
bullets[random.randrange(6)]="X"
raw_input("? ? ? ? ? ?\nGo ahead!")
for i in range(6):
os.system('clear')