Posts Tagged ‘script’

Access MSSQL with Python!!!

April 24th, 2010

I started using Python a while ago and one of the things I need to do is to be able to connect to Microsoft SQL server from Linux servers… Its suprising that only a few projects are alive for it… One of them is the commercial MxODBC from eGenix, while the other is called pymssql… I gave pymssql a try and it worked almost at once on my Ubuntu machine…

First I installed the prerequisites: freetds and the python headers:

aptitude install python2.5-dev freetds-dev

There might be some other packages that are needed but that was the only ones I needed to install…. Then I decompressed the latest release from pymssql at sourceforge and installed:

NOTE : for me the latest package did not work so I used pymssql-0.8.0

tar -xvzf pymssql-0.8.0.tar.gz
cd pymssql-0.8.0
python setup.py install

Now you can test it out by creating your own test script and it is as follows :

import pymssql
conn = pymssql.connect(host='<hostname>', user='<username>', password='<passwd>', database='<dbname>')
cur = conn.cursor()
cur.execute('SELECT * FROM tbABCD')
row = cur.fetchone()
while row:
 print "ID=%s, Name=%s, Address=%s" % (row[0], row[1], row[2])
 row = cur.fetchone()
conn.close()

The script ran perfectly…
For more information on pymssql you can view its documentation here

Tags: , , , , ,
Posted in Techi Gyaan | Comments (0)

Set Twitter Tweet as status message in Pidgin!!!

March 8th, 2009

While browsing the net you would have heard about Twitter. Twitter is basically another name for micro blogging. In twitter you just have to sign up, then update a single line(“what you are currently doing?”) whenever you can. Its just so simple, no fuss about making a profile or writing down about the movies you like and blah blah…

A lot of us also use different instant messaging clients on a day to day basis. Pidgin is one of these clients that runs on a number of platforms, including Windows, Linux, and other UNIX operating systems. The advantage of using pidgin is that instead of running GTalk, Yahoo Messenger and MSN Messenger separately, you can have everything-in-one.

Now Pidigin allows us to set a status message and that is exactly what we do in twitter. So here’s what we can do.. update twitter status alone and then get Pidgin to retrieve and set the tweet as the IM status automatically.. This can be done with a help of a code found at Google Codes.

To do this follow the steps below. (I am taking it that you have already got Pidgin installed on your system.)

1. Firstly we need to install Perl. Most Linux distros come with Perl preinstalled. Windows users, download and install Perl from here.

2. Take a Command Prompt/Terminal and enter: perl -MCPAN -e shell (Linux users, would have to add a sudo before this). In the CPAN prompt that you got now, enter install XML::XPath and hit enter. This will download and install the Xpath module from Internet.

3. Now launch Pidgin. Go to Help -> About and scroll to the bottom of the page. You should see a line : Perl: Enabled.

4. Download twitter.pl from http://code.google.com/p/pidgin-twitterstatus/downloads/list and put it into your Pidgin plugins folder. Usually the plugins folder is C:\Program Files\pidgin\plugins for Windows users and ~/.purple/plugins for Linux users.

5. Now restart Pidgin and go to Tools -> Plugins. There will be a new plugin named Twitter Status Feed. Enable it and click on Configure Plugin. Enter your twitter user name in the dialog box that pops up.

6. Now in the drop down box where we select our status choose Twitter.

Note : sometimes in Linux copying the twitter.pl in “~/.purple/plugins” may not work. If you have root privilege then copy the script in “/usr/lib/purple-2/“.

Please leave comments if you face problems. :)

Tags: , , , , , , ,
Posted in Fun Gyaan, Techi Gyaan | Comments (0)

Python Script for Cricket Score!!!

February 1st, 2009

Here’s a script that i wrote to get the latest cricket score without visiting the site. It’s a python script that makes use of rediff scores.

import urllib
class Score(object):
SCORE_URL = ""
def __init__(self):
self.vars = {} # dictionary variables read from the score file
def refresh(self):
"""Refresh the score"""
url_opener = urllib.URLopener()
url = url_opener.open(self.SCORE_URL)
self.vars = {}
for stmt in url.read().split('\n'):
stmt = stmt.strip()
if stmt == "": continue
var, val = stmt.split('=')
self.vars[var] = val
url.close()
return self.vars
def render(self):
"""Return the rendered text"""
return "%(l1)s\n%(message)s\n%(tagline)s\n" % self.vars
class RediffScore(Score):
SCORE_URL = "http://livechat.rediff.com:80/sports/score/score.txt"
def getMatchName(self):
return self.vars['tagline']
def getScore(self):
return self.vars['l2']
def getTeams(self):
return self.vars['l1']
def getDate(self):
return self.vars['date']
def getMessage(self):
return self.vars['message']
def getInterval(self):
return self.vars['interval']
if __name__ == '__main__':
print 'Current score'
sc = RediffScore()
sc.refresh()
print sc.render()

Copy the script in a text editor and save it as score.py in your home folder.
Now run the script by :
animesh@animesh-laptop:~$ python score.py

