Python Tutorial

Wednesday, May 15, 2013

Shutil: high level file operations

The shutil module provides a number of high-level operations on files and collections of files.

import shutil
import os

# copy(& rename) file from src to dst.
src = os.getcwd()+"\\"+"a.txt"
dst = os.getcwd()+"\\"+"data\\new_a.txt"
shutil.copyfile(src, dst)

# copy file from src to dst directory.
src = os.getcwd()+"\\"+"a.txt"
dst = os.getcwd()+"\\data2"
shutil.copy(src, dst)

'''
    Recursively copy an entire directory tree rooted at src.
    dst file must not exists 
'''    
src = os.getcwd()+"\\" + "data"
dst = os.getcwd()+"\\" + "data3"
shutil.copytree(src, dst)

# Recursively move a file or directory to another location.
src = os.getcwd()+"\\" + "data3"
dst = os.getcwd()+"\\" + "data4"
shutil.move(src, dst)

# delete entire directory
dest_file = os.getcwd()+"\\data3"
shutil.rmtree(dst)

Tuesday, May 14, 2013

django-grappelli: Custom admin panel

django-grappelli makes django admin panel cool looking. It is a grid-based alternative/extension to the django administration interface. I used this module in many projects.

Installation:
$ pip install django-grappelli

django celery : asynchronous task queue, scheduling

django-celery provides celery integration for django. Using django-celery we can maintain asynchronous task queue/job queue. It is focused on real-time operation, but supports scheduling as well.

Installation:
$ pip install django-celery

Monday, May 6, 2013

Python string title - auto capitalize first character of the word

Python auto capitalize first character of all the word/words in a string.
s1 = "life is very easy with python"
s2 = "python"
print s1
print s1.title()

print s2
print s2.title()

Output:
life is very easy with python
Life Is Very Easy With Python
python
Python

Saturday, May 4, 2013

Itertools - Combinatoric generators

  • product: Equivalent to nested for-loops in a generator expression.
  • permutations: Return successive r length permutations of elements in the iterable.
  • combinations: Return r length subsequences of elements from the input iterable.
  • combinations_with_replacement: Return r length subsequences of elements from the input iterable allowing individual elements to be repeated more than once.
import itertools

A = [ 1, 2, 3]
B = [4]
C = [5, 6]

print "product example:"
s=[ A, B, C ]
print list(itertools.product(*s))


print "permutations example:"
i = 1
for n in itertools.permutations(A,3):
  print i,":",n
  i += 1


print "combinations example:"  
i = 1
for n in itertools.combinations("ABCD",3):
  print i,"[:]",n
  i += 1

print "combinations_with_replacement example:"    
print list( itertools.combinations_with_replacement("ABD",3))


Output:
product example:
[(1, 4, 5), (1, 4, 6), (2, 4, 5), (2, 4, 6), (3, 4, 5), (3, 4, 6)]
permutations example:
1 : (1, 2, 3)
2 : (1, 3, 2)
3 : (2, 1, 3)
4 : (2, 3, 1)
5 : (3, 1, 2)
6 : (3, 2, 1)
combinations example:
1 [:] ('A', 'B', 'C')
2 [:] ('A', 'B', 'D')
3 [:] ('A', 'C', 'D')
4 [:] ('B', 'C', 'D')
combinations_with_replacement example:
[('A', 'A', 'A'), ('A', 'A', 'B'), ('A', 'A', 'D'), ('A', 'B', 'B'), ('A', 'B', 'D'), ('A', 'D', 'D'), ('B', 'B', 'B'), ('B', 'B', 'D'), ('B', 'D', 'D'), ('D', 'D', 'D')]

Itertools - Python iterators terminating on the shortest input sequence.

Iterators terminating on the shortest input sequence:
  • imap : Make an iterator that computes the function using arguments from each of the iterables. Always consider shortest iterables.
  • chain : Make an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable ans so on.
  • ifilter : Make an iterator that filters elements from iterable returning only those for which the predicate is True.
  • ifilterfalse : Make an iterator that filters elements from iterable returning only those for which the predicate is False.
  • compress : Make an iterator that filters elements from data returning only those that have a corresponding element in selectors that evaluates to True.
  • izip : Make an iterator that aggregates elements from each of the iterables.
  • izip_longest : Same as izip, except for uneven length values are filled by default None.
import itertools

def add(*args):
  s = 0;
  for num in args:
    s += num
  return s

print "imap example:"
# will calculate 5+2+10, 2+3+10
for n in itertools.imap(add, (5,2,3), (2, 3, 3),(10, 10)):
    print n


print "chain example"
for ch in itertools.chain('ABC','DEF'):
  print ch


print "ifilter example "
A = [1,2,4,5,7,8]
for n in itertools.ifilter(lambda x:x%2 ,A):
  print n


print "ifilterfalse example:"
for n in itertools.ifilterfalse(lambda x:x%2 ,A):
  print n


print "compress example:"
for ch in itertools.compress('abcdef',[0,1,0,1,1,1]):
  print ch

Output:
imap example:
17
15
chain example
A
B
C
D
E
F
ifilter example 
1
5
7
ifilterfalse example:
2
4
8
compress example:
b
d
e
f
izip example:
('a', 'X')
('b', 'Y')
izip_longest example:
('a', 'X')
('b', 'Y')
('c', None)
('d', None)