Feeds:
Posts
Comments

Archive for the ‘Scripts’ Category

Atlast i came up with a real rhythmbox plugin for setting the status message in pidgin as the current song title playing in Rhythmbox music player .I wrote it a few month before and was using it. Thought of sharing it before but didnt get time. Actually i forgot about my blog when i started microblogging. From now onwards i will be updating this blog regularly.( this is what i wanted to do always 😀 )

Download the plugin from here : http://ragsagar.freehostia.com/rb-nowplaying.tar.gz
To install it.
1) Extract the tarball.
2) Copy it to ~/.gnome2/rhythmbox/plugins/ (if no such directories exists, create them)
3) Open rhythmbox. Edit -> Plugins
4) Check ‘Now Playing Pidgin’

That’s it. If you are facing any problems do comment.

Advertisements

Read Full Post »

Just thought of sharing the code i written to learn linked list implementation the day before my data structures model practical exam.

/*
 *      linkedlist.c
 *      
 *      Copyright 2009 Rag Sagar.V <ragsagar@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */


#include <stdio.h>
#include <stdlib.h>

typedef struct list
{
	int data;
	struct list *next;
}LIST;
LIST *ptr,*temp,*start=NULL;
void insert_after(int ,int );
void remove_item(int );
void display(void);
int count=0;
int main()
{
	int item,opt,dat;
	system("clear");
	ptr=NULL;
	/* printf("%d",sizeof(LIST)); */
	do
	{
		printf("\n########## MENU ##########\n");
		printf("1.Insert\n2.Remove\n3.Display\n4.Exit\n");
		printf("Enter your option : ");
		scanf("%d",&opt);
	 	switch(opt)
	 	{
	 		case 1:
	 		printf("Enter the data to insert ");
	 		scanf("%d",&item);
	 		if(count==0)
	 		{
	 			ptr = (LIST *)malloc(sizeof(LIST));
	 			ptr->next = NULL;
	 			ptr->data = item;
	 			start = ptr;
			}
			else
			{
				printf("Enter the item after which you have to insert new element : ");
				scanf("%d",&dat);
				insert_after(dat,item);
			}
			count++;
				break;
	 		case 2: 
	 		if(count==0)
	 		{
	 			printf("\nList is empty\n"); break; 
			}
	 		printf("Enter the item to remove : ");
	 		scanf("%d",&item);
	 		remove_item(item);
	 		count--;
	 		break;
	 		case 3: 
	 		if(count==0)
	 		{
	 			printf("\nList is empty\n");
	 			break;
	 		}
	 		else
	 		{
	 			printf("List elements are \n"); display(); 
			}
			break;
	 		case 4: break;
		}
	}while(opt!=4);
	return 0;
}
void insert_after(int data, int item)
{	
	LIST *tmp;
	temp=(LIST *)malloc(sizeof(LIST));
	temp->data=item;
	ptr=start;
	
	while(ptr!=NULL)
	{
		if(ptr->data==data)
		{
			tmp=ptr->next;
			ptr->next=temp;
			temp->next=tmp;
			break;
		}
	ptr=ptr->next;	
	}
		
}	

void remove_item(int item)
{
	ptr=start;
	if(ptr->data == item)
	{
		start = ptr->next;
		free(ptr);
	}
	while(ptr->next!=NULL)
	{
		if((ptr->next)->data==item)
		{
			temp=ptr->next;
			ptr->next=(ptr->next)->next;
			free(temp);
			break;
		}
		ptr=ptr->next;
	}
}
			
void display()
{
	ptr = start;
	while(ptr!=NULL)
	{
		printf("%d -> ",ptr->data);
		ptr=ptr->next;
	}
}			

Read Full Post »

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
#https://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

output

Read Full Post »

My first post in tuxopia is a how-to for writing basic IRC bot in python.Thought of sharing the link here 🙂

Read Full Post »

Here is a script to provide weather information of a city with the help of google api.
#!/usr/bin/python
#Rag Sagar 
#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],"  \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

Weather utility

Thanks to rohit

Read Full Post »

Iam back with another shell script written for my sis as part of her assignment. This time what i wanted to do is to count the unique words in a file and to print it in alphabetical order. For that i iterated through the words in the given file using for loop, filtered out the unique words using sort -u and counted it using wc -l.

#!/usr/bin/bash
echo Enter the filename
read fn

for WORD in $(cat $fn)
do
	echo "$WORD"
done | sort -u | wc -l	

 for WORD in $(cat $fn)
 do
  echo "$WORD "
 done | sort -u

Hope this will help someone to do his/her assignment 😀

Read Full Post »

Ok here is a script to cipher text contents in a file. It will shift the letters to the fifth letter to its right, ie if a is the letter, it will change it to f.

echo Enter the filename
read fn

while read -n 1 letter
do
  case $letter in
    '')
      echo
    ;;
    ' ')
      printf ' '
    ;;
	'.')
	  printf '.'
	;;
    *)
      asci=$(printf %d \'$letter)
      ((asci+=5))
      fwd_letter=$(printf "\\$(printf "%03o" $asci)")
      printf "$fwd_letter"
    ;;
  esac
done < $fn

You can decipher it by just replacing the + in 18th line to
Note : This is a assignment my sis got from IGNOU. I hope this will help someone to do his/her assignment 🙂

Read Full Post »

« Newer Posts - Older Posts »

%d bloggers like this: