Skip to content

Instantly share code, notes, and snippets.

@Achifaifa
Achifaifa / comas.py
Created July 9, 2014 16:31
Simple file to automatically add commas to MySQL .csv dumps. Use python comas.py [filename]. CSV file must be in the same directory. Output in ./comas_out
#! /usr/bin/env python
import sys
if __name__=="__main__":
try:
path="./"+sys.argv[1]
except:
print "Use: python comas.py [filename]"
print "Wrong syntax or file not found"
@Achifaifa
Achifaifa / muncher.py
Created July 10, 2014 17:06
Small script for product photography sessions with hundreds of files. Take photo, note the number (DSC_XXXX.nef), and add the product name and the weight (Requirements for this particular assignment). run [python this_file.py reference_file], and all the photos will be renamed to product_(weight kg).nef. Fiddling a lite with the code allows to w…
#! /usr/bin/env python
import os, sys
if __name__=="__main__":
try: os.chdir(os.path.dirname(__file__))
except OSError: pass
filepath="./"+sys.argv[1]
with open (filepath,"r") as listfile:
for line in listfile:
for filename in os.listdir("./"):
@Achifaifa
Achifaifa / card.py
Created July 18, 2014 16:45
This code reads and displays data from Spanish social security magnetic cards.
#!/usr/bin/env python
import os, sys, termios, tty
def getch():
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
try:
tty.setraw(fd)
return sys.stdin.read(33)
finally:
@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')
@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 / 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 / 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 / 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 / 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 / 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: