Wednesday, 14 May 2014

The typical American programmer

Typical American symbolism
The other day a young friend asked me what programming language she should learn, to get a high paying job as soon as possible. I asked her to concentrate on data-structures, algorithms and problem-solving skills in general, instead of focussing on learning one programming language.

But the question still seemed important. I wanted to know what the typical American programmer programs in? How much does she earn? Where is the best place for her to live in?

So, I did my own little 'research' using Google and other websites. Here is what I found. Note that all this data is based the past 12 months (May 2013 to May 2014).

I am focussing on 5 very popular programming languages: Java, Python, Ruby, C# and PHP.

I am also focussing on the following 5 states: Florida, North Carolina, California, New York and Maryland.

1. What is the most preferred programming language?
American interest in programming languages
So, that is what Americans want to learn. Java is clearly the winner here, followed rather closely by Python. All the other languages are far behind them. I am sure one of the main reasons for this is Android. You can't be an Android developer without knowing Java. Moreover, a lot of enterprise systems are still written in JEE. However, a lot of developers from third world countries are Java programmers, and they sure are cheap, so I am assuming the salary of a typical Java programmer won't be very high. We will know if that is actually the case next.

2. What is the average salary of programmers in these languages in the US?

The salaries shown here are the national averages, for that exact designation. So, seniors and associate programmers are bound to get higher or lower salaries. As expected Java developers are paid the least. Now, I am talking about just Java developers (not Android or JEE specialists.) Python and C# seem to be the languages to learn, if you are interested in high salaries.

3. What is the average salary of programmers in these locations?

Here, I am considering the average salary of a "Web Developer". This designation is likely to include most of the languages that I mentioned. So, Florida is the state with the lowest salary, and New York is the one with the highest. This is expected, based on the cost of living in those states.

Based on these, I have come to the conclusion that my young friend should learn Python, and move to New York. The chances of her finding a good job seem to be the best there. But hey, we all should only do what we are interested in doing, and live only where we want to live! So these were merely suggestions.

But I had more questions. What are the salaries in other countries like, compared to the US. Surprisingly, much of the world earns far less than what Americans earn. Here is what I found.
Very interesting indeed. But what are the salaries like in the third world countries, you ask?
Note that those are the yearly salaries of people there. It is more than obvious why companies prefer to outsource projects now.

I have gathered all data from online sources only, so you can always look it up. Some numbers have been rounded off to the nearest 100. That is all for now. Please do leave comments and share.

Tuesday, 13 May 2014

Quick Tip: How to find your public IP address using Python

To know your internal IP address is simple. Just type out ifconfig in the terminal, and you get it.

However, that is not the IP that is visible to external websites. Yes, in the browser, you can simply type "my ip" on Google, and Google will show it for you.

But what if you want to know that public IP address in a script? This tutorial will show you how to do that.

There are a lot of websites that have a JSON-based API that can return your public IP address, and other such information. One such website is http://httpbin.org

So, if you visit http://httpbin.org/ip, you will get your public IP address, like this,
{
  "origin": "2.6.8.2"
}
Now that we know how to get the public IP address, we just need to write a script that can connect to this website, and parse the JSON that it returns. Let us use python for this.
from json import loads
from urllib2 import urlopen

# This line will connect to the website, read its contents
# and parse the JSON output
data = loads(urlopen("http://httpbin.org/ip").read())
print "The public IP is : %s" % data["origin"]
The output will look like,
The public IP is : 2.6.8.2
That is all. This is useful if you are using a proxy, and want to confirm that your public IP address is actually different. Note that if the proxy you are using is not truly anonymous, you will get two or more IP addresses, one of which will be yours, and one that belongs to the proxy server.
Hope you liked this quick tip. Please comment and share.

Monday, 12 May 2014

How to create a simple Reddit Bot in 10 minutes

Ever read a comment from a Reddit bot? It is very likely that you have, if you are an avid Redditor. You must have noticed some of the famous ones like /u/imirror_bot and /u/xkcdcomic_bot. For a longer list of Reddit bots, check out this comment on Reddit.

So, how do you make one?
Well, it is very simple to make a bot. Most bots are written in Python using an open-source library named PRAW. And today, we will be using just that. If you know a little bit of Python, you are good to go.

Step 1 : Install PRAW
Now, I am going to assume you are using Ubuntu. Just type out the following on the command prompt,
sudo apt-get install python-pip   #in case you don't have pip installed
sudo pip install praw
Step 2 : Decide what your bot should do
Bots are generally designed to automate one task. Let us create a bot that goes through all the comments on a subreddit, and lists the users that use the words "thanks" and "please". So, we are finding all the users of a subreddit who are polite.

Step 3 : The algorithm
Here's how most bots work,
  1. Fetch comments
  2. Process those comments
  3. Sleep for sometime
  4. Go to 1
Most bots work on entire subreddits, instead of just one post. For this tutorial, we will be using /r/AskReddit.

Step 4 : Now for the actual code
This code should be self-explanatory. The in-line comments should help you if something is not clear.
# author: Hathy (WhyCouch)

import praw
import time

# Initialize PRAW with a custom User-Agent

r = praw.Reddit('Simple comment parser from WhyCouch')

polite_users = set()   # to avoid duplicates

for i in xrange(0,10):  # Run the loop 10 times
    comments = r.get_comments('askreddit')
    for comment in comments:
        body = comment.body.lower()
        if body.find("thank") != -1 or body.find("please") != -1:
            polite_users.add(comment.author)
    time.sleep(120)   # Sleep for 2 minutes

print "The polite users were :"
for user in polite_users:
    print user
Your output would look something like this,
The polite users were :
ChaletMontagne
Brackit-
That is all there is to it, and I am sure you too were able to create it in less than 10 minutes. You now have your own little Reddit bot that performs a meaningful action. Refer to the PRAW documentation if you want to create more advanced bots. I will soon be creating a new tutorial, that will tell you how to create a bot that can post comments.

As always, please comment and share.