Add month value in python

Tuesday, August 6, 2013

Subscribe for future Changes.. Explanation soon..

This is short and sweet method to add a month in a date using relativedelta.
from datetime import datetime
from dateutil.relativedelta import relativedelta

date_after_month = datetime.today()+ relativedelta(months=1)
print 'Today: ',datetime.today().strftime('%d/%m/%Y')
print 'After Month:', date_after_month.strftime('%d/%m/%Y')
Output:
Today: 01/03/2013
After Month: 01/04/2013

Easy way to remove duplicate items from a list using set.

Tuesday, July 16, 2013

A program to check if a list has any duplicates and if it does it removes them and returns a new list with the items that werent duplicated/removed.
In Python it can be done easily. First make a set from the list and then make a list for that set.
 a_List = list(set(a_List))  
Here is a python example:
 >>> a_List = [1, 2, 3, 3, 2, 2, 4, 5, 5]  
 >>> a_List  
 [1, 2, 3, 3, 2, 2, 4, 5, 5]  
 >>> a_List = list(set(a_List))  
 >>> a_List  
 [1, 2, 3, 4, 5]  
Comments are always welcome!
Have a Happy Python!

Calculate time difference in minutes

Thursday, June 13, 2013


Below code will explain how to find a difference in the minute,
if you have two dates, then you can find the difference of the time using divmode function of python
 >>> import datetime  
 >>> end = datetime.datetime.now()  
 >>> start = datetime.datetime.now()  
 >>> diff  
 datetime.timedelta(0, 7, 424199)  
 >>> diff = start - end  
 >>> divmod(diff.days * 86400 + diff.seconds, 60)  
 (0, 7) # 0 minutes, 7 seconds  

Creating generators objects in python

Sunday, June 2, 2013

What is Generator?

A generator is a function which can stop whatever it is doing at an arbitrary point in its body, return a value back to the caller, and, later on, resume from the point it had `frozen' and merrily proceed as if nothing had happened (link) . Here is a simple example: 

if you write 

x = [n for n in foo if bar(n)]

you can get the list object assigned to the x.



x=(n for n in foo if bar(n))
you can get out the generator and assign it to x. Now it means you can do

Monkey patching objects in python

Thursday, May 30, 2013


Every object in Python has a __dict__ member, which stores the object's attributes.
So, you can do something like this:

 class surf(object):  
   def __init__(self, arg1, arg2, **kwargs):  
     #do stuff with arg1 and arg2  
     self.__dict__.update(kwargs)  
 s = surf('arg1', 'arg2', bar=20, baz=10)  
 # Here s is a object of surf with two extra attributes.  


Built-in getattr function

 You can get a reference to a function without knowing its name until run-time, by using the getattr function.
 class Cls():  
   def getfood(self):  
     self.food = ['Cheese', 'Pizza', 'Burger', 'Hotdog', 'Pie', 'Fires']  
     return self.food  
 c = Cls()  
 print getattr(c, 'getfood')() 

The output of following code will be
 ['Cleese', 'Palin', 'Idle', 'Gilliam', 'Jones', 'Chapman']

Devide a list in n sized chunks.

Friday, May 24, 2013


Here is the method to devide list in to chunks of n size
 def chunks(list, size):  
   for i in xrange(0, len(list), size):  
     yield list[i:i+size]  
 for list in list(chunks(product_without_season, 1000)):  
   print list  
 

Search This Blog

Loading...