RSS

Category Archives: Python

Threading in Python

It will be better for us if we directly start with an example. After every small patch of code, I have tried to explain what, how and why.

Let us start considering basic producer-consumer problem using thread module

import thread
import time

# Define a function for the thread
def process(person, delay):
    count = 0
    while count < 5:
        time.sleep(delay)
        count += 1
        #material = count
        print "%s material %s at time %s" % (person, count, time.ctime(time.time()))

# Create two threads as follows
try:
    thread.start_new_thread( process, ("Producer produces", 1, ) )
    thread.start_new_thread( process, ("Consumer consumes", 2, ) )
except:
    print "Error: unable to start thread"

while 1:
    pass

Output

Screenshot from 2012-12-11 14:02:25

In the above example we spawn 2 new threads using following method available in thread module

thread.start_new_thread(function, arguments_in_tuple_form)

Creating Thread using Threading module

To implement a new thread using the threading module, you have to do the following:

  • Define a new subclass of the Thread class.
  • Override the __init__(self [,args]) method to add additional arguments.
  • Then override the run(self [,args]) method to implement what the thread should do when started.

Once you have created the new Thread subclass, you can create an instance of it and then start a new thread by invoking the start() or run() methods.

Now we will see the same producer consumer example by using Threading module

import threading
import time

class myThread (threading.Thread):
    def __init__(self, person, delay):
        threading.Thread.__init__(self)
        self.person = person
        self.delay = delay

    def run(self):
        process(self.person, self.delay)

def process(threadName, delay):
    counter = 1
    while counter <= 5:
        material = counter
        time.sleep(delay)
        print "%s material %s at time %s" % (threadName,
                                             material,
                                             time.ctime(time.time()),)
        counter += 1

# Create new threads
thread1 = myThread("Producer produces", 1)
thread2 = myThread("Consumer consumes", 2)

# Start new Threads
thread1.start()
thread2.run()

Output

Screenshot from 2012-12-11 14:02:13

Synchronizing threads

Currently, The Python Interpreter (Python 2.3.4) is not thread safe. There are no priorities, no thread groups. Threads cannot be stopped and suspended, resumed or interrupted. That is, the support provided is very much basic. However a lot can still be accomplished with this meager support, with the use of the threading module, as we shall see in the following sections. One of the main reasons is that in actuality only one thread is running at a time. This is because of some thing called a Global Interpreter Lock (GIL). In order to support multi-threaded Python programs, there’s a global lock that must be held by the current thread before it can safely access Python objects. Without the lock competing threads could cause havoc, for example: when two threads simultaneously increment the reference count of the same object, the reference count could end up being incremented only once instead of twice. Thus only the thread that has acquired the GIL may operate on Python Objects

import threading
import time

class myThread (threading.Thread):
    def __init__(self, person, delay):
        threading.Thread.__init__(self)
        self.person = person
        self.delay = delay

    def run(self):
        threadLock.acquire()
        process(self.person, self.delay)
        threadLock.release()

def process(threadName, delay):
    counter = 1
    while counter <= 5:
        material = counter
        time.sleep(delay)
        print "%s material %s at time %s" % (threadName,
                                             material,
                                             time.ctime(time.time()),)
        counter += 1

threadLock = threading.Lock()
threads = []

# Create new threads
thread1 = myThread("Producer produces", 1)
thread2 = myThread("Consumer consumes", 2)

# Start new Threads
thread1.start()
thread2.run()

# Add threads to thread list
threads.append(thread1)
threads.append(thread2)

# Wait for all threads to complete
try:
    for t in threads:
        t.join()
except:
    print ""

Look at lines 18 and 20 in the code. We acquired the lock and then thread did its work. Later we released the lock.

Output

Screenshot from 2012-12-11 14:01:18

 

I hope this post gave you a basic idea of how threading works in Python.

To read further, visit:
http://linuxgazette.net/107/pai.html
http://www.ibm.com/developerworks/aix/library/au-threadingpython/

 
2 Comments

Posted by on December 11, 2012 in Python

 

Tags: , , ,

The Zen of Python

Tim Peters succinctly channels the BDFL’s
guiding principles for Python’s design into 20 aphorisms, only 19
of which have been written down.

    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!
    

Original source: PEP 20 — The Zen of Python

 
Leave a comment

Posted by on December 10, 2012 in Python

 

Tags: , , ,

‘Have you met PyKudos?

You might be thinking what wacky words are these! So let me tell you, even the ideas are as wacky! 😉

Let me introduce you to PyKudos, an organization started by me and Jay Rambhia

We are these guys who love experimenting stuff using python, and mind you, we are on a mission. 😀
PyKudos was created with nothing in mind but only a motive of cruising in this vivid ocean of Python.

We have taken up KudoEdit as our first project. It is a hybrid editor (in the making) for students, which will help in shifting the whole hard-copy system of assignments and journals, to soft-copy. The idea is not just limited to making an editor. We will be updating our code and our plans, soon.

Keep visiting! Cheers! 🙂

 
Leave a comment

Posted by on June 19, 2012 in Python

 

Tags: ,

