About Infix and Postfix
In an expression if the operators are placed between the operands, it is known as infix notation ( eg A+B) . On the other hand if the operators are placed after the operands then the expression is in postfix notation .( eg AB+)
Infix Notation Postfix Notation
(A-C)*B AC-B*
A+(B*C) ABC*+
(A+B)/(C-D) AB+CD-/
Code
#!/usr/bin/python
#http://ragsagar.wordpress.com
postfix = []
temp = []
operator = -10
operand = -20
leftparentheses = -30
rightparentheses = -40
empty = -50
def precedence(s):
if s is '(':
return 0
elif s is '+' or '-':
return 1
elif s is '*' or '/' or '%':
return 2
else:
return 99
def typeof(s):
if s is '(':
return leftparentheses
elif s is ')':
return rightparentheses
elif s is '+' or s is '-' or s is '*' or s is '%' or s is '/':
return operator
elif s is ' ':
return empty
else :
return operand
infix = raw_input("Enter the infix notation : ")
for i in infix :
type = typeof(i)
if type is leftparentheses :
temp.append(i)
elif type is rightparentheses :
next = temp.pop()
while next is not '(':
postfix.append(next)
next = temp.pop()
elif type is operand:
postfix.append(i)
elif type is operator:
p = precedence(i)
while len(temp) is not 0 and p <= precedence(temp[-1]) :
postfix.append(temp.pop())
temp.append(i)
elif type is empty:
continue
while len(temp) > 0 :
postfix.append(temp.pop())
print "It's postfix notation is ",''.join(postfix)
Code Explanation
Above code converts infix notation in variable infix into postfix notation and stores in postfix list. This algorithm makes use of list temp to hold operators and left parantheses in the infix notation. The postfix list will be constructed from left to right using operands from infix and operators which are removed from temp.

output
Posted in Uncategorized | Tagged conversion, infix, postfix, Python, stack | 3 Comments »
My first post in tuxopia is a how-to for writing basic IRC bot in python.Thought of sharing the link here
Posted in Howtos | Tagged bot, how-to, irc, Python, socket | 2 Comments »
Here is a script to provide weather information of a city with the help of google api.
#!/usr/bin/python
#Rag Sagar <ragsagar @gmail.com>
#Free to copy, modify and redistribute
import sys
from urllib2 import urlopen, URLError
from xml.sax import make_parser
from xml.sax.handler import ContentHandler
class WeatherFinder(ContentHandler):
"""the handler class"""
def __init__(self):
self.weather_list = []
self.go_on = False
def startElement(self, tag, attrs):
"handles the opening tags"
if tag == 'current_conditions':
self.go_on = True
if self.go_on : # so that only values inside current_conditions tag is appended
if tag == 'condition' :
self.weather_list.append('Condition: '+attrs.get('data',""))
elif tag == 'humidity' :
self.weather_list.append(attrs.get('data',""))
elif tag == 'temp_c' :
self.weather_list.append('Temperature:'+attrs.get('data',"")+'C')
elif tag == 'wind_condition' :
self.weather_list.append(attrs.get('data',""))
return
def endElement(self, tag):
"handles closing tags"
if tag == 'current_conditions':
self.go_on = False
if len(sys.argv) is not 2 :
print "Syntax : ",sys.argv[0]," <city> \n"
sys.exit(1)
else :
city = sys.argv[1]
url = 'http://www.google.com/ig/api?weather='+city
parser = make_parser()
obj = WeatherFinder()
parser.setContentHandler(obj)
try :
parser.parse(urlopen(url))
except URLError:
print "\n\33[33m Unable to fetch data..\33[0m\n"
sys.exit(1)
if obj.weather_list == [] :
print "\n\33[33m Invalid city name, Try another.. \33[0m\n "
else :
print "\33[33mWeather Conditions of",city,"\33[36m"
for i in obj.weather_list:
print i
print "\33[0m"
Checkout its screenshot

Weather utility
Thanks to rohit
Posted in My_Works | Tagged google api, Python, urllib2, weather, xml | 2 Comments »