Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Sunday, April 26, 2009

The intersection of two Python lists

Suppose that you have two lists, x and y. You can list out the items that are common to the two lists.
>>> x = [1,2,3,4,5]
>>> y = [2,6,7,3]
>>> [i for i in y if i in x]
[2, 3]
To see how many items found in the intersection.
>>> len([i for i in y if i in x])
2

Wednesday, April 22, 2009

How to install IDLE in Linux

IDLE is the default editor for Python. However, despite the fact that Python is installed by default in most Linux system, the user should install IDLE manually. These are the commands for installing IDLE.

Debian/Ubuntu

sudo apt-get install idle

Fedora/Red Hat

yum install python-tools

Wednesday, March 25, 2009

4 database abstraction layers for Python

Database abstraction layers for Python includes:

  1. DB-API
    A standard python api and each db creates their own module that uses it.


  2. CouchDB Python
    Python library for working with CouchDB


  3. SQLObject
    Object Relational Manager for providing an object interface to your database, with tables as classes, rows as instances, and columns as attributes.


  4. SQLAlchemy
    Enables classes to be mapped against the database in more than one way.


Tuesday, February 24, 2009

How to sort Python dictionary

To sort a Python dictionary, and append the keys into a list.
>>> x = {'a': 2, 'c': 1, 'b': 3}
>>> sorted(x, key=x.get)
['c', 'a', 'b']

To sort a Python dictionary, and append tuples of the key and value into a list.
>>> x = {'a': 2, 'c': 1, 'b': 3}
>>> [(the_key, x[the_key]) for the_key in sorted(x, key=x.get)]
[('c', 1), ('a', 2), ('b', 3)]

To sort a Python dictionary, and append the keys into a tuple, and the values into a tuple.
>>> x = {'a': 2, 'c': 1, 'b': 3}
>>> [(the_key, x[the_key]) for the_key in sorted(x, key=x.get)]
[('c', 1), ('a', 2), ('b', 3)]
>>> x = {'a': 2, 'c': 1, 'b': 3}
>>> the_keys, the_vals = zip(*[(the_key, x[the_key])
for the_key in sorted(x, key=x.get)])
>>> the_keys
('c', 'a', 'b')
>>> the_vals
(1, 2, 3)

The results from the above examples are in ascending order. To sort in descending order, attach reverse=True at the end of the sorted() function.
>>> x = {'a': 2, 'c': 1, 'b': 3}
>>> sorted(x, key=x.get, reverse=True)
['b', 'a', 'c']

Unzip a Python list

Suppose that you have a list with tuples as the member.
>>> test = [('x',1), ('y',2), ('z',3)]
We can unzip the list by using zip() in conjunction with the * operator.
>>> a, b = zip(*test)
>>> a
('x', 'y', 'z')
>>> b
(1, 2, 3)

Thursday, February 19, 2009

A simple way to time a small bits of Python code

A simple way to time a small bits of Python code is by using the time module. Please look at the example below.
>>> from time import time, sleep
>>> def f(x):
'''
Does nothing. Sleep for x seconds.
'''
sleep(x)

>>> time1 = time(); f(5); print('Run in {0:f} seconds.'
.format(time()-time1))

Run in 5.000000 seconds.

Code break down:


In the above example, we want to measure how long will the function f() run.
  1. Import the time module.
    from time import time, sleep

  2. Get the current time.
    time1 = time()

  3. Run the function, get the current time, print the time elapsed.
    f(5)
    print('Run in {0:f} seconds.'.format(time()-time1))

How to convert two Python lists into a dictionary

To convert two Python lists into a dictionary. First, pair the two list with the zip() function, as key-value pairs. The last step is to use the dict() constructor to build a dictionary directly from the key-value pairs.
>>> x = ['a', 'b', 'c']
>>> y = [1 ,2 ,3]
>>> dict(zip(x,y))
{'a': 1, 'c': 3, 'b': 2}

We can also zip the two lists together, as list of tuples, and use the dict() constructor for the iterator of that list.
>>> x = ['a', 'b', 'c']
>>> y = [1, 2, 3]
>>> dict(iter(list(zip(x, y))))
{'a': 1, 'c': 3, 'b': 2}

Thursday, December 11, 2008

How to extract email from a Web page using Python

To build an email extractor from a Web page is very easy using Python and regular expression (regex).

The first task is to extract the text from the Web page. To extract the text from the Web page, use the Python urlopen function from urllib module.

from urllib import urlopen text = urlopen('http://the.web.url')
Second, define the regular expression to identify the email. Compile it into variable named pattern.
pattern = re.compile(r"[w!#$%&'*+/=?^_`{|}~-]+"
    + r"(?:.[w!#$%&'*+/=?^_`{|}~-]+)*"
    + r"@(?:[a-z0-9](?:[w-]*[w])?.)+"
    + r"(?:[w^d]{2}|"
+ r"com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)b")

Lastly, use the Python re module to search and extract the email, from the text that had been extracted previously.
pattern.findall(text)
It will return a Python list of all emails in that Web page.

This is the full code listing:
import re
from urllib import urlopen
def extractEmail(theUrl):
    text = urlopen(theUrl).read()
    pattern = re.compile(r"[w!#$%&'*+/=?^_`{|}~-]+"
                + r"(?:.[w!#$%&'*+/=?^_`{|}~-]+)*"
                + r"@(?:[a-z0-9](?:[w-]*[w])?.)+"
                + r"(?:[w^d]{2}|"
         + r"com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)b")
    return pattern.findall(text)

To remove duplicates from the list, use set.
return set(pattern.findall(text))
Please use this code wisely. Thank you.

Last update: Monday, May 31, 2010.

Sunday, November 23, 2008

python list in sql query as parameter

Suppose that we have a list [10, 20, 4, 6, 9], and we want to write a sql query to get the data for all elements of the list, like
select * from students where id in (10, 20, 4, 6, 9)
How to do that in Python? Actualy, it is very easy. Below are the list of techniques to do it:

Technique 1

id = [10, 20, 4, 6, 9]
sql = 'select * from studens where id in %s' % str(tuple(id))

Technique 2

id = [10, 20, 4, 6, 9]
sql = 'select * from studens where id in (%s)' % ','.join(map(str, id))

Technique 3

id = [10, 20, 4, 6, 9]
xx = ', '.join(id)
sql = 'select * from students where id in (%s)' % xx

Technique 4

id = [10, 20, 4, 6, 9]
sql = 'select * from studens where id in (' +
         ','.join(map(str, id)) + ')'

Technique 5

id = [10, 20, 4, 6, 9]
sql = 'select * from studens where id in (' +
         ','.join((str(n) for n in id)) + ')'

Technique 6

id = [10, 20, 4, 6, 9]
sql = 'select * from studens where ' +
         ' or '.join(('id = ' + str(n) for n in id))
I prefer the first technique. What about you? Do you have some other techniques to share? If you do, please leave a comment. Thank you.

Note: The above codes were tested using Sqlite3 and Python 2.5.

Wednesday, August 27, 2008

Find max or min in Python dictionary

Suppose that mydict is a dictionary defined by
mydict = {'a': 2, 'c': 5, 'b': 1, 'd': 4}
How to find the key for the max or min value in the items?

Applying
max(mydict)
would produce 'd'. This is wrong. The answer should be 'c'.

Doing it this way, the Python try to find the max in the key part. In this case, 'd' is the max, in alphabetical order.

The correct way is,
max(mydict, key = lambda x: mydict.get(x) )
Similarly, to find the min,
min(mydict, key = lambda x: mydict.get(x) )

Tuesday, August 26, 2008

Sort Python dictionary

Suppose that mydict is a dictionary defined by
mydict = {'a': 2, 'c': 5, 'b': 1, 'd': 4}
Remember that dictionary has no function sort, since it is unordered, not like a list, or tuple.

However, the key function, introduced in version 2.4, is helpful in sorting a dictionary.

To sort by items, and return the keys and items,
sorted( mydict.items(), key=lambda x: x[1] )
output:
[('b', 1), ('a', 2), ('d', 4), ('c', 5)]
To return only the keys, sorted by the items,
sorted( mydict.keys(), key=lambda x: mydict[x] )
output:
['b', 'a', 'd', 'c']
Note that, adding reverse=True at the end of the sorted function would produce a decending order list.
sorted( mydict.items(), key=lambda x: x[1] )
output:
[('c', 5), ('d', 4), ('a', 2), ('b', 1)]
Happy sorting.

