H. Chase Stevens Logo

Most Common Tags

  1. programming
  2. python
  3. code
  4. philosophy
  5. evolution
  6. game design
  7. probability
  8. video games
  9. genetic algorithms
  10. government

Archives

Loading

Posts with tag "tkinter":

More Stupid Higher-Order Function Tricks

posted on: Thursday, March 15, 2012 (11:23 am) by Chase Stevens

A while ago, a friend of mine who was unfamiliar with python asked me how to go about parsing a CSV into a list of lists. Moreover, the data included integers which needed to be parsed as such (and not as strings). My solution was as follows:

(lambda f: [map((lambda x: x.isdigit() and int(x) or x),(line.split(','))) for line in f.readlines()]) #takes a file
Just yesterday, another friend of mine had accidentally merged and subsequently sorted two excel spreadsheets, and overwrote one with the result. She asked if I could take the merged file and the remaining original file and re-construct the file she had overwritten. Incorporating the CSV parsing function I had written earlier, I wrote this:
from tkFileDialog import asksaveasfile, askopenfile
full = (lambda f: [map((lambda x: x.isdigit() and int(x) or x),(line.split(','))) for line in f.readlines()])(askopenfile())
exclude = (lambda f: [map((lambda x: x.isdigit() and int(x) or x),(line.split(','))) for line in f.readlines()])(askopenfile())
(lambda f: map(f.write,(map(','.join,filter((lambda x: x not in exclude),full)))))(asksaveasfile())

Tags: code, csv, filter, lambda, map, programming, python, tkinter

Editing Metadata as a Spreadsheet

posted on: Thursday, December 15, 2011 (10:13 pm) by Chase Stevens

A friend of mine was talking to me recently about how she wished iTunes let her edit song metadata more easily, as if she were editing a spreadsheet in Excel or OpenOffice. Drawing on this as inspiration, I've written a little python script that gets all music files (as specified in the list file_types) from a folder (and all subfolders), exports their metadata to a Comma Separated Value file, and then takes all changes made to that file and applies them to the tracks. As with my xml playlist downloader, you'll need the ID3 library for python installed.

Download


import fnmatch
import os
import Tkinter
import tkFileDialog
import ID3
from tkMessageBox import showinfo

root = Tkinter.Tk()
root.withdraw()
folder = tkFileDialog.askdirectory(title="Please select a music directory.")

matches = []
file_types = ['mp3','aiff']
for root, dirnames, filenames in os.walk(folder):
    for filename in reduce((lambda x,y: x + y), [fnmatch.filter(filenames, ('*.' + filetype)) for filetype in file_types]):
        matches.append(os.path.join(root, filename))

output = 'Title,Artist,Album,Track,Year,File Location (Do Not Alter)\n'
for song in matches:
    line = ''
    id3info = ID3.ID3(song)
    columns = [id3info.title,id3info.artist,id3info.album,id3info.track,id3info.year,song]
    for column in columns:
        line += '"' + str(column) + '",'
    line = line[:-1] + '\n'
    output += line

filename = tkFileDialog.asksaveasfilename(defaultextension='.csv',filetypes=[('All Files','.*'),('Comma Separated Values File','.csv')])
f = open(filename,'w')
f.write(output)
f.close()

showinfo("File Created","Your CSV file has been created and is ready for you to edit. After you have done so and saved, please click 'OK'.")

f = open(filename,'r')
f.readline() #Removing column titles
for line in f.readlines():
    line.replace('"','')
    columns = line.split(',')#0:title,1:artist,2:album,3:track,4:year,5:location
    id3info = ID3.ID3(columns[5])
    id3info['TITLE'] = columns[0]
    id3info['ARTIST'] = columns[1]
    id3info['ALBUM'] = columns[2]
    id3info['TRACKNUMBER'] = columns[3]
    id3info['YEAR'] = columns[4]
f.close()

showinfo("Success","Song metadata has been successfully updated.")

Tags: code, csv, metadata, mp3, music, programming, python, tagging, tkinter

Exporting Color Scheme Designer Palettes to Paint.Net

posted on: Saturday, November 19, 2011 (10:59 am) by Chase Stevens

If you're like me and use the excellent tool over at Color Scheme Designer in conjunction with Paint.Net, you may have grown frustrated with creating palettes one color at a time by copy-pasting hex values. No longer! Below, please find a short python application I wrote (which even uses a fancy and unnecessary GUI) to create Paint.Net palettes from Color Scheme Designer palettes. Just export your Color Scheme Designer palette to text, copy what the site gives you, paste it into the application, and click "convert and save". The resulting file is ready to import into Paint.Net at your leisure.

from Tkinter import *
from tkFileDialog import asksaveasfilename as save
from tkMessageBox import showinfo as disp

def convert():
    intext = str(text.dump(1.0, END))
    intext = intext.split(' = ')
    colorlist = list()
    for ss in intext:
        if ss[0] == '#':
            colorlist.append(ss[1:])
    filename = save(defaultextension='txt',initialdir='\%HOMEPATH\%\\My Documents\\Paint.NET User Files\\Palettes')
    palette = open(filename, 'w')
    for color in colorlist:
        palette.write('FF%s\n' %color)
    palette.close()
    disp('File Saved', 'File successfully saved as %s.' %filename)
    
root = Tk()
menubar = Menu(root)
menubar.add_command(label="Convert and Save", command=convert)
menubar.add_command(label="Quit", command=quit)
root.config(menu=menubar)
scroll = Scrollbar(root)
scroll.pack(side=RIGHT, fill=Y)
text = Text(root, wrap=WORD, yscrollcommand=scroll.set)
text.pack()
scroll.config(command=text.yview)
root.mainloop()

Tags: code, color, design, internet, programming, python, tkinter, web, web design