Handling JSON in Python

On php, I just use json_encode() and json_decode() to handle JSON data with ease. Python has a built in module named “json” for the same purpose. Here’s the link for the official documentation: http://docs.python.org/library/json.html :) The documentation has pretty examples. Still, I wanted to try things out by myself. I used Python to parse the JSON output of a certain Yahoo! Pipes feed :

 
import json,urllib
data = urllib.urlopen("http://pipes.yahoo.com/pipes/pipe.run?_id=a0b9e3295664870ee81711aed6f7ecd6&_render=json").read()
d = json.loads(data)
for x in d['value']['items']:
    print x['title']

Output:

masnun@ubuntu:~/Desktop$ python jsontest.py
Sms Plugin
..........
[SNIP]
..........
Profit Spreadsheet
Graphic Web Des. Needed Fast
Customer Ordering Website
Dating Site Profiles
masnun@ubuntu:~/Desktop$
Tagged | 1 Comment

Created Another WordPress Theme

I have finally chosen web application engineering as my core subject of specialization. I have always appreciated the beauty and usability of web applications. You can easily paint your dreams on a web page, rather than on a desktop software. I have been developing mobile web sites for over 3 years now. I loved playing with WML and later XHTML along with bits of HTML. Later, when I realized that I have a good command over PHP, I looked at the web industry. I introduced myself to serious web development through my free lance career. The very first free lance job I got, involved setting a javascript countdown timer. I had to design the page and install the counter with a newsletter subscription form. I was in a mess with HTML and CSS. I was a newbie that time. (In fact, even today I don’t know much designing). Luckily the project was very simple and I didn’t have to code much. But still I got paid $30 :) I was inspired and started looking for more and more free lance jobs. Given my lackings in web design skills, I tried to find only projects which required programming alone. But I had little luck. I always had to play with bits of designing on almost all the projects. Gradually, I started learning HTML and CSS over time. When I joined Leevio, Hasin Vai introduced me to massive designing trends. I was amazed to see the beauty of web designs.

I was using WordPress for my personal website for a long time. I was always interested in the WP API. But somehow I couldn’t make up my mind to do something with the API. Hasin vai inspired me a lot to work on WordPress. Finally, on a good day, I managed to build my first WP plugin — “gaf-text-link”. The plugin was featured by GAF and is available from the affiliates page of GAF (Now, freelancer.com). GAF paid me $100 appreciating my work as a marketing of their service. I was damn motivated to work on WordPress again — because of the money. Whenever I looked at the job listing at my favorite free lance sites, WP related projects were available in a large numbers and many of them involved good price :) My desire to work with WP grew.

I later created another plugin to integrate admob ads into WP. Hasin Vai one day gave me links to a bunch of nice of books on WP. They were great and I really learnt a lot. I coded up my first WP theme which was being used on this site until yesterday night.

From yesterday, I started working to improve my design skills. I started with WP theme design so that I can gather knowledge on two topics from a single work. I have got decent command over CSS based styling now, but I am still falling short in layout designing. My design last night was not up to mark, at least it was not what I wanted. Hasin vai suggested me to use a CSS framework for layout. I had two primary options — fluid.gs for fluid layout and 960.gs for fixed width layout. I chose 960.gs since I wanted my layout to be fixed width. It was cool working with it. It saved me a lot of time and let me do what I really wanted to do! :D I am not willing to use raw CSS for layout again ;-)

Not only CSS and HTML, I have also worked with GIMP for bit of image editing. I am very new to GIMP. Had to struggle a bit with the interface. I hope I will be able to cope up really soon.

I finished the theme just a few hours ago. I made it widget-ready this time. It’s not bad for me! I will keep digging with designs and see if I can come with something better :)

Tagged | 6 Comments

A Simple Web Server in 30 Lines of Python Code

I haven’t recently had much time to do the fancy R&Ds I always loved! Today afternoon I was not feeling good to study, let alone that huge microeconomics assignment. I fired off the python interpreter and started coding aimlessly. I was inspecting the http.server module of Python 3 when an urge to create a web server popped into my mind. I started studying the Python 2.X docs and soon coded up a very simple web server. It can parse and serve dynamic pages (currently php, py and pyc files). It can not yet serve static files. :( And one more issue: If you run a phpinfo() with it, you won’t get a good looking output like you get on a LAMP, WAMP or MAMP.

To start the server, just use this command:

python server.py

Source Code:

 
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
import os,glob
 
class RequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        fpath = os.path.join(os.getcwd(),self.path[1:])
        response = ""
        if os.path.isfile(fpath):
                response = open(fpath,"rb").read()
                if fpath[-3:] == 'php':
                    response = os.popen("php5 -f " + fpath).read()
                if fpath[-2:] == 'py':
                    response = os.popen("python " + fpath).read()
                if fpath[-3:] == 'pyc':
                    response = os.popen("python " + fpath).read()
        elif os.path.isdir(fpath):
                dirlist = glob.glob(fpath + "/*")
                response = "<html><head><title>Directory Listing of " + os.path.basename(fpath) + "</title><head>"
                response += "<body>"
                for x in dirlist:
                    response += "<a href=\"" + os.path.basename(x) + "\">" + os.path.basename(x) + "</a><br />"
                response += "</body></html>"
 
        self.send_response(200)
        self.send_header("Content-type","text/html")
        self.end_headers()
        self.wfile.write(str(response))
 
httpd = HTTPServer(('',7676),RequestHandler)
httpd.serve_forever()

I love you, Python! :D

Tagged | 2 Comments

A reflect() Function For Quick Reflection in PHP

The Reflection API is the perfect way to reverse engineer and inspect php objects and functions. I have written a function to quickly pass params to the Reflection API and get things done.

Here’s the source codes:

 
<?php
 
function reflect($data,$return = false) {
    $string = "\n \nReflecting: {$data} \n";
    $string .= "\n====================\n";
    if(is_object($data)) {
        $string .= Reflection::export( new ReflectionClass($data), true );
        $string .= "\n====================\n";
    } else {
        if(function_exists($data)) {
            $string .= Reflection::export( new ReflectionFunction($data), true );
            $string .= "\n====================\n";
        }
    }
 
    if($return) {
        return $string;
    } else {
        echo $string;
    }
 
}
?>
Tagged | 1 Comment

Track Your Favorite Topic on Twitter: A PHP CLI Tool

Here’s a little php utility that will let you watch the Twitter public time line for your favorite topic. This is specially helpful for those who want to build some tracking application on the basis of the twitter streaming APIs.

Here’s the source:

<?php
$topic = $argv[1];
if(empty ($topic)) {
    $topic = "php";
}
$username = "twitter_username";
$password = "twitter_password";
$fp = fopen("http://{$username}:{$password}@stream.twitter.com/1/statuses/filter.json?track={$topic}","r");
while( $data = fgets($fp) ) {
    $tweet = json_decode($data,true);
    if(!empty($tweet['text'])) {
        echo $tweet['user']['screen_name'].": ".$tweet['text']." \n -------------------- \n";
    }
}
?>

Run this script with the keyword as the argument:

php twitter.php keyword
Tagged , | 5 Comments

Twitter Streaming API with Python

Hasin Vai has a good post about collecting tweets in real time with php. Here’s the post: http://hasin.wordpress.com/2009/06/20/collecting-data-from-streaming-api-in-twitter/ :) If you are a php developer look into there.

Well, I am going to cover Python for the Twitter streaming API. You need the “tweetstream” python module which depends on the “anyjson” module. Please make sure you have these modules installed before you proceed.

Download Links: anyjson | tweetstream

To install these packages, use the built in setup.py of the individual packages :

sudo python setup.py install

In my case, I didn’t have the “setuptools” python module installed with my default python installation. So I had to install it to get the setup.py work:

sudo apt-get install python-setuptools

I love the magic of debian packages :)

Now, you can install the modules and dive into our example:

 
#! /usr/bin/env python
 
import tweetstream
 
stream = tweetstream.TweetStream("twitter_username", "twitter_password")
 
for tweet in stream:
    if tweet.has_key("text"):
        print tweet['user']['screen_name'] + ": " + tweet['text'] , "\n"
        print "-----" * 4

That’s it! In real, you get the stream dictionary which you can use to manipulate the tweets.

The “tweetstream” module also lets you access the Track and Follow APIs in a superb fashion!

I love Twitter, I love Python! :)

Tagged , | 3 Comments

PHP: Setting the include_path for quick includes

PHP being the first language I loved and used for a long time, I now use it for all sorts of purposes. I also love and use Python but very often I end up coding in php because I am more habituated to this language than the beautiful python :) Again for some simple tasks, php is loads better than Python. Just imagine you need the md5 hash of a string. Will you import the hashlib first, then go through the md5() and hexdigest() methods ? Nope. I will simply fire off a php interactive shell and use the built in md5() function with an echo.

One of the major problems I was facing that all the external libs I use, I have to copy them into my current working directory or have to use some other techniques to import them into my current script.

To get a permanent work around this problem, I decided to use the include_path directive in php.ini :) I created a directory named “phplib” under my home directory and put all the libs in there.

Next, I opened up /etc/php5/conf.d/ with root access and added a new file named phplib.ini with the following contents:

include_path = "/home/masnun/phplib"

Now, whenever I need a library loaded, I just type :

<?php
include("mailer.php");
?>

That imports the mailer class, even on my interactive php shell :)

Interactive shell
 
php > include("mailer.php");
php > $m = new Mailer();
php > $m->setTo("maSnun <masnun@gmail.com>");
php > $m->setFrom("phpGeek <masnun@leevio.com>");
php > $m->setSubject("A bunch of files");
php > $m->setMessage("Hello buddy! Files attached!");
php > $m->sendMail();
php >

PS: Well, since the php interpreter always has your home directory as the CWD, you could just use include(“phplib/mailer.php”); in this case. But what about the php module running with apache? The include_path affects that one as well. It helps! But also it has a negative side. Make sure you upload the respective libs to the web server before you deploy your web application on the production box! :D

Tagged | 1 Comment

The Google Go Experience

I have always wanted to check out the Go language sooner but unfortunately enough couldn’t have a go… Today I googled if there are any debian packages for Google Go and luckily I found one… It’s of course not an official release from the Go team. Still it interested me. I downloaded the file and installed the package. Yeah, it works! This is the first time I am playing with Go, so no issues have been encountered. But the debian package requires me to overwrite some of the GCC files. Again, I am not a serious C/C++ programmer, so I don’t know if that has damaged my GCC to any extent.

Oh yeah, I forgot to mention I am on Ubuntu 9.04 :) I downloaded the package from this link: http://drop.io/gccgodeb/asset/gccgo-4-5-0-1-i386-deb

It’s nearly a 27 MB of download. I downloaded and double clicked to install it. The installation failed with some errors. So, I pointed back to the Original Source Page (Author’s Blog Post) of the package and re-read carefully. I had to use the command line to set “–force-overwrite” mode to the dpkg utility so that it could overwrite files at it’s discretion. Here’s the command:

sudo dpkg --force-overwrite -i gccgo_4.5.0-1_i386.deb

Now, for quick demo, I downloaded the sample “Hello World!” source code from the official Go website at http://golang.org/ :) (Hey, wasn’t it http://go-lang.org the last time I visited it? No way, both URL works now :D )

package main
 
import "fmt"
 
func main() {
  fmt.Printf("Hello, ??\n")
}

PS: I am sorry that my Syntax Highlighter doesn’t yet have the Go syntax highlighting patterns. Neither does my code editor! But still I used the code block, just out of a good practice! :P

Then I compiled the source (after naming it masnun.go) with the following command:

gccgo masnun.go

It created the file a.out . I ran it from the command line again:

masnun@ubuntu:~/Desktop$ ./a.out
Hello, ??
masnun@ubuntu:~/Desktop$

It was pretty easy in fact! I am going to play with Go at my leisure to check out what it has to provide the developers with… I love Python, let’s see if Go can give me anything more than Python :)

Thanks to Google, we have another programming language to do our bidding! Happy programming!

Tagged , | 2 Comments

Sending Mail Via Postfix: A perfect Python Example :)

I have been playing with Postfix for the last couple of days. I have already posted a perfect php class for sending email with attachments in a previous post. The Python example I posted earlier does the job for me but it was not perfect like the php one.

After digging a while, I found a nice way to do the same for python as well. I have just tested the codes and the result is perfectly what I desired :)

Here’s the code:

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import os
 
def sendMail(to, fro, subject, text, files=[],server="localhost"):
    assert type(to)==list
    assert type(files)==list
 
 
    msg = MIMEMultipart()
    msg['From'] = fro
    msg['To'] = COMMASPACE.join(to)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject
 
    msg.attach( MIMEText(text) )
 
    for file in files:
        part = MIMEBase('application', "octet-stream")
        part.set_payload( open(file,"rb").read() )
        Encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment; filename="%s"'
                       % os.path.basename(file))
        msg.attach(part)
 
    smtp = smtplib.SMTP(server)
    smtp.sendmail(fro, to, msg.as_string() )
    smtp.close()
 
# Example:
sendMail(['maSnun <masnun@gmail.com>'],'phpGeek <masnun@leevio.com>','Hello Python!','Heya buddy! Say hello to Python! :)',['masnun.py','masnun.php'])
2 Comments

Good Bye 2009, Welcome 2010!

I should feel sorry now that 2009 is leaving me, leaving us! I have had the most eventful year in 2009 :)

First things First
What were the things those happened first time in my life?

– I sat for University Admission test.
– Got chances in SUST CSE and 4 disciplines in KU.
– My University life began.
– Started free lancing seriously.
– Started earning money at a regular interval.
– Went to BAD picnic and my first tour with the BAD family into the Sunderbans :)
– Joined my first Job (At Leevio as a software engineer).
– Attended the first ever FICC.
– Stood 3rd in my first Uni term final :)
– Started using Ubuntu seriously
– Started working with WP API
– Worked on GAE and other Python stuff :)

What else?
– BAD won the cricket championship. I was an active part in the gallery.
– Met a bunch of cool friends.
– Lost a few fake ones as well.
– My brother got chance in Khulna Zilla School :)

Wishlist for 2010
I would wish only one thing — A laptop in 2010 :)

I am really contended with what I have got in life. I am happy with what I got in 2009. Thanks to the Almighty Allah for everything HE has blessed on me :) I just pray that nothing bad happens in 2010 :D

I pray to the Almighty so that HE bestows his blessings upon those who have a worse life than me. I have nothing more to expect :)

Good Bye 2009, Welcome 2010! :)

Leave a comment