Sunday, August 10, 2008

Evaluate Ankermann Function using Python

The Ankermann function, A(m, n) is defined as


The Python code below could be used to evaluate the Ankermann function.
def ack(m,n):
    if m == 0:
        return n+1
    elif m > 0:
        if n == 0:
            return ack(m-1, 1)
        elif n > 0:
            return ack(m-1, ack(m, n-1))
Usage:
ack(3, 4)
However, this code can't be used for larger values of m and n.

Friday, August 1, 2008

Even-valued Fibonacci terms summation

Problem #2 of Project Euler

Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million.

Analysis
The first 10 terms of Fibonacci sequence are
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

We need to add the even numbers terms
2 + 8 + 34, ...

Solution:

def sumEvenFibonacci( limit ):
    a, b = 1, 1
    sum = 0
    while b < limit:
        if b%2 == 0: sum += b
        a, b = b, a + b
    return sum

Usage:

print sumEvenFibonacci( 4000000 )
If you are interested in listing all the Fibonacci numbers less than a given limit, read this article.

Thursday, July 31, 2008

Generate Fibonacci numbers with Python

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Mathematically, we write it as:
F(n) = F(n) + F(n-1)

This can easily be done in Python, using generators.
def Fibonacci(n):
    a, b = 1, 1
    while b < n:
        yield b
        a, b = b, a + b
To print all the Fibonacci numbers less than 4000000,
for i in Fibonacci(4000000):
    print i,
Output:
>>> 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578

Monday, July 14, 2008

Komodo Edit save and run python macro

Komodo Edit is a good editor for many programming language. However, since it is free as compared to its brother Komodo IDE, there is lack of debugging function. But, Macro and run command do exist though. You can make use of those feature to save and debug your script with a click of a shortcut key.

Step 1: Create a new macro.


Step 2: Enter the macro script below.

if (komodo.view) { komodo.view.setFocus() };
komodo.doCommand('cmd_save')
ko.run.runEncodedCommand(window, '%(python) -O \"%F\" {\"cwd\": u\"%D\"}');
You are free to change %(python) to %(perl), provided that perl is installed, and the environment path is set.

Step 3: Set a key binding to the new created macro.


Now you just need to press the 'F5' key inorder to save and run a python script.

Tuesday, July 8, 2008

Executing Python Code Within Komodo Edit

Komodo Edit is a good free editor for Python development. However, there is no debugging feature in Komodo Edit. Therefore, it is a cumbersome process to execute Python scripts. Fortunately, there is a feature called Run command to satisfy this job.

To make use of the Run command feature to execute Python code, you can follow these steps:
  1. click Toolbox > Add > New Command.
  2. type "Run Python file" in the top field.
  3. enter in the command field,
    %(python) "%F"
  4. Click on the "Key Binding" tab and you can set the keyboard shortcut key (optional). I personally like the F5 key.
  5. Click OK.
Now, you can easily execute the Python script by pressing "F5", or by double clicking the "Run Python file" in your toolbox pane, which is usually located at the right. The output will be displayed in the Command output pane at the bottom.

For more information on the "Run Command":

http://aspn.activestate.com/ASPN/docs/Komodo/4.4/run.html

Wednesday, April 16, 2008

Add all the natural numbers below 1000 that are multiples of 3 or 5

Problem #1 of Project Euler

Add all the natural numbers below 1000 that are multiples of 3 or 5.

Analysis:

Sequence of multiple of 3: 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, ..., 999

Sequence of multiple of 5: 5, 10, 15, 20, 25, 30, ..., 995 So, the sum is 3 + 5 + 6 + 9 + 10 + 12 + 15 + 18 + ... + 999.

Take note that we have to remove the duplicate of 15, 30, 45, ..., 990

Method # 1: using Python

All the natural numbers below 1000 that are multiples of 3 or 5, are divisible by either 3 or 5. Hence, we need to add the numbers that are only divisible by 3 or 5. That is very easy in Python. Using the % operator will give use the remainder of the division.

For example, 4%2 = 0, 4%3 = 1. Therefore, in order to ad the number that is divisible by 3 or 5, we have to check for the remainder. We will only add the numbers that give zero for the remainder.
total = 0
for i in range(1000):
    if not (i % 3 and i % 5):
    total += i

Method # 2: using Arithmetic sequence