The output should be somewhat like :
Current score
Australia vs New Zealand
Australia 181-10 (48.4)
S Tait(9)

Note: There is a problem with the script here.. i think when i copy pasted the file the indentation got messed up..
So here’s a link from where you can download it..
http://www.box.net/shared/uu203jgou0

If you face any problem please comment. :)

Tags: , , , ,
Posted in Techi Gyaan | Comments (9)

Compress Pictures In Linux(Modified)!!!

February 1st, 2009

I got the script to move the new compressed images into a new folder that would be located in your home folder.

As i explained before you need imagemagick installed on your system to run the script.
In ubuntu it can be installed by typing in the terminal sudo apt-get install imagemagick

Heres the script :
#!/bin/bash
# A script to resize images.
readonly OUT_FMT="_new.jpg"
if [ "$#" -eq 0 ] ; then
echo "A script to resize images "
echo "How to use : $(basename $0) add_of_image1 add_of_image2 ..."
exit 1
fi
read -p 'Enter name of Folder where compressed images would be located (would be made in your home folder) : ' fname
mkdir ~/$fname
for pic
do
# New filename
out_name="${pic%.*}$OUT_FMT"
if [ -e "$out_name" ] ; then
echo "Output file $out_name exists, Not resizing!!!"
elif [ "${pic#*$OUT_FMT}" == "" ] ; then
echo "$pic already resized!!!"
elif [ ! -r "$pic" ] ; then
echo "Error : could not access $pic !!!"
else
echo -n "$pic -> $out_name"
convert -quality 80 -resize 800x600 "$pic" "$out_name"
if [ ! -e "$out_name" ] ; then
echo " Error .. No output file!!!"
else
echo " ($(du -h "$out_name" | cut -f1))"
fi
fi
mv "$out_name" ./$fname/
done

I also didn’t explain the script properly in the previous post so i’ll that now.
Firstly the script should be copied from here and pasted in a text editor. Then save it as resize.sh in your home folder(you can save it any where you wish but then how you call the scritp would change thats why its easier to save it in the home folder :) ). Now open a terminal type:
animesh@animesh-laptop:~$ sudo chmod +x resize.sh

after this do animesh@animesh-laptop:~$ ./resize.sh aaa.jpg
This would ask a for a folder where new image should be placed
Enter name of Folder where compressed images would be located (would be made in your home folder) :
Specify the name of a new folder where you wanna save the image.
The new compressed image would be saved in the folder you specified with the name aaa_new.jpg

You can also convert all pictures of a directory at once by
animesh@animesh-laptop:~$ ./resize.sh Desktop/linux09/*.JPG
This to would ask for a folder where you would like to save the new compressed images.

Now in the line convert -quality 80 -resize 800×600 "$pic" "$out_name" in script the value after -quality is the argument specifies the amount of JPEG/MIFF/PNG compression level, the lower the value(meaning 60 or 50) the higher is the compression(change it according to what you want). The -resize is used to resize an image, the argument following resize(i.e. 800×600) can be changed to what you want.
Leave a comment if you face any problems. :)

Tags: , , , , ,
Posted in Techi Gyaan | Comments (7)

Compress Pictures In Linux!!!

January 26th, 2009

I have made a script to compress a set of images so that they can be mailed as images taken form a digicam are normally of 10 or 15MB and to upload a set of images is a pain in the ***. :)

Firstly you need to install package Imagemagick…
In ubuntu you can install by sudo apt-get install imagemagick

Now just copy the attached in an editor:
#!/bin/bash
# A script to resize images.
readonly OUT_FMT="_new.jpg"
if [ "$#" -eq 0 ] ; then
echo "A script to resize images "
echo "How to use : $(basename $0) add_of_image1 add_of_image2 ..."
exit 1
fi
for pic
do
# New filename
out_name="${pic%.*}$OUT_FMT"
if [ -e "$out_name" ] ; then
echo "Output file $out_name exists, Not resizing!!!"
elif [ "${pic#*$OUT_FMT}" == "" ] ; then
echo "$pic already resized!!!"
elif [ ! -r "$pic" ] ; then
echo "Error : could not access $pic !!!"
else
echo -n "$pic -> $out_name"
convert -quality 80 -resize 800x600 "$pic" "$out_name"
if [ ! -e "$out_name" ] ; then
echo " Error .. No output file!!!"
else
echo " ($(du -h "$out_name" | cut -f1))"
fi
fi
done

Now save it as resize.sh in your home folder.

Now in the terminal type
animesh@animesh-laptop:~$ sudo chmod +x resize.sh

after this do animesh@animesh-laptop:~$ ./resize.sh aaa.jpg
This would return a file aaa_new.jpg

You can also convert all pictures of a directory at once by
animesh@animesh-laptop:~$ ./resize.sh Desktop/linux09/*.JPG

The only problem with the script is that all the resized pictures would be in the same folder as original pictures. I am not able to move it in a new folder don’t know why. If you face any problem please leave a comment or if you know the solution to my problem please lemme know… :)
This post has being modified.

Tags: , , , , ,
Posted in Techi Gyaan | Comments (2)