Where and when I would use if( __name__ == “__main__”):

Hi,

I’m Confused. pls, Clarify on this

I clearly understood the concept of above statement, but I don’t know where the situation which I can use that __name__ statement effectively. What I have understood on the statement is  python interpreter has some variables by default like that it also have a variable called __name__. During the execution of python files it will assign values to that variable. If it is an stand-alone program execution then the value of __name__ would be “__main__” if python is imported then the value of __name__ would be the file name of the python file.
Example:

#Filename: foo1.py
print “Value of __name__ Variable:  ”+__name__

if I execute the above file.
Execution: python foo1.py

Output is:

Value of __name__ Variable:  __main__

Whereas if I execute the file as module by importing (i.e.)

#Filename: foo2.py
import  foo1

print “I am foo2”

Execution: python foo2.py

Output:

Value of __name__Variable: __foo1.py
I am foo2

Now, my question is,  if(__name__ ==”__main__”): statement used only to identify whether it is main program or stand-alone program. Isn’t it? Am I correct? Pls clarify more……

Regards,
Balakrishnan V.

 

PYGTK Simple Tasks

Hi to all,

A long ago Raji akka teached pygtk to us, also i learned pygtk with eager at the time. But after that i didn’t worked it out regularly, fortunately i had to do some simple task in pygtk, I started it from scratch finally i did one Hello World program and Simple currency converter. May be these programs are simple, but it shows i too know pygtk 😉

1. Hello World program

#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk

class HelloWorld:
    def hello(self, widget, data=None):
        print "Hello World"
    def delete_event(self, widget, event, data=None):
        print "delete event occurred"
        return False
    def destroy(self, widget, data=None):
        print "Completely quit"
        gtk.main_quit()
    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("delete_event", self.delete_event)
        self.window.connect("destroy", self.destroy)
        self.window.set_border_width(50)
        self.hobox = gtk.HBox(False, 0)
        self.window.add(self.hobox)
        self.b = gtk.Button("Hello World")
        self.b.connect("clicked", self.hello)
        self.hobox.pack_start(self.b, True, False, 0)
        self.b.show()
        
        self.a=gtk.Button("    Exit   ")
        self.a.connect("clicked", self.destroy)
        self.hobox.pack_start(self.a, True, False, 0)
        self.a.show()
        
        self.hobox.show()
        self.window.show()
    def main(self):
        gtk.main()
if __name__ == "__main__":
    hello = HelloWorld()
    hello.main()

Here is the screen shot:

 

To get the output…………?

* Copy and paste the code in text editor.

* save the filename with .py extenstion

* run the file with the following command in terminal python filename.py

* You will get a window like above 🙂

2. Simple Currency Converter:

import pygtk
pygtk.require('2.0')
import gtk
class Conversion:
    def convert(self,widget):
        self.t1=self.text1.get_text()
        self.val=int(self.t1)
        self.vali=self.val*44.75
        self.text2.set_text(str(self.vali))
        self.vale=self.val*0.68
        self.text3.set_text(str(self.vale))
    def delete_event(self, widget, event, data=None):
        print "delete event occurred"
        return False
    def destroy(self, widget, data=None):
        print "Completely quit"
        gtk.main_quit()
    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("delete_event", self.delete_event)
        self.window.connect("destroy", self.destroy)
        self.window.set_border_width(60)
        table=gtk.Table(4, 2, True)
        self.window.add(table)
        self.label1=gtk.Label("Enter USD Amount: ")
        table.attach(self.label1, 0, 1, 0, 1)
        self.label1.show()
        self.text1=gtk.Entry(0)
        table.attach(self.text1, 1,2,0,1)
        self.text1.show()
        self.label2=gtk.Label("Indian Value Is (INR): ")
        table.attach(self.label2, 0,1,1,2)
        self.label2.show()
        self.text2=gtk.Entry(0)
        table.attach(self.text2, 1,2,1,2)
        self.text2.show()
        self.label3=gtk.Label("Euro Value Is (EUR): ")
        table.attach(self.label3, 0,1,2,3)
        self.label3.show()
        self.text3=gtk.Entry(0)
        table.attach(self.text3, 1,2,2,3)
        self.text3.show()
        self.but1=gtk.Button("Convert")
        self.but1.connect("clicked",self.convert)
        table.attach(self.but1,0,1,3,4)
        self.but1.show()
        self.but2=gtk.Button("Quit")
        self.but2.connect("clicked",self.destroy)
        table.attach(self.but2,1,2,3,4)
        self.but2.show()
        table.show()
        self.window.show()
    def main(self):
        gtk.main()
        
if __name__ == "__main__":
    conversion = Conversion()
    conversion.main()

Screen shot :

To get the output…………?

As mentioned above do it here also.

* Copy and paste the code in text editor.

* save the filename with .py extenstion

* run the file with the following command in terminal python filename.py

* You will get a window like above 🙂

It will help to beginner to learn pygtk.

Thank You

With Regards,
V. Balakrishnan.

User Input as Password in Python

Hi Guys,

Probably, we can know how to get an input from user using raw_input(); Like in some cases we have to get input from user in authentication process at that time we might be a get user password from user. If user enters the password means it should be invisible to others for that there is an option to get the input depending to password.

We have to import a library called getpass With the help of this library we can get the user input as password. Examples are as follows.


import getpass
pw=getpass.getpass("Enter Your Password Here: ")
if (pw=="password"):
print "You are Welcome..."
else:
print "Sorry! Your are not allowed."

Copy this Code and Paste it in any text editor with file extension of .py; If you execute the code means the terminal waits for user input at “Enter Your Password Here: ” here if you type your password it is invisible to you but it has been storing your password into variable name called pw. After then it has been processed with the string for further Statements. If the condition true means True statements executed other wise false statement will executed.

Output:

password inputIn the figure you can see that cursor waits for user input.

bala@bala-desktop:~$ python pass.py
Enter Your Password Here:
You are Welcome.

Hope that these information is useful for you. 🙂

Thank You. 🙂

With Regards,
V. Balakrishnan.

About ChennaiPY meet on 26-02-’11

Hello friends,

Greetings to Everyone.

After long period I had chance to attend the chennaipy monthly meeting. Actually ChennaiPy is a Python group which is conducting in Chennai at every last week of the month. I have decided to attend that meeting so i went to Chennai from Kanchipuram at 1’o clock. I think its too late but i had some works which is very urgent to do. So only i have started on that time. I reached there at 3.45 totally i missed 40 minutes Speech.

When i am entering into the class i saw that Mr. Lakshmi Vyas handling the session with the topic called “Meta Programming”. It is totally new concept for me not only concept that word too. Anyhow i listened it very carefully because it is new concept for me. He explained the class fully in Processional  English, but i cant understand his Hand-writing which was written in that board. After some time he wrote in Capital letters to understand his hand writing.

He explained some programs in live to get clear of that concept. It contains totally 7 programs due to time constraint he can cover upto 5th program. He told that we can cover it on next session but he gave overview of that remaining 2 programs.

From his explanations i got some information
* python Decorators
*Function Returns Function

Also he show a real time project that contains meta programming which was created by himself called “HYDE”.

I Feel that the  Session is very useful and advanced for me…

Thats it.