Wednesday, November 26, 2014

A first Python one-liner

By Vasudev Ram

1. Install Python 2.7 (if you don't have it already).

2. Run this at the command line:

python -c "print ''.join(list(reversed('!dlrow olleH')))"

- Vasudev Ram - Dancing Bison Enterprises

Contact Page

Monday, November 24, 2014

youtube-dl, Python video download tool, on front page of Hacker News

By Vasudev Ram

youtube-dl is a video download tool written in Python.

I had blogged about youtube-dl a while ago, here:

youtube-dl, a YouTube downloader in Python [1]

and again some days later, here:

How to download PyCon US 2013 videos for offline viewing using youtube-dl

(The comments on the above post give some better / easier ways to download the videos than I gave in the post.)

Today I saw that a Hacker News thread about youtube-dl was on the front page of Hacker News for at least part of the day (up to the time of writing this). The thread is here:

youtube-dl (on Hacker News)

I scanned the thread and saw many comments saying that the tool is good, what different users are using it for, many advanced tips on how to use it, etc. The original creator of youtube-dl, Ricardo Garcia, as well as a top contributor and the current maintainer (Filippo Valsorda and Philipp Hagemeister, respectively) also participated in the HN thread, as HN users rg3, FiloSottile and phihag_, respectively. I got to know from the thread that youtube-dl has many contributors, and that its source code is updated quite frequently (for changes in video site formats and other issues), both points which I did not know earlier. (I did know that you can use it to update itself, using the -U option).

Overall, the HN thread is a worthwhile read, IMO, for people interested in downloading videos for offline viewing. The thread had over 130 comments at the time of writing this post.

(On a personal note, since I first got to know about youtube-dl and downloaded it, I've been using it a fair amount to download videos every now and then, for offline viewing, and it has worked pretty well. There were only a few times when it gave an error saying the video could not be downloaded, and I am not sure whether it was due to a problem with the tool, or with the video site.)

[1] My first post about youtube-dl also had a brief overview of its code, which may be of interest to some Pythonistas.

This other post which mentions youtube-dl may also be of interest:

The most-watched Python repositories on Github

since youtube-dl was one of those most-watched repositories, at the time of writing that post.

- Vasudev Ram - Dancing Bison Enterprises

Signup for emails about new products from me.

Contact me for Python consulting and training.

Friday, November 21, 2014

Google Contributor, a new monetization option for online publishers

By Vasudev Ram

I saw this via a tweet by GigaOm:

Google launches Contributor, a crowdfunding tool for publishers

Excerpts (shortened and partly re-worded) from the GigaOm article:

... Google Contributor, which is designed to allow web users to pay sites that they visit a monthly fee, and in return see no Google ads when they visit those sites.
the new feature is launching with 10 publishing partners, including Mashable, Imgur, WikiHow and Science Daily
the user who goes to the Contributor web site sees a list of participating web sites, and can decide to give them $1, 2 or 3 per month.
The site gets paid when the user actually visits it. The formula for payment is not specified in the GigaOm article.
Earlier, Google also launched a contribution system for YouTube video creators called “Fan Funding,” which allows viewers to donate anywhere from $1 to $500 to the channel of their choice.

Here is the Google Contributor site.

Based on the article, Google Contributor seems like a potentially good idea for web publishers, such as big sites as well as small sites and blogs. Time will tell, though, how useful it turns out to be. I signed up for an invite, and if I get invited, I'll try it out and may write about my experience with it, here on this blog.

Update: Here is another article about Google Contributor, by NextWeb:

- Vasudev Ram - Dancing Bison Enterprises

Signup to be notified about new products from Vasudev Ram.

Contact Page

Thursday, November 20, 2014

Find if a Python string is an anagram of a palindrome

By Vasudev Ram

I saw this interesting thread on Hacker News some 10-odd days ago:

HN: Rust and Go

Apart from being generally of interest, it had a sub-thread that was about finding if a given string is an anagram of a palindrome. A few people replied in the thread, giving solutions in different languages, such as Scala, JavaScript, Go and Python.

Some of the Python solutions were already optimized to some extent (e.g. using collections.Counter and functools.partial - it was a thread about the merits of programming languages, after all), so I decided to write one or two simple or naive solutions instead, and then see if those could be optimized some, maybe differently from the solutions in the HN thread.

Here is one such simple solution to the problem, of finding out if a string is an anagram of a palindrome. I've named it (for Is Anagram Of Palindrome, version 01). The solution includes a scramble() function, to make an anagram of a palindrome, so that we have input for the test, and a main function to run the rest of the code to exercise things, for both the case when the string is an anagram of a palindrome, and when it is not.

The logic I've used is this (in pseudocode, even though Python is executable pseudocode, ha ha):

For each character c in the string s:
        If c already occurs as a key in dict char_counts,
        increment its count (the value corresponding to the key),
        else set its count to 1.
    After the loop, the char_counts dict will contain the counts 
    of all the characters in the string, keyed by character.
    Then we check how many of those counts are odd.
    If at most one count is odd, the string is an anagram of 
    a palindrome, else not.

And here is the Python code for

Program to find out whether a string is an anagram of a palindrome.
Based on the question posed in this Hacker News thread:

from random import shuffle

def anagram_of_palindrome(s):
    char_counts = {}
    for c in s:
        char_counts[c] = char_counts.get(c, 0) + 1
    odd_counts = 0
    for v in char_counts.values():
        if v % 2 == 1:
            odd_counts += 1
    return odd_counts <= 1

def scramble(s):
    lis = [ c for c in s ]
    return ''.join(lis)

def main():
    # First, test with a list of strings which are anagrams of palindromes.
    aops = ['a', 'bb', 'cdc', 'foof', 'madamimadam', 'ablewasiereisawelba']
    for s in aops:
        s2 = scramble(s)
        print "{} is an anagram of palindrome ({}): {}".format(s2, \
            s, anagram_of_palindrome(s2))
    # Next, test with a list of strings which are not anagrams of palindromes.
    not_aops = ['ab', 'bc', 'cde', 'fool', 'padamimadam']
    for s in not_aops:
        s2 = scramble(s)
        print "{} is an anagram of a palindrome: {}".format(s2, \

And here is the output of running it:
$ python
a is an anagram of palindrome (a): True
bb is an anagram of palindrome (bb): True
ccd is an anagram of palindrome (cdc): True
ffoo is an anagram of palindrome (foof): True
daadmamimma is an anagram of palindrome (madamimadam): True
srewaeaawbeilebials is an anagram of palindrome (ablewasiereisawelba): True

ba is an anagram of a palindrome: False
bc is an anagram of a palindrome: False
dec is an anagram of a palindrome: False
loof is an anagram of a palindrome: False
ampdaaiammd is an anagram of a palindrome: False

One simple optimization that can be made is to add these two lines:
if odd_counts > 1:
    return False

just after the line "odd_count += 1". What that does is stop early if it finds that the number of characters with odd counts is greater than 1, even if there are many more counts to be checked, since our rule has been satisfied.

If I think up more optimizations to the above solution, or any alternative solutions, I'll show them in a future post. Update: Since it is on a related topic, you may also like to check out this other post I wrote a while ago: A simple text file indexing program in Python.

BTW, the two longer palindromes are lower-cased, scrunched-together versions of these well-known palindromes:

Madam, I'm Adam.

Able was I ere I saw Elba

(attributed to Napoleon).

- Vasudev Ram - Dancing Bison Enterprises

Signup for news about products from me.

Contact Page

Sunday, November 16, 2014

Scaled Inference, a cloud AI startup by ex-Googlers, gets Khosla Ventures $8M funding

By Vasudev Ram

Saw the news via this tweet by @asenkut - Aydin Senkut, Google's first product manager, and founder of Felicis Ventures.

Here is a TechCrunch article about the funding of Scaled Inference by Khosla Ventures:

Excerpts from the article:

Scaled Inference, a startup founded by two ex-Googlers that is building a cloud-based platform for third parties that want to use artificial intelligence and machine learning tools to run their apps and services – has raised another round of funding to continue its development and hiring, a Series A round of $8 million from Khosla Ventures.
has been putting together some of the first products (or services) that will run on top of that platform: pattern recognition, anomaly detection, prediction, and predictive ranking, which will be accessible by developers by way of a set of APIs.
As an example of where pattern recognition might be applied, he says, take a bank or credit card company like Visa.
“The pattern recognition APIs can then be used to detect correlations such as increased spending during certain time periods, at certain locations, by certain customers, on certain items, for certain combinations of these attributes,” he says. In turn, this type of insight can help guide important business decisions
this is a long-term vision, but this is one of the reasons that Scaled Inference went with the investors that it has done, Sercinoglu says. “Vinod Khosla is a long term investor, one of the longest-term thinkers in the business, and ours is a long term vision for AI and machine learning. That really resonated with him.”

Longer term, he says, “We seek to offer the same powerful technology as a public cloud API to everyone and for virtually any application; anything from personalized magazines to intelligent context-sensitive address books, app launchers, e-commerce sites,
They are not open to the public yet.

The idea seems interesting, not just for the applications mentioned, but also for the fact that they plan to offer the service as a public cloud API, available to everyone, so looks like it could have a lot of potential.

- Vasudev Ram - Dancing Bison Enterprises

Sign up to know about new products from me..

Contact Page