correl.phoenixinquis.net

projects and coding adventures


Project maintained by correl Hosted on GitHub Pages — Theme by mattgraham

Learning Functional Programming, Part One


09 April 2012 functional · python

Part One: Lambdas? In my Python?

Over the past few months, I’ve decided to take a stab at learning some functional programming. I’d been doing python for a few years (and completely falling in love with it), and so I’d been exposed to a few functional concepts it offers - primarily higher-order functions and list comprehensions, both of which allow for very clear, concise and powerful code. Since that’s where I started my journey, that’s where my post will begin as well.

Functions are objects, too

Having graduated to python from PHP and C/C++, perhaps the biggest new thing to wrap my head around (besides readable code, whitespace-as-syntax, programming being fun again, and all that), is that in python, functions (and classes!) are objects, just like anything else. They can still be defined in the usual way, but they can also be assigned, passed as arguments, even modified and replaced like any other value or object in your program.

def do_a():
    print "Doing something"

do_b = do_a

do_b()

# Prints "Doing something"

Functions themselves no longer require formal definitions, either, they can be created anonymously:

my_send = lambda person, thing: send(person.upper(), thing, subject="Check this out!")
ucase_people = map(lambda name: name.upper(), ["Joe", "Mary", "Zach"])

Abstracting behaviour

You’ll find you can now start abstracting away common idioms. For example, you probably very often find yourself looping over some list of items, performing some set of actions on them, or passing them to some other function or method:

people = ["Joe", "Chris", "Matt", "Jennifer"]
for person in people:
    u_person = person.upper()
    send(person, super_fun_thing)

Instead of that, you could have a function that takes a list as one argument, and a function to apply to each item in it as another:

def dostuff(action, things):
    result = []
    for thing in things:
        result.append(action(thing))
    return result

dostuff(send, people)

The above example is actually just a simple definition of one of the most common higher-order functions, map, which python already provides for you. Another particularly useful higher-order function is filter which, given a function that returns true of false if its criteria are met by the passed item, will return the subset of the passed list that satisfy the filtering function:

stuff = ["My notes.txt", "Matt's notes.txt",  "My music.pls"]
my_stuff = filter(lambda s: s.startswith("My "), stuff)

# my_stuff = ["My notes.txt", "My music.pls"]

List comprehensions provide a cleaner, easier to read way to perform mapping and/or filtering on a list:

stuff = ["My notes.txt", "Matt's notes.txt",  "My music.pls"]

my_stuff = [file for file in stuff if file.startswith("My ")]
# ["My notes.txt", "My music.pls"]

upper_stuff = [file.upper() for file in stuff]
# ["MY NOTES.TXT", "MATT'S NOTES.TXT", "MY MUSIC.PLS"]

music = [file.upper() for file in stuff if file.endswith(".pls")]
# ["MY MUSIC.PLS"]

Tip of the iceberg

This is just a very small taste of functional programming concepts. Later, I’ll introduce a couple of functional languages, and explain what sets them apart from object-oriented and imperative programming languages.

Transmission, RSS and XBMC


27 April 2011 bittorrent · htpc · linux · python · transmission · xbmc

I’m a huge fan of XBMC. My pc (currently running Ubuntu 10.04) has taken root in my living room, piping all my movies and tv shows straight to my HDTV.

While my pc is set up as a DVR using MythTV to record shows off my FIOS box, it tends to be a little unreliable, which can suck when it’s time to catch up on Daily Show and Colbert episodes. I’ve had Transmission set up for a while for all my torrenting needs, and I’ve even written an XBMC script to manage torrents, so I got to looking for tools to track tv show torrent rss feeds.

My first stop was TED. TED worked well enough, but would occasionally hang. Since it’s a GUI java app running in the taskbar, it would require me to dig out my mouse and break out of full screen XBMC to fiddle with it. I eventually got tired of dealing with TED and went back to prodding Myth.

Recently I’ve been itching to reliably watch my shows again, so I checked around for a simple command-line utility to track rss feeds and download torrents. Finding none, I loaded up vim and threw together a python script to handle it all for me.

I also have another, simple script from when I was using TED (or just manually downloading shows) which looks at completed torrents, compares their names with the folders in my TV directory, and moves the shows into them for XBMC to see.

A couple cron jobs and a few rss feeds later, and I’ve got all my shows automatically delivered straight to XBMC for my lazy evening viewing pleasure.

trss.py

Download

Usage:
    trss.py add <rss-url> [<recent-items>]
        Adds an RSS feed to follow
        rss-url:        Full URL to the RSS feed
        recent-items:   (Optional) number of recent items to queue
                        for downloading
    trss.py remove <index>
        Remove an RSS feed
        index:          Numeric index of the feed to remove as
                        reported by the list command
    trss.py list
        Displays a list of followed feeds

    trss.py download
        Fetch all feeds and download new items

    trss.py set [<setting> [<value>]]
        Set or view configuration settings
        Call without any arguments to list all settings and their values
        Call with a setting and no value to see the current value for that setting

        Currently, the only used setting is 'download_dir', which allows you to set
        a directory to store all retrieved torrents, such as a directory your
        torrent application watches for new downloads. If 'download_dir' is not set,
        the current directory will be used.

transmission-tv.py

#!/usr/bin/python
import os
import re

import transmissionrpc

TV_PATH = '/media/Gaia/Video/TV/'

class TVShowCollection:
	def __init__(self, path):
		self.path = path
		self.shows = os.listdir(path)
		self.patterns = [[s.lower().replace(' ', '.?'), s] for s in sorted(self.shows, key=len, reverse=True)]
	def match(self, filename):
		for pattern, show in self.patterns:
			if re.findall(pattern, filename.lower()):
				return show
		return None

def move(self, ids, location):
	"""Move torrent data to the new location."""
	self._rpc_version_warning(6)
	args = {'location': location, 'move': True}
	self._request('torrent-set-location', args, ids, True)

if float(transmissionrpc.__version__) < 0.4:
	# The move function is not present in versions 0.3 and older
	transmissionrpc.Client.move = move

collection = TVShowCollection(TV_PATH)
client = transmissionrpc.Client()

torrents = client.info()
for i, torrent in torrents.iteritems():
	status = torrent.status
	if status not in ['seeding', 'stopped']:
		continue
	show = collection.match(torrent.name)
	if show is None:
		continue
	path = '{0}{1}/'.format(TV_PATH, show)
	if torrent.downloadDir.startswith(path):
		continue
	print 'Found {0} torrent \'{1}\' in show \'{2}\', moving...'.format(status, torrent.name, show)
	result = client.move(i, path)
	if status == 'seeding':
		print 'Re-starting torrent to continue seeding'
		client.start(i)

Meh.php


27 April 2011 php
<?php
if (!defined('meh')) define('meh', null);

class Meh {
    public function __set($name, $value) {
    }
    public function __get($name) {
        return meh;
    }
    public function __isset($name) {
        return true || false;
    }
    public function __unset($name) {
    }
    public function __call($name, $arguments) {
        return meh;
    }
    public function __callStatic($name, $arguments) {
        return meh;
    }
}

$bwuh = new Meh();
$bwuh->give_a_shit();
echo $bwuh->concerns;

class SuperDuperBillingProcessor extends Meh {}

$p = new SuperDuperBillingProcessor();
$p->calculateEverything();
$p->profit();

Potatoes and Portal Guns


27 April 2011 portal · xbox

Portal 2 Logo Got my hands on Portal 2 and finished a run through the single player campaign. Was a lot of fun, the characters were bursting with humor and personality. Just like the first game, it was hard to stop playing. Unlike the first game, it’s got some length, so I stayed up late a couple nights with my eyes glued to the television. I already want to play through it again to find any little things I my tired eyes may have missed.

I’m itching to give co-op a try, so if you happen to have it on xbox or care to drop by, let me know.

Update: Played some co-op with Jen, had fun navigating puzzles together :)