Tag Archives: python

C++ as a scripting language

Jussi Pakkanen of Canonical writes:

With the release of C++11 something quite extraordinary has happened. Its focus on usable libraries, value types and other niceties has turned C++, conceptually, into a scripting language.

I don’t necessarily agree with everything on that post, but one thing’s clear: C++ isn’t what it used to be. With C++11 things have changed, a lot.

If I was to write a script it’d be python or a shell script, I wouldn’t even consider C++. But then again, if that is some script you need to run every once in a while or one that has to be run over and over again within a short period of time, why not just write it in C++ and compile to native code? It would run faster than any other scripting language would, without waiting for some runtime or VM to load up.

And now you don’t need to learn the ins and outs of pointer arithmetic to do this. 😉

Python import and easter eggs

Amir Rachum has a good primer on python’s import. If you’re new to python you should definitely give it a go.

The post ends up with some cool import easter eggs.

import antigravity
import this
from __future__ import braces
import __hello__
from __future__ import barry_as_FLUFL

Try them yourself in a REPL 🙂

print statement for C++

print-stmt is a Python-like print statement for C++. It’s damn easy to add it to the project:

The easiest way to use this is to clone this repo and copy the file print.h into your project. Then simply

#include "print.h"

at the top of the file, but after the system #includes.

This is pretty handy. Some example features:

  • bool prints ‘true’ or ‘false’
  • A type that defines a member function called c_str() is converted to a string by calling this function.
  • A type that has a begin() member function that returns in iterator but does not have a c_str() member function is printed like a list, e.g. [“one”, “two”].

Processing music tags with Python

I was looking for the optimal way to traverse the directory tree with Python and found out that os.walk is the answer. To put this into practice, I wrote the following script which traverses the music library and finds out which MP3s haven’t been properly tagged. In other words, it will print the full paths of all MP3s that have any of the artist, album or title tracks missing. In my case, 502 of the 10466 songs had bad tags.

[sourcecode language=”python”]
#!/usr/bin/python

import os
import id3reader

rootdir = "/home/thameera/Music"
processed = 0
bad = 0

for thisDir, subdirList, fileList in os.walk(rootdir):
for fname in fileList:
if fname[-3:] == 'mp3':
processed += 1
fullPath = os.path.join(thisDir, fname)

try:
id3r = id3reader.Reader(fullPath)
except:
bad += 1
print "Error extracting id3 info from %s" % fullPath
continue

if None in (id3r.getValue('performer'), id3r.getValue('album'), id3r.getValue('title')):
bad += 1
print fullPath

print "%d MP3s processed. Found bad tags in %d of them" % (processed, bad)
[/sourcecode]

It uses the id3reader library to parse the tags. I don’t think it’ll work if you have files with other formats, like wma or flac.

This script can be further expanded to ignore certain directories, output only one entry per each directory that has bad tags, check for other tags as well, check if the artist tag does not match the name of the parent directory, etc, etc.

Most popular twitter clients

I was playing with Twitter’s streaming API and thought of finding out what the most popular twitter clients are. So I listened to Twitter’s stream of public tweets for half an hour and sorted the list of tweet sources. The sample contained 100,000 random public tweets. The top ten clients were:

  1. web – 24697
  2. Twitter for iPhone – 17564
  3. Twitter for Android – 13007
  4. Twitter for BlackBerry® – 10592
  5. Mobile Web – 3521
  6. UberSocial for BlackBerry – 1849
  7. TweetDeck – 1776
  8. Twitter for iPad – 1411
  9. Facebook – 1322
  10. Echofon – 1320

As expected, the Twitter web is the source of almost a quarter of the tweets, while the official clients for iPhone, Android and Blackberry take the next slots. And then comes the Mobile Web. Twitter for iPad sits in the 8th place. In other words, more than 70% of the world’s tweets are posted from official clients.

The complete list of results is here.

Kivy – Python framework for Android and more

Came across this article on Kivy for Android on Hacker News. According to kivy.org, Kivy is

an open source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps.

The widgets look pretty and the samples seem to have sophisticated and rich interfaces. The graphics are in fact drawn by hardware-accelerated OpenGL commands. The widgets are written in C to improve performance. I went through some of the Docs and the learning curve seems to be surprisingly short. I’m yet to build a package for Android, it’d work without much fuss hopefully. Will keep you updated.

And the best thing is, it’s Python; not the ugly Java.