Python Tutorial

Friday, October 19, 2012

python base64 encode decode


Python base64 encode decode is very simple. Lets see a sample code

 
import base64
encoded = base64.b64encode('life is very easy with python')
print encoded
data = base64.b64decode(encoded)
print data


Output:
bGlmZSBpcyB2ZXJ5IGVhc3kgd2l0aCBweXRob24=
life is very easy with python

Thursday, October 18, 2012

Python datetime to timestamp two way conversion


It is very nice to having datetime to timestamp two way conversion. Python simply give us this feature. Lets have a look

 
from datetime import datetime
import time

date= datetime(2012,10,16) # You set date as datetime.now()
print date
print time.mktime(date.timetuple())
print datetime.fromtimestamp(time.mktime(date.timetuple()))


Output:
2012-10-16 00:00:00
1350324000.0
2012-10-16 00:00:00

Tuesday, October 9, 2012

Design pattern in python : decorator pattern


Python decorator allow us to modify or inject code into a function and classes. It is very easy to use.
Here something we need to remember, __init__ function exceute when decorator is declare and __call__ execute when function('doSomethingCopy') will call independently. We can also call function('doSomethingCopy') from anywhere of the decorator class('MyDecorator').

 
class MyDecorator(object):
    def __init__(self, f):
        self.doSomethingCopy=f
        print "This line will call on decoration"

    def __call__(self,p,q): # here we pass the function parameter 
        print "This line will work when we call the function 'doSomething' "
        self.doSomethingCopy(p+1,q+1)

@MyDecorator
def doSomething(a,b):
    print "I am doing something"
    print "A ",a," B ",b

doSomething(3,4)


Output:
This line will call on decoration
This line will work when we call the function 'doSomething' 
I am doing something
A  4  B  5

Sunday, October 7, 2012

Python design pattern : Iterator


Iterators are built into the Python language. Dictionaries, lists, tuples, strings, streams, generators, and classes for which you implement iterator syntax are iterable. Lets see an example of iterator pattern with generator.

 
def show_chars(n):
    list=['A','B','C','D','E','F','G','H','I','J']
    for data, pos in zip(list, range(n)):
        yield data

chars_set= show_chars(5)
for ch in chars_set:
    print ch


Output:
A
B
C
D
E

Python keyword: zip


Python zip keoword return a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The returned list is truncated in length to the length of the shortest argument sequence.

 
a=[1,2,3]
b=[4,5,6]

z=zip(a,b)
print z

ai,bi=zip(*z)
print ai
print bi

print "-"*25

a=[1,2]
b=[4,5,6]

z=zip(a,b)
print z

ai,bi=zip(*z)
print ai
print bi

print "."*25

a=[1,2,4]
b=[4,5]

z=zip(a,b)
print z

ai,bi=zip(*z)
print ai
print bi


Output:
[(1, 4), (2, 5), (3, 6)]
(1, 2, 3)
(4, 5, 6)
-------------------------
[(1, 4), (2, 5)]
(1, 2)
(4, 5)
.........................
[(1, 4), (2, 5)]
(1, 2)
(4, 5)

Python keyword: Yield



Python Yield keyword work as return, but it always return a generator. You can get the value/values by iterating the generator.
It is very usefull when you know your function will return a huge set of values that you will only need to read once
Code:

 
def myFun(n):
    for i in range(0,n):
        yield i


it=myFun(5)

for i in it:
    print i


Output:
0
1
2
3
4