To add all natural numbers from 1 to 10.

Notice that the sum will always gives 11, if we write it this way. 11 occurs 10 times, so, 11 x 10 = 110. However, that is the sum of 1 to 10 twice. So, we divide 110 by 2, we get the answer, which is 55.

The formula: Sum = n (a1 + a2) / 2 where n is the number of occurrence of the sequence from a1 to a2.

Back to our problem, the sum of 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, ..., 999. Adding the first and last numbers gives us 3 + 999 = 1002. 1002 occurs 333 times, so 1002 * 333 = 333666. Divide it by 2, we get 166883.

Repeat the same procedure for the second sequence of 5s. Then, add the answers. However, as I mentioned previously, remember that we also have to remove the duplicates of 15, 30, 45, ..., 990.

The formula would be like this:

sum of multiple of 3 + sum of multiple of 5 - sum of multiple of 15

Python: Conditional Statement with lambda

Suppose that we have a conditional statement:

In Python, we can write it in three ways:

f = lambda x: x > 2 and (x + 1) or 0

or
f = lambda x: 0 if x <= 2 else (x + 1)

or
f = lambda x: [x + 1, 0][x <= 2]

However, if the number of conditions if more than 2, it is best to use the first method.

For example, consider this conditional statement:


Here is the Python code:
f = lambda x: (x >= 5 and x+1) or ( (x < 5 and x > 2) and 3 ) or 0

We read it as:
if x >= 5, f(x) = x + 1 else, if 2 < x < 5, f(x) = 3 else, f(x) = 0

Python: Parse Apache log to sqlite database

This Python script was written for a friend in Australia, as part of his Ph.D project. The script will parse the Apache server log into sqlite3 database.

Apache server log like this:
23.13.171.152 - - [26/Sep/2007:21:20:36 +0800]
“GET /forum/Themes/BlueStory/images/bbc/ftp://ftp.gif HTTP/1.1? 200 191
“http://www.malaysiaindah.com/index.php?action=post;topic=587.0;
num_replies=3?
“Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7)
Gecko/20070914 Firefox/2.0.0.7?
could be parse to an sqlite database, with the class below.
class Parser:
    def __init__(self, serverLog, db):
        if db.strip() == "":
        db = "log.db"
        try:
            conn = sqlite3.connect(db)
            cursor = conn.cursor()
            cursor.execute("create table if not exists log
            (ip, date, time, gmt, request,
            errorcode, bytes, referel, osa)" )
        except sqlite3.Error, error:
            wx.MessageBox(str(error), "Info")
            exit()

        numLog = len(open(serverLog).readlines())
        for line in open(serverLog, "r"):
            data = []
            a = line.split(‘"‘)
            line = line.split()
            data.append(line[0])

        #ip
        data.append(line[3][1:line[3].index(":")])

        #date
        data.append(line[3][line[3].index(":") + 1:])

        #time
        data.append(line[4][:-1])

        #gmt
        data.append(line[5][1:] + " " + line[6])

        #urlreq
        data.append(line[8])

        #statuscode
        data.append(line[9]) #bytestr data.append(line[10][1:-1])

        #referel
        data.append(a[-2])

        #osagent
        try:
            cursor.execute( "insert into log values(
             ?, ?, ?, ?, ?, ?, ?, ?, ?)",
            (data[0], data[1], data[2], data[3],
            data[4], data[5], data[6],
            data[7], data[8]) )
        except sqlite3.Error, error:
            wx.MessageBox(str(error), 'Info')
            exit()
        
        conn.commit()
        cursor.close()
        conn.close()

Python: py2exe setup file example

py2exe is a Python package that can convert Python scripts into executable Windows programs. This is an example of py2exe setup file.
from distutils.core import setup
import py2exe
import sys

# no arguments
if len(sys.argv) == 1:
    sys.argv.append("py2exe")

def compile(appName, console=False):
    OPTIONS = {"py2exe":
             {"compressed": 1, "optimize": 0, "bundle_files": 1, } }
    ZIPFILE = None
if console:
        setup(
            options=OPTIONS,
            zipfile=ZIPFILE,
            console=[appName]
        )
    else:
        setup(
            options=OPTIONS,
            zipfile=ZIPFILE,
            windows=[appName]
        )
compile(‘example.py’)