Facebook BirthdayManager app

Its a busy life we live, isn’t it?

The day starts and ends and we have no clue what did we do the whole day. No time to catch up with friends, no time to stay home and spend some quality hours with ourselves.

And in all this hustle and bustle, we often miss on events, even miss wishing our friends their birthday, in spite of getting daily updates on Facebook. Also, we may forget wishing them if we don’t visit our Facebook account on daily basis. I wanted a solution to this. So I started making a Facebook app which fetches birthdays from your friends’s account and wishes them on their birthday automatically.

The app will automatically post “Happy Birthday <friend’s-first-name>! :)” to your friend’s wall from your FB account.

I started tinkering with Facebook API and found that most of the things were in PHP. I wanted a Python API for Facebook and found pyfacebook – A Python Interface to Facebook.

I started making an app on developers.facebook.com
Things mentioned are quite clear. You can easily make your app reading those guidelines. And then use Heroku or maybe Google App Engine to write the code.
I used Heroku and found it pretty easy to understand. The web interface is pretty impressing. Neat.

So I started with my code. Used OAuth since app required ‘friends_birthday’ and ‘offline_access’ permissions from user to authenticate the app to begin with. If this was done, the platform was set. Then there was just the need to write a few tens of lines of code to get through with the app.

The problem I faced was, ‘offline_access’ falls under Extended Permissions category and ‘friends_birthday’ is under User and Friends Permissions category. I tried to get ‘offline_access’. It worked pretty well. The problem was with getting ‘friends_birthday’ from User and Friends Permission. There were a few things I tried doing.

I tried to get the permissions by passing two calls (Only a part of the code is shown here)

facebook.auth.createToken()
#Show login window
facebook.login()

#Log in to the window and then press enter
print "After you log in, press enter"
raw_input()

facebook.request_extended_permission('offline_access')
raw_input()

facebook.request_basic_permission('friends_birthday')
raw_input()

But this did not work.

I posted on stackoverflow about this. Immediately I got a response suggesting me to ask for all the permissions in the single call. It seemed right because I came across fb:prompt-permission page where they have clearly passed various permissions like email, publish_stream, offline_access, create_event in the same category, separated by commas.

So I tried doing that as well. A part of the code was modified to

facebook.auth.createToken()
#Show login window
facebook.login()

#Log in to the window and then press enter
print "After you log in, press enter"
raw_input()

facebook.request_extended_permission('offline_access', 'friends_birthday')
raw_input()

But, even then it did not work. Ultimately I got to know that Facebook has always been in process of making changes with the API and the pyfacebook that I was using was really a very old python facebook interface. *sigh*

There are many things Facebook is changing and I don’t know and don’t understand why.
One important thing I would like to mention is the change in the Authenticaion policy that Facebook has implemented lately is

I tried to find for some other Facebook API which used Python. Did find a few complicated things for which I had to go through some really complex Django part. Doing that for just getting through a simple and small thing did not make me any happier.

So, for now this code has been pushed to my GitHub account. Have a look and I would be glad if you can help.

Now, I am onto another project which deals with Twitter lists and Trello. As I am done with it, will blog about it.
Till then, adios!
Keep coming back!
Cheers!

 
Leave a comment

Posted by on June 14, 2012 in Python

 

Tags: , , ,

WakeUp!

Python Script for an alarm

Today I wanted to get up at 6am to reach college, at time! Submissions, you see! 😉

So wanted to keep alarm for that. But my phone’s alarm was not working. I would have told my parents to wake me up, but, they are out of town. Could have told my neighbour, but then waking them up at 2am just to ask them to wake you up at 6am doesn’t seem good, does it? (Agar bell maarta itni raat ko toh mujhe vaise hi maar dete vo 😛 ) So tried to keep an alarm in my laptop using sudo apt-get install toshutils. But that feature was not available for my laptop. *sigh*

Asked 2 friends to wake me up. But then, couldn’t rely on them. 😛 (Even they are humans! And friends ko bhool jaaneki bimaari hoti hai 😛 )

Asked Jay what to do. He said “Python script likh daal for alarm for the laptop” 😀

So I wrote this python script for alarm. You just need to give time, in 24 hour format, for when you want your alarm to ring, for eg: if you want alarm to ring at 6am, you type 0600
After doing that, go to sleep, peacefully! 🙂
At 6am, banshee player will be played.

The python script that I wrote is

import os
import time
timer=raw_input("Alarm should ring at: ")
hour=timer[:2]
minute=timer[2:]
def run():
    t=time.ctime()
    currenttime_hour=t[11:13]
    currenttime_minute=t[14:16]
    if currenttime_hour==hour and currenttime_minute==minute:
        os.system('banshee --play &')
        time.sleep(4)
        os.system('banshee --play')
    else:
        time.sleep(60)
        run()
def main():
    run()
if __name__== '__main__':
    main()

It took me a lot of time for testing this code because Banshee player does not play in a go. You have to give ‘banshee –play’ again for it to play. Realizing this part took almost an hour. 😦
Nevertheless, we got to know something about banshee player! 😀
Will put up the code at Github as I get time.
So, adios people, going to sleep now, have to get up at 6, remember? 😉

 
2 Comments

Posted by on April 8, 2012 in Python

 

Tags: ,

TedTalksDownloader

A friend posted on twitter that he wanted to download all the TED Talks. He could not find a way to do that. So I thought of writing a python script which downloads all the TED Talk videos.

For the ones who want to know how to do that:

I read about BeautifulSoup, urllib, urllib2.
Wrote an algorithm.
Made the CLI.

The code is:

import urllib
from BeautifulSoup import BeautifulSoup

url = "file:///home/kunj/Downloads/Python-2.7.2/ted.html"
x=urllib.urlopen(url).read()
soup=BeautifulSoup(x)

hrefs = soup.findAll( "a" ,{ "target" :True, "href" :True, "style" :True})

for links in hrefs:
    m= str(links.contents)
    link1= m[3:-2]
    page=urllib.urlopen(link1).read()
    soup1=BeautifulSoup(page)
    vid_link = soup1.find("meta",{"property":"og:video","content":True})["content"]
    print vid_link
    filename = vid_link.split("/")[-1]
    urllib.urlretrieve(vid_link,filename)

This code can be further shortened but then I am a lazy fellow 😛 😉

This is my second GitHub repository.

PS: There are total 1048 Ted Talk videos 😀 So run the script and be patient.
PPS: Recommended only if you have an unlimited internet plan or your dad has a lot of money. 😀 😉 No offence.

 
1 Comment

Posted by on March 11, 2012 in Python

 

Tags: , , ,

My first app “NoteIt”

Hey!

This post is all about the first app that I made using python language. (Finally, I made an app! 😀 )
Name of this app is ‘ NoteIt ‘

Some of us do have a habit of keeping a diary, or I would say just keeping a note (read, memo) of stuff happening in day to day life. Some of us do maintain a record of daily expenses, also! But then, since we are humans( read, lethargic 😛 ) we tend to postpone and think “Kal likh loonga” 😀
The main problem is we don’t feel like taking a notebook/dairy and then find a pen and note the things. We are busy people you see! 😉

So I made an app which is an editor, where  you can write stuff you want and then click save. It automatically saves the file with today’s date and time as the file name. So at the end of the month you will have 30/31 files(that is, if you make one everyday) with their names as dates and time when you created that file.

Easy to use and awesome for keeping record of stuff. 🙂

Screen shots of the app:

Type in the text and save the file.

It gets saved with the name as date and time when you saved it. Done! 🙂

For the ones who want to know how did I do it:

To start with, I made a CLI (command-line interface) and got done with the basic things that this app would do.
The basic CLI code of this app is:


import datetime
now = datetime.datetime.now()
now1=str(now)[:19]
f=open(now1+'.txt','w')
s=""
while s!='quit'+"\n":
s=raw_input()
s+="\n"
f.write(s)
f.close()

Then I thought of making GUI so that people find it easy and comfortable to use.
I asked people of what GUI platform do I use. I got a lot of options. Then I tried to skim through a few and found wxPython to be the one which suited all my requirements. I learnt how to use wxPython and then made a basic GUI for NoteIt.

The actual GUI code for this app is:

 


import wx
import datetime
class Example(wx.Frame):
    
    def __init__(self, parent, title):
        super(Example, self).__init__(parent, title=title,
            size=(260, 180))

        self.InitUI()
        self.Centre()
        self.Show()

    def InitUI(self):

        menubar = wx.MenuBar()

        fileMenu = wx.Menu()
        self.SetMenuBar(menubar)

        nmi = wx.MenuItem(fileMenu, wx.ID_NEW, '&New\tCtrl+N')
        fileMenu.AppendItem(nmi)
        smi = wx.MenuItem(fileMenu, wx.ID_SAVE, '&Save\tCtrl+S')
        fileMenu.AppendItem(smi)
        qmi = wx.MenuItem(fileMenu, wx.ID_EXIT, '&Quit\tCtrl+W')
        fileMenu.AppendItem(qmi)

        self.Bind(wx.EVT_MENU, self.new, nmi)
        self.Bind(wx.EVT_MENU, self.OnQuit, qmi)
        self.Bind(wx.EVT_MENU, self.savetext, smi)

        menubar.Append(fileMenu, '&File')
        self.SetMenuBar(menubar)
        self.tv=wx.TextCtrl(self, style=wx.TE_MULTILINE)
    def new(self, e):
        self.tv.Clear()
    def savetext(self,e):
        s=self.tv.GetValue()
        if s:
            now = datetime.datetime.now()
            now1=str(now)[:19]
            f=open(now1+'.txt','w')
            f.write(s)
    def OnQuit(self, e):
        self.Close()

if __name__ == '__main__':

    Example(None, title='')
    app = wx.App()
    app.MainLoop()

Making a GitHub repository, too.
A basic app, though, feels wonderful to make one.
This was done because of my mentor-friend Jay Rambhia. Had he not inspired me to get going with the idea, I would have never implemented it.

This was my first app, what’s yours?

 
4 Comments

Posted by on February 23, 2012 in Python

 

Tags: ,