Tuesday, July 30, 2013

List Comprehension Use Case

Problem : 

We have a list of  key , value pair and we just need to sum up the value part of the list. One way in languages like C# or Java was to loop through the whole loop and sum up the value part of the KeyValue object.

Solution:

    class KeyValue():
   
    def __init__(self,key,value):
    self.key = key
        self.value = value
        def __unicode__(self):
        return self.key+"~"self.value
So typical C# or Java code for the key value object mentioned above will look like -

    def sum_up_list():
    # Lets create List
    key_value_list = []
    key_value_list.append(KeyValue('V1',1))
    key_value_list.append(KeyValue('V2',2))
    # What if to the sum the list
    sum_of_value = sum([x.value for x in key_value_list])  ----------One line code

       foreach obj in key_value_list {sum = sum + obj.value }; -----Same effect using loop in C#

And we are good to go


Tuesday, July 23, 2013

Make dictionary out of two lists

Problem : Now that we have two lists - One list of keys and other list of values and we want to make dictionary out of this. How to do this using Python

Solution : Use zip command to merge the two lists. If the list count is not equal the zip command will truncate the longer one. So I will try to put a length check before merging.

So if keyList and valueList are the two lists we need to merge and make dictionary , lets make one :


newDict ={}
if len(keyList) == len(valList):
    newDict = dict(zip(keyList,valList))

And we are good to go :)

Saturday, July 20, 2013

Generating a Time Interval Based String

Problem :  Need to generate time divided string for time series representation of data. The data can be in 1 minute interval or can be 15 minute interval or can be 1 hour interval. 

Sample Output: 
  • if  timeInterval in 1 min : '01/01/2001 00:00~01/01/2001 00:01'............23:58,23:59,24:00
  • if  timeInterval in 15 min : '01/01/2001 00:00~01/01/2001 00:15'.......,
  • if  timeInterval in 30 min : 01/01/2001 00:00~01/01/2001 00:30'...........
Solution:
Lets think , we need to travel through the day based on time interval - can be of 1 min , 15 min , 30 min , 60 min till the end of day. 
So start condition is : 'startDate 00:00'
timeInterval : deltaT 
end Condition when the day gets over so = startDate+ 1 day
Below is the python code that does just that :
from datetime import timedelta,datetime  
timeGrain = 15 # Can be 30 or so  
deltaT= timedelta(minutes=int(timeGrain)) 
startDate = datetime.datetime.strptime(inputDateTime,'%Y-%m-%d %H:%M:%S') # Convert  String to Date time 
endDate= (startDate+timedelta(days=1))  # End Condition 
while startDate != endDate:
    var = startDate
    nextDateTime = startDate+ deltaT
    keyList.append(str(var)+'~'+str(nextDateTime))
    startDate =nextDateTime
   
And the output :
['2013-03-01 00:00:00~2013-03-01 00:15:00', '2013-03-01 00:15:00~2013-03-01 00:3 0:00', '2013-03-01 00:30:00~2013-03-01 00:45:00', '2013-03-01 00:45:00~2013-03-0 1 01:00:00', '2013-03-01 01:00:00~2013-03-01 01:15:00', '2013-03-01 01:15:00~201 3-03-01 01:30:00', '2013-03-01 01:30:00~2013-03-01 01:45:00', '2013-03-01 01:45: 00~2013-03-01 02:00:00', '2013-03-01 02:00:00~2013-03-01 02:15:00', '2013-03-01 02:15:00~2013-03-01 02:30:00', '2013-03-01 02:30:00~2013-03-01 02:45:00', '2013- 03-01 02:45:00~2013-03-01 03:00:00', '2013-03-01 03:00:00~2013-03-01 03:15:00']

So we are good to go with timedelta object one can iterate over seconds , minutes and days



Thursday, July 18, 2013

Community for Development

Well ,

I am really impressed by various community tools present for any Open Source Developer . Will I be wrong in saying .... One Open stack developer knows and learns more that one Enterprise ( Read .Net  Microsoft Platform ) and can use lot of code and best practices and need not to write lot of stuff for things that are already done and made and perfected  , but in this case the open source . ( Read Linux OS on Apache Web Server ) end up having issues with integration , getting things working and moving , downloading and installing packages after packages to make the stuff moving , Unlike Microsoft's one click and done process ! ...Still to have a opinion on this.