Python Tutorial

Wednesday, April 1, 2015

Pillow python image processing library


Pillow is fork form PIL (Python Imaging Library) and very easy to use. To download source and/or contribute please follow https://github.com/python-pillow/Pillow

Tuesday, March 31, 2015

Nose unittest OOP example


Here we will test helloWorld.py using nose
All source code are available on github
# testHelloWorldOOP.py
__author__ = 'AbuZahedJony'

from helloWorld import add_two_num
from helloWorld import multi_two_num

class TestHelloWorld:
    def __init__(self):
        pass

    # This function will run before any test case (only once)
    @classmethod
    def setup_class(cls):
        print "Main Setup"

    # This function will run after all test case (only once)
    @classmethod
    def teardown_class(cls):
        print "Main Teardown"

    # This function will call per test case (before)
    def setup(self):
        print "SETUP"

    # This function will call per test case (after)
    def teardown(self):
        print "TEAR-DOWN"

    def test_add_num(self):
        print 10*"*"+" Test add num"
        assert add_two_num(2, 3) == 5
        assert add_two_num(-2, 3) == 1

    def test_multi_num(self):
        print 10*"*"+" Test multi num"
        assert multi_two_num(2, 3) == 6
        assert multi_two_num(-2, 3) == -6
  

RUN: nosetests -s testHelloWorldOOP.py

Output:
Main Setup
SETUP
********** Test add num
TEAR-DOWN
.SETUP
********** Test multi num
TEAR-DOWN
.Main Teardown

----------------------------------------------------------------------
Ran 2 tests in 0.002s

OK

Nose unittest simple example


Here we will test helloWorld.py using nose
All source code are available on github
# testHelloWorld.py
from nose import with_setup

__author__ = 'AbuZahedJony'

from helloWorld import add_two_num
from helloWorld import multi_two_num

def m_setup():
    print "\nRun SETUP"

def m_teardown():
    print "Run TEAR-DOWN"

@with_setup(m_setup, m_teardown)
def test_add_num():
    print "Running test ADD"
    assert add_two_num(2, 3) == 5
    assert add_two_num(-2, 3) == 1
    assert add_two_num(-2, -3) == -5

@with_setup(m_setup, m_teardown)
def test_multi_num():
    print "Running test MULTI"
    assert multi_two_num(2, 3) == 6
    assert multi_two_num(-2, 3) == -6
    assert multi_two_num(-2, -3) == 6


RUN: nosetests -s testHelloWorld.py

Output:

Run SETUP
Running test ADD
Run TEAR-DOWN
.
Run SETUP
Running test MULTI
Run TEAR-DOWN
.
----------------------------------------------------------------------
Ran 2 tests in 0.002s

OK

Nose python unittest framework


Nose official doc

Setup

pip install nose

Please install pip if required

Tuesday, September 30, 2014

Python profiling

Profiling in python is very handy. It will help you to monitor your code performance and find the hot spot of your code.
Here I am using cProfile for profiling. It is built in with python.

Download sample code , unzip the file and cd to the folder.
No run this command python -m cProfile a.py
Output will be

Visualization of cProfile data:

I used RunSnakeRun for visualization.
Now run this following two command (make sure you installed RunSnakeRun)
python -m cProfile -o file.prof a.py
runsnake file.prof

Output will be

Check it out.

Sunday, September 28, 2014

Min, max value and index from list

See also enumerate and operator.itemgetter
import operator
a = [2, 5, 1, 4, 8, 71, 4, 1, 21]
min_i, min_v = min(enumerate(a), key = operator.itemgetter(1))
max_i, max_v = max(enumerate(a), key = operator.itemgetter(1))

print "[Min] index:",min_i,"value:",min_v
print "[Max] index:",max_i,"value:",max_v


Output:
[Min] index: 2 value: 1
[Max] index: 5 value: 71

Python: Absolute difference between successive elements in list


See also abs, zip and islice
from itertools import islice

a = [1, 4 , 2 , 6 , 7, 3]
result = [abs(p-q) for p, q in zip(a, islice(a, 1, None))]

print result

Output:
[3, 2, 4, 1, 4]