Created
November 14, 2012 18:07
-
-
Save SegFaultAX/4073727 to your computer and use it in GitHub Desktop.
Functional URL Manipulation in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from operator import attrgetter, itemgetter | |
from urlparse import urlparse, parse_qs | |
def compose(*fns): | |
def _comp(f, g): | |
def inner(*args, **kwargs): | |
return f(g(*args, **kwargs)) | |
return inner | |
return reduce(_comp, fns) | |
def embed_url(ytid): | |
return "http://www.example.com/embed/{0}".format(ytid) | |
get_qs = attrgetter("query") | |
get_v = itemgetter("v") | |
first = itemgetter(0) | |
extract_qs = compose(parse_qs, get_qs, urlparse) | |
create_embed = compose(embed_url, first, get_v, extract_qs) | |
print create_embed("http://www.youtube.com/watch?v=J1j8k3cvG88") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment