• About

    Swaroop C H is 27 years of age. He graduated in B.E. (Computer Science) from PESIT, Bangalore, India. He has previously worked at Yahoo! and Adobe.


    Email: swaroop (at) swaroopch.com

    Read more about him

  • Subscription

    If you want to know when new stories and articles appear on this website, you can subscribe to the RSS feed or have them emailed to you.

  • Want me to write about something?

  • I'm a Wannabe Hacker

    The Glider: A Universal Hacker Emblem

Archive for the ‘Python’ Category

Coding Problems for Homework

Tuesday, November 24th, 2009

“Coding Homework” is a small website that I have built to list small problems that one can work on, to learn how to use a new programming language. For example, small problems requiring to read from a file, or to use regular expressions, how to find duplicate files in a folder, and so on.

Note that the problems listed on the site is not for testing your algorithm skills, there are many sites for that already.

This list was inspired by repeated requests and suggestions from readers of A Byte of Python for homework problems at the end of each chapter to exercise the skills they have just learned. So I thought why not make it applicable to any language and multiple programming skill levels. And it’s a good topic that can be collaboratively worked on with the programming community, à la Stack Overflow.

All the content will be licensed under Creative Commons Attribution-Share Alike 2.5 India License so that anybody can reuse this content, especially in classroom situations.

Screenshot of "Coding Problems for Homework" website

I also had my own specific goals when implementing this side project:

  1. Solve the lack of “homework problems” for people to exercise their programming skills, especially in the context of learning a new programming language.
    • I am not trying to replace existing lists but rather focus on making the reader active (providing exercise problems) than letting him/her be passive (reading code listings).
  2. Learn how to do website layouts, specifically how to use YUI Grids CSS.
  3. Learn how to pick colors for website design; ColorCombos turned out to be useful.
  4. Learn to use Google App Engine.

It has been a fun side-project, spending a few hours here and there. It is very far from polished, but the basic functionality works. There is still more to do — adding a search functionality, conforming to standard UI design patterns, caching for the rendered HTML (from Markdown), optimizing the housekeeping code, and so on.

This site itself is a good example on the kind of problems that beginners can work on, but they would not know what kind of problems they can solve and what level of expertise (beginner / intermediate / advanced) would be needed. That is where this list of problems can help.

I request you to spend 5 minutes of your creativity to add a few problems so that beginners and intermediate level folks will have interesting problems to test their learning of a new programming language. Thanks!

It might be helpful to you as well when you’re going to play around with functional languages (Haskell, Erlang, etc.), funky new languages (Ioke), or new languages by big companies (Go).

Link: http://codinghomework.appspot.com

An interactive version of A Byte of Python

Monday, November 9th, 2009

Roorky is a new startup that has created a new file format and software for interactive books. As part of the default installation of the software, they are bundling A Byte of Python free with the software.

What is interesting is that this may help complete beginners who stumble in getting started with IDLE, etc. The most common email that I get asked is when people run python abc.py on the IDLE interpreter prompt and wonder why it is not working – both the concepts of two command lines as well as the concept of running a program is not grokked well by beginners who are getting started, especially people who are self-taught. From that perspective, this is an interesting approach to the problem.

I am still not convinced about this approach because people cannot bypass the learning of how to edit, compile and run the code in the native environment, because that will be needed when writing new programs. It will be interesting how the two opposing needs will be balanced.

But I hope beginners will try it out and see if it helps them get started.

A full size video of a walkthrough of the software is available at the Roorky website.

Note: Be warned that it is a beta software.

Interview with CocoaCast

Thursday, May 7th, 2009

I was recently interviewed by CocoaCast (mp3) to talk about Python for their Mac developer community, as part of their “Unbound Developers” podcast series.

I was apprehensive about doing this because I’ve never been live-interviewed before, but in the spirit of doing new things, I went ahead and it turned out to be fun.

We talked about Python, my background and my startup, Macs, Python vs. Ruby and Django vs. Rails, Apple and Google, cloud computing, databases, Scala/JVM, performance, collection classes, and so on.

Surprisingly, Vlad and myself had talked for nearly 25 min. It’s a fun interview, although, there’s probably nothing new to know from this podcast for those who have already read the book.

Why Stack Overflow is useful

Thursday, February 5th, 2009

In one of my previous thoughts, I had mentioned about a website called “Stack Overflow” in passing.

I’m surprised that many people do not know or use this resource and community. Just a brief search over there would answer so many questions that programmers have.

For example, there is a suggestion on my skribit page:

“how to give back to the open source community”?

And this question has 20 votes!

I wonder why this question to me, then I remembered this suggestion popped up soon after I wrote “Why use Creative Commons license?” where I had written “The book was intended to be a contribution back to the open source community. We constantly keep taking and taking – whether it is using Linux, Vim, Firefox, or countless other software, so it felt great to be useful to the community in return.”

I guess I had it coming.

First of all, I would say that the best place to actually learn such a topic would be another book (I bet you saw that one coming!) called Producing Open Source Software by Karl Fogel (which is itself an open source book) to understand how an open source project works right from the technical infrastructure to the social and political infrastructure, how to communicate, and so on. And finally, the chapter on Volunteers explains the different kinds of volunteers that are helpful to an open source project which indirectly means that those who are interested can participate in the projects in one of those roles => You’re giving back to the open source community!

But perhaps there are better suggestions in this discussion on Stack Overflow when somebody asked, duh, How to get involved in an open source project?

Someone also posted another skribit suggestion asking:

hi, can u give me a link on examples with python or projects in python book i am a beginner

Guess what? I already answered that in a discussion at Stack Overflow.

The answer is that there are two projects – the “Programming Language Examples Alike Cookbook” project and the Rosetta Code project which lists vast numbers of example programs in multiple programming languages.

Again, the person could have found this answer already by a simple search on the Stack Overflow website.

For some of the programming queries I had, I didn’t know whom to ask. There used to be an internal algorithms-discuss mailing list when I was at Yahoo!, but whom do I turn to now? The answer again was “Stack Overflow” (which at that time was yet to be launched, so I was waiting in anticipation):

How to convert floats to human-readable fractions?

Let’s say we have 0.33, we need to output “1/3″. If we have “0.4″, we need to output “2/5″.

The idea is to make it human-readable to make the user understand “x parts out of y” as a better way of understanding data.

I know that percentages is a good substitute but I was wondering if there was a simple way to do this?

And someone nicknamed “Epsilon” pointed to me to a brilliantly simple algorithm by David Eppstein which exactly answers this question.

# Usage: ./frap <fraction> <maximum denominator>

$ ./frap 0.33 10
1/3, error = -3.333333e-03
3/10, error = 3.000000e-02

$ ./frap 0.2342 100
11/47, error = 1.574468e-04
15/64, error = -1.750000e-04

Isn’t that amazing? Both the algorithm and the community at Stack Overflow.

If you want to know why Stack Overflow works so well, there is a discussion on that, at Stack Overflow you might want to read :-)

A Byte of Python in hard copy

Wednesday, October 1st, 2008

If you’re the kind of person who prefers to read a physical book vs. online books, then you’ll be happy to know that the A Byte of Python book is now available as a printed hard copy.

The best part is that the hard-working translators can also publish their translations and sell the printed copies, benefiting both the readers and the translators.

I had received many requests from readers for hard copies of the book and I’m glad to finally get this working. Interestingly, I was previously trying to get the book printed via CreateSpace because the book would automatically get listed on Amazon.com (since CreateSpace is owned by Amazon). However, their process was not streamlined and confusing. Worse, I couldn’t get the PDF in their required size formats because of a bug with mwlib.rl.

I got tired and decided to try Lulu and I was very surprised. They are miles ahead in terms of usability of their service as well as wide range of options and sensible defaults. For example, it was a pain waiting for manual approval of the book by the CreateSpace staff and it is an unnecessary delay every time I upload a new version. On the other hand, Lulu made it very easy to design a rudimentary cover using their process. Overall, I was able to make the printed copy available for purchase in a single evening.

Of course, all this is possible because of the ability to generate PDFs from a wiki, thanks to the nice people at PediaPress.

Update: For Indian readers, the book is now available via pothi.com.

Book updated for Python 3.0

Friday, September 5th, 2008

After a gap of 3.5 years, I’ve finally updated the ‘A Byte of Python’ book.

The interesting news is that it is updated for the upcoming Python 3.0 language making it probably the first book to be released for Python 3.0.

The book is now a wiki too at www.swaroopch.com/notes/Python which means you can contribute too!

The book and wiki are now under the Creative Commons Attribution-Share Alike 3.0 Unported license. The Non-Commercial clause present in the previous edition of the book has been removed. It was becoming a hurdle for translators as well as people who wanted to use the book for genuinely good activities, so I decided it to drop the clause.

Since it is a wiki, volunteers can directly create their translations on the wiki. This eliminates the need to learn DocBook XML and its tools which had become a hindrance for many translators, and I’m glad to see this already bearing fruit with Eirik Vågeskar starting off a Norwegian translation at www.swaroopch.com/notes/Python_nb-no:Forord.

I will soon be making a printed version of the book available as I have had many requests for this.

So back to the main question: Why an update after nearly 4 years? Two reasons.

First, because of foss.in. I dedicate this new release to the foss.in community for their spirit and enthusiasm over the years which have rubbed off on me and kept me working on the update of the book.

Second, Over the past few years, the readers’ reactions have been simply splendid:

Neil (bigdealneil-at-yahoo-dot-com) said:

“(I) got an if else to work and I can follow your tutorial, which I have never been able to do no matter who wrote the thing! you’re a genius Swaroop!”

Gao shuai (ejwjvh-at-126-dot-com) took the effort taken to write an email to me in English:

dear swaroop: I am a chinese student.My name is gao shuai,”gao”is my family name. Although your book is easy to understand,but my english is bad,so what I read is the chinese edition. I have made some programs now.It is interesting.I like it very much.

I emailed back and he replied:

Mr Swaroop: I am exciting to read your back. _(Editor’s note: I think he means ‘reply’)_ Tt is the first time that I talk to foreigner though the internet. I saw that you have your own mail ab.I think You’re a great man. Thanks for your back!(*^_^*) regards, gaoshuai

The interesting part is that this student somewhere in China was being benefited by this book and he “talked to a foreigner through the internet for the first time” and that person was me. It was truly humbling.

People are even putting ads for it, and I had no clue about it until I chanced upon it myself:

An ad for 'Byte of Python'!

If that wasn’t enough, I found out that there are 8-9 university courses officially using the book, including Harvard and other institutions. And apparently even NASA is using the book in their Jet Propulsion Laboratory.

Users have suggested that it should replace the official tutorial but I really wouldn’t go as far as that :)

Recently, I had sent a sneak peek for the book’s group of readers and within a day, I had the first 10$ donation by Horst JENS. I remembered seeing that name somewhere, so I searched my emails and found this:

On Mar 4, 2007:

“Hello Swaroop, i teach a class of (3) Children how to program in Python. Just want to thank you because without your ‘a byte of python’ (that i read one year ago) i would maybe never have begun to code in python and consequently would never leaved my old job to become a Python teacher.”

A person in Vienna, Austria changed his career from a sys-admin job which he didn’t like, to teaching children about programming, a job he loves. Wow! Again, this is so humbling. I could have never imagined that a small book can make such a difference.

The point is that I’m grateful for all these people writing to me and sharing their delight and stories. The book is still alive and kicking thanks to all these people.

Happy programming!

Hack Day India

Monday, October 8th, 2007

I was at Yahoo! Open Hack Day at Bangalore on Friday and Saturday. 24 hours of hacking, meeting lots of old friends, and sarcasm unlimited. It doesn’t get better than this.

  • 2007-10-05 Fri 02:30 PM
    • Arrived at Taj.
    • Registered myself, got the schwag
    • Met Raghu and discussed the presentation he’ll be making on Flex
    • Social networking, the offline kind

Hack Day India 01 Raghu and me checking out his Flex talk ppt Hack Day India 03

  • 2007-10-05 Fri 03:30 PM
    • The presentations start.
    • Joe starts the ceremonies.
    • Chris starts the first talk on what Yahoo can do for you. Yahoo APIs, that is.

Hack Day India 06 Hack Day India 10

  • 2007-10-05 Fri 05:30 PM
    • On the fly, we three become a team : Pradeep, Raghu and myself.
    • Pradeep and myself know each other from Deep Root Linux projects, after college hours, in PESIT. Raghu and me are colleagues. Pradeep and Raghu met the first time today.
    • We start discussing ideas…
    • WiFi’s good

Hack Day India 11 Hack Day India 12

(more…)

Why students and open source?

Tuesday, October 2nd, 2007

Two days before the BMS College Information Science Department Fest called “Genesis 2007″, I received an email from a couple of students asking me to talk about “introduction to open source”. Apparently, they were frantically looking for a speaker. Since I’m not the right person for this, I agreed to come only if they didn’t find someone else… and I ended up going there on Friday.

The talk was supposed to be an introduction for a day-long session on Open Source Hacking which was organized by few enthu students trying to get other students interested.

I started making the presentation on the midnight before Friday, so I didn’t have a very polished presentation, but I had something reasonable. The title of the talk was “How to make money from coding (or Why Open Source)”. That should get their attention.

15 minutes before the talk, there were 2 students in the hall. I wanted to start the talk on time and decided to start without much crowd anyway. My sore throat was troubling me and I was coughing every two minutes. Anyway, I started off with a funny anecdote. It flopped. Oh boy.

Then, I decided they’re not warmed up yet, and recovered quickly. 15 minutes later, the 225 seater hall was full. Phew.

Genesis 2007 at BMSCE

An hour later, they were still all there, they were asking lots of questions and they seemed genuinely interested. I hope the students do take FOSS software seriously, if not for the freedom and open source aspects, at least for their own career aspects which I detailed out in the talk. (And I’m sure once they’re hooked, they will later “get” the freedom and open source aspects.)

Why do I say that? Well, it comes down to the first question in the Q&A session – “How to get into Yahoo!?”, and I replied “Well, do you want to know how I got into Yahoo!?”. A unanimous yes. I told them the MySQL story, the Python story and few other tidbits. Now, they’re really listening. I pointed out that I didn’t have any special skills, just the knowledge of these two open source software got me the job at Y!, and it saved me from a service industry job (no offense meant, just a personal preference).

Next question: “Any regrets in college life?”. It caused a flashback
in my mind on Atul’s words
“There are two times you innovate in your life – one is when you are a student, the other is when you retire.” Back then, I didn’t believe him. Now, I do. So, I told them “I haven’t yet regretted not scoring well in college. This is the only ‘free time’ you have, so use it well.” I got lot of smirks and “oh, please, we have so much to study” looks. I said “Two years later, I’ll see how many of you come back and tell me I’m wrong.”

Genesis 2007 at BMSCE Genesis 2007 at BMSCE

Then, after the session ended, a few electrical students said they wanted to get into the software industry and don’t know where to start. I told them that some of the best programmers I’ve known are from a mechanical background, so that’s okay. You should prove your skills, that’s all, your background shouldn’t matter, although it may be difficult to get your first job because you’re not a computer science student. Then, a telecom student. I was happy about this guy because he said he wanted to remain in the telecom domain but learn coding really well, I said that’s a very good decision he’s taken and told him to see open source projects such as Asterisk and OpenMoko. He said “I’m in my final year, just 8 months to go, am I too late?” I said “8 months is a really long time, you’re not late, you just have to start now.” (8 months is a long time when you think about it, but it seems to fly away so soon).

After that, students headed towards the computer lab where I gave a crash course in using subversion. I had to get back to work, so I didn’t stay for the rest of the day, but I heard there was a “good response” from the students.

In the end, I don’t know if anyone was inspired about FOSS or not, but I did see that few students absorbed the fact that knowledge and projects are going to get them good jobs, not just marks (of course, you do have to have a decent score), and working on FOSS projects is one way to achieve that.

P.S. If you’ve read this far, and you’re interested in learning how to contribute to open source software, then you’re in luck, because the foss.in community event is coming up soon. You can start right now by reading Atul’s latest post on foss.in.

Update : A related must-read article is “How to Get a Job Like Mine” by Aaron Swartz.

Lookup

Saturday, August 25th, 2007

Most of my writing inside the browser is done with the combination of the It’s All Text extension and Vim. During this writing, I use words whose meaning I know vaguely but don’t know the exact usage, and in such cases, I usually open a new browser window and look up the meaning of the word in an online dictionary. This process of looking up the dictionary meaning was getting rather tedious, so I wrote a small Vim plugin today to do this job with a simple command.

To use the plugin, first install BeautifulSoup. Then, get my lookup.vim script and copy it to ~/.vim/plugin/.. See the new version’s installation instructions, it now uses John Goerzen’s dict client implementation in Python.

Next time you use Vim, just place your cursor over any word and run :Lookup, and you’ll see something like this:

lookup.vim screenshot

Since, this is the only command starting with L on my Vim installation, I just end up running :L. Of course, you can always create your own keyboard shortcuts to make it easier.

Update: Based on the comments, I’ve updated the script to now use the DICT protocol and talks to some servers. This avoids screen-scraping, is much faster to use, allows me to now use both a dictionary and a thesaurus which is very very handy because I can see what other words I can use, and of course, doesn’t violate any TOS (which screen-scraping could amount to).

Big Brother

Tuesday, April 10th, 2007

Long ago, I had seen Nat Friedman show off a small Mono app that displayed the amount of time you spent on each application, and updated the chart in real time – so that you could clearly make out how much time you’re spending in the browser and how much time you’re actually spending on work.

I couldn’t locate that app, so I had been wanting to write my own version for a long time and finally got around to doing it today.

It turned out to be easier than I thought. First, I had to figure out how to find out what application has the current focus at any point in time, and I was trying to see if I can do it from Python (using the Win32 extensions, and yes, this hack is Windows-only). After a lot of searching, I finally found out that it boils down to just one line: win32gui.GetWindowText(win32gui.GetForegroundWindow()) and this would fetch the title of the application that the user is using at that moment.

After this, all I had to was record the window title every 5 seconds and increment the time spent for each window, and voila, the data is ready. I was quite satisfied with just a command line output, but Raghu egged me on to create a Flex chart frontend for it as well, and when Harish joined in, the fun really got started. The first problem was how to push the data from Python to Flex, and we use the oldest trick in the book – write it to a file. Next, Flex can read the same file as long as the file is in the same directory as the SWF (i.e. the Flash file) location. Then we parse the text to get back the original data, create a pie chart and point it to this data, and voila we have a beautiful chart:

big brother gui example

Now I can finally track what I actually end up doing the whole day, heh. You can download the files if you want to use it as well:

And who said meetings aren’t productive ;-)


Update:

  1. Chris J Andrews made a JavaScript GUI frontend.

  2. Theyagarajan modified the Python script to make it work on Linux.