Posts

Showing posts from August, 2008

Regular Expression in Python

Image
'How to learn regular expression in Python?' - a very common question from the beginners or Python newbies. Well... if you can use regular expression in Perl or PHP, then it's not very different in Python. And if you are completely new to regular expression and want to learn it, then you can follow the following guideline: [Update on Oct 30, 2010]: I think you should start with this video on regular expression . At first, you should be to read Regular Expression HowTo by A.M. Kuchling. Read it carefully and read it at least twice :) So... now you have finished reading Regular Expression How To and still not very confident. Don't worry. It's time to dive into regular expression. Just go through Chapter 7 Regular Expressions of Dive Into Python. Don't forget to code each and every example. There are some nice case studies. Those will help (it was really helpful in my case). And now it's time for you to check details about the re module from python

Looking for Python Jobs? Check Job Boards

Sometime I see people wondering about the Job market of Python. Now days kids are more interested about their career :) ... So I have decided to collect Python Job board addresses and post the links here. Note that this page will be updated whenever I find any new link. You can also help me by posting comments. Python Job Board : This is a community maintained page. It's frequently updated. Also there is a volunteer opportunity page.

Is Python only for web programming?

Image
As a Python enthusiast/advocate I always have to face some common questions about Python from friends, juniors, colleagues... One of those is, "Is Python used only for web programming?" So I think I better write the answer here :) And the answer is NO. Python is used in lots of places: Web Programming: Python is heavily used in Web Programming. There is a great CMS named Plone, developed in Python. There are also some good web frameworks. Also the Google Application Engine is developed using Python Desktop Computing: Python can be used to develop software for desktop. There are multiple ways to develop GUI for your python software. You can use Tkinter, wxPython etc. Distributed Computing: It's another field where Python is used effectively. Once I heard about the RIVER project (I forgot the link) which is developed using Python. Mobile Phone Programming: Yes, Python can be used in your mobile phones. Nokia developed PyS60 to run in Symbian S60 (series 60) phones. There is

PyS60 - download file using urlretrieve

The following function written in Python downloads a remote file to mobile phone. import urllib def download_file(): remoteFileName = u"http://example.com/a.txt" # set the url here localFileName = u"c:\\items.txt" urllib.urlretrieve(remoteFileName, localFileName) download_file() The code was used in my 'DSE Price Tracker' application in order to download the list of companies.

Create selection list from a file in PyS60

Image
Let's come back to the project - DSE price tracker. The next step was to load all the company names from a file (stored in the mobile phone memory) and display in the selection list. The file contains the company names, one per line. So I wrote the following code: import appuifw def get_data_from_file(): fp = open('c:\\names.txt', 'r') li = fp.readlines() fp.close() compList = [] for item in li: item = item.decode("utf-8") compList.append(item) return compList L = get_data_from_file() index = appuifw.selection_list(choices=L , search_field=1) Yes, it works. Note that you must decode each line to unicode (utf-8). ( item = item.decode("utf-8") ) But wait, what are those small boxes beside each name? I guess it's the newline character ('\n') after each line. So I need to replace '\n' with a blank ''. Here is the modified get_data_from_file() def get_data_from_file(): fp = open

Designing Class in Python - Pythonic way

I found two very interesting discussions on writing / designing class in Python. If you still in search of the beauty of object oriented programming in Python, check the following links: Is my thinking Pythonic? Handling Property and internal ('__') attribute inheritance and creation Enjoy Python :)

Create a selection list in PyS60

Image
Creating a selection list for my software - 'DSE Stock Tracker' was very important task. It will show the list of companies of Dhaka Stock Exchange. So for demo I tried to create a list of four companies. Here is the code: import appuifw L = ['DHAKABANK', 'AIMS1STMF', 'EXIMBANK', 'TRUSTBANK'] index = appuifw.selection_list(choices=L , search_field=1) I tried to run the program but got the following error: SymbianError: [Errno -6] KErrArgument Oh, I forgot to add the 'u' (u for unicode) before each string. So I changed my code and it works. import appuifw L = [u'DHAKABANK', u'AIMS1STMF', u'EXIMBANK', u'TRUSTBANK'] index = appuifw.selection_list(choices=L , search_field=1) Now, as there are hundreds of companies, it's not wise to write every name in the code. So the next to-do is load the list from a file.

'DSE Stock Tracker' in First BdOSN Code Sprint

I am back from the first BdOSN code sprint - the first of it's kind in our country - Bangladesh. It was held at SUST (Shah Jalal University of Science & Technology), Sylhet - one of the most beautiful places in Bangladesh. I did my graduation from this university :) There I lead a project named 'DSE Stock Tracker'. It shows real-time (4 minutes delay) stock prices from Dhaka Stock Exchange to your mobile. The mobile client was developed in Python (PyS60). The whole system has three parts: A spider that harvests data from dse website and stores in mysql database An API for mobile application/client A mobile client All the parts are developed using Python. I shall write my experience, challenges faced and how I solved them in next several posts. Stay tuned!

How to send email from a Python script

Today I needed to send email from a Python script. As always I searched Google and found the following script that fits to my need. import smtplib SERVER = "localhost" FROM = "sender@example.com" TO = ["user@example.com"] # must be a list SUBJECT = "Hello!" TEXT = "This message was sent with Python's smtplib." # Prepare actual message message = """\ From: %s To: %s Subject: %s %s """ % (FROM, ", ".join(TO), SUBJECT, TEXT) # Send the mail server = smtplib.SMTP(SERVER) server.sendmail(FROM, TO, message) server.quit() [Source: http://effbot.org/pyfaq/how-do-i-send-mail-from-a-python-script.htm] But when I tried to run the program, I got the following error message: Traceback (most recent call last): File "email.py", line 1, in import smtplib File "/usr/lib/python2.5/smtplib.py", line 46, in import email.Utils File "/home/dsebd/email.

Create thumbnail by resizing image in Python

Sometimes we need to create thumbnails of certain size from image. In this post I show you how to create thumbnail by resizing image in Python. Here is the Python code for creating thumbnail / resizing an image: import Image def generate_and_save_thumbnail(imageFile, h, w, ext):     image = Image.open(imageFile)     image = image.resize((w, h), Image.ANTIALIAS)     outFileLocation = "./images/"     outFileName = "thumb"     image.save(outFileLocation + outFileName + ext) # set the image file name here myImageFile = "myfile.jpg" # set the image file extension extension = ".jpg" # set height h = 200 # set width w = 200 generate_and_save_thumbnail(myImageFile, h, w, extension) The second argument of image.resize() is the filter. You have several options there: Image.ANTIALIAS Image.BICUBIC Image.BILINEAR Image.NEAREST Each uses different algorithm. For downsampling (reducing the size) you can use ANTIALIAS. Other filers may increase the